Session不能与www一起工作


Session wont work with www

我的问题是:当用户登录到我的网站时,我将他们所有的用户信息放入一个会话中,如下所示

session_start();
//Put all user info into session cookie
$_SESSION["login"] = 'true';
$_SESSION["id"] = $user_info['id'];
$_SESSION["firstname"] = $user_info['firstname'];
$_SESSION["lastname"] = $user_info['lastname'];
$_SESSION["screen_name"] = $user_info['screen_name'];
$_SESSION["facebook"] = $user_info['facebook'];
$_SESSION["email"] = $user_info['email'];
$_SESSION["date_joined"] = $user_info['date_joined'];
$_SESSION["account_type"] = $user_info['account_type'];
$_SESSION["account_active"] = $user_info['account_active'];
$_SESSION["hashed_password"] = $user_info['hashed_password'];

问题是,如果他们从www.domain.com登录,然后结束在domain.com的页面或其他方式,他们从domain.com登录,并结束在www.domain.com页面上存储的信息会话是不可用的。

我怎么能有会话信息可用,无论他们登录与www或不?

这样做合适吗:

    <?php
    //Ok I modified the code so I don't get the undefined errors I was getting
//OLD CODE
    //$currentCookieParams = session_get_cookie_params(); 
    //$rootDomain = '.domain.com'; 
    //session_set_cookie_params( 
        //$currentCookieParams["3600"], 
        //$currentCookieParams["/"], 
        //$rootDomain, 
        //$currentCookieParams["false"], 
        //$currentCookieParams["false"] 
    //); 
    //session_name('mysessionname'); 
//NEW CODE
    $rootDomain = '.beckerfamily1.com'; 
    session_set_cookie_params( 3600, '/', $rootDomain, false, false); 
    session_start();
    if (isset($_SESSION['LAST_ACTIVITY']) && (time() - $_SESSION['LAST_ACTIVITY'] > 2700)) {
        // last request was more than 45 min ago
        if(isset($_SESSION['id'])){
        $connection = mysql_connect('localhost', '******', '*******');
        if (!$connection){
            die('Database connection failed: ' . mysql_error());
            }
        $db_select = mysql_select_db('beckerfamily');
            if(!$db_select){
                die('Could not select database: ' . mysql_error());
                }
        $query = "UPDATE users SET online='no' WHERE id='{$_SESSION['id']}' LIMIT 1";
        $result = mysql_query($query);
        if (!$result) {
                die("Database query failed: " . mysql_error());
            }
        }
            $_SESSION = array();
            if(isset($_COOKIE[session_name()])) {
                setcookie(session_name(), '', time()-42000, '/');
            }
        session_destroy();   // destroy session data in storage
        session_unset();     // unset $_SESSION variable for the runtime
        if(isset($connection)){
            mysql_close($connection);
            }
     }
    $_SESSION['LAST_ACTIVITY'] = time(); // update last activity time stamp
    ?>

也有必要有session_name('mysessionname');或我可以省略它和PHP将设置会话名称自己?

cookie(如PHPSESSID cookie)仅在其设置的域上可用。您可以使域包括所有子域:

ini_set('session.cookie_domain', '.example.com' );

或者如果配置不允许重写,

$currentCookieParams = session_get_cookie_params(); 
$rootDomain = '.example.com'; 
session_set_cookie_params( 
    $currentCookieParams["lifetime"], 
    $currentCookieParams["path"], 
    $rootDomain, 
    $currentCookieParams["secure"], 
    $currentCookieParams["httponly"] 
); 
session_name('mysessionname'); 
session_start(); 
http://php.net/manual/en/function.session-set-cookie-params.php

更好的可能是选择是否要通过www访问您的站点,并将所有请求重定向到另一个。

我不知道你在使用什么语言,但你需要改变你的会话cookie的"域"属性。如果你设置cookie域为"domain.com",它将被"domain.com"answers"www.domain.com"访问。