重定向后丢失会话变量

问题描述:

This is my setup. The user can connect with facebook. I then use the facebook id to find the id in my user table. I want to add the facebook id and the user id from my table in the session. Then I redirect the user to the main page.

        /*
         * Leg til facebook id i session hvis bruker logger in/registrer seg med facebook
         */      
        $_SESSION['fb_id'] = $fb['id'];                     

        /*
         * Set user id
         */
        $_SESSION['id'] = $id; 

        /*
         * Auto log-out after 45 minutes
        */          
        $_SESSION['expires'] = time()+(45*60);

        /*
        * Log to db
        */  
        $this->log_login_attempt(true); 

        /*
        * Redirect user to game
        */           
        $host = $_SERVER["HTTP_HOST"]; 
        header("location: http://$host/homepage.php");  //@ redirect
        exit();

If I do a echo var_dump($_SESSION) before the redirect I get:

array(3) { ["fb_id"]=> string(9) "7683402XX" ["id"]=> int(12) ["expires"]=> int(1303482308) } 

So I redirect the user to homepage.php here I just enter:

error_reporting(E_ALL); 
ini_set("display_errors", true); 
require '../sys/core/errorhandler.php';
set_error_handler('my_error_handler');

$a = session_id();
if ($a == '') session_start();

echo var_dump($_SESSION); 
exit();     

This result in:

array(2) { ["id"]=> int(12) ["expires"]=> int(1303482647) } 

Somehow $_SESSION['fb_id'] disappeared? How can this happen?

这是我的设置。 用户可以与Facebook连接。 然后我使用facebook id在我的用户表中查找id。 我想在会话中添加我的表中的facebook id和用户ID。 然后我将用户重定向到主页面。 p>

  / * 
 * Leg til facebook id i session hvis bruker logger in / registrer seg med facebook 
 * / 
 $ _SESSION ['fb_id'] = $ fb  ['ID'];  
 
 / * 
 *设置用户ID 
 * / 
 $ _SESSION ['id'] = $ id;  
 
 / * 
 * 45分钟后自动注销
 * / 
 $ _SESSION ['expires'] = time()+(45 * 60); 
 
 / * 
 *日志 到db 
 * / 
 $ this-> log_login_attempt(true);  
 
 / * 
 *将用户重定向到游戏
 * / 
 $ host = $ _SERVER [“HTTP_HOST”];  
标题(“location:http://$host/homepage.php”);  // @ redirect 
 exit(); 
  code>  pre> 
 
 

如果我在重定向之前执行 echo var_dump($ _ SESSION) code>: p>

  array(3){[“fb_id”] =>  string(9)“7683402XX”[“id”] =>  int(12)[“expires”] =>  int(1303482308)} 
  code>  pre> 
 
 

所以我将用户重定向到 homepage.php code>这里我只需输入: p> \ ñ

 的error_reporting(E_ALL);  
ini_set(“display_errors”,true);  
require'../sys/core/errorhandler.php';
set_error_handler('my_error_handler');
nna = session_id(); 
if($ a =='')session_start(); 
  
echo var_dump($ _ SESSION);  
退出();  
  code>  pre> 
 
 

结果如下: p>

  array(2){[“id”] =>  int(12)[“expires”] =>  int(1303482647)} 
  code>  pre> 
 
 

以某种方式 $ _ SESSION ['fb_id'] code>消失了? 怎么会发生这种情况? p> div>

Make sure you have session_start() at the very top of your script before you set $_SESSION['fb_id'] and so on. Without the session started, when you dump $_SESSION the values will be there, but as regular variables - they won't persist between page loads.

I once had the same issue and it turned out that there was no more available disk space on the server, so it couldn't store $_SESSION variables.