php - 重定向后丢失 session 变量

标签 php mysql arrays session variables

这是我的设置。用户可以与 facebook 连接。然后我使用 facebook id 在我的用户表中查找该 id。我想在 session 中添加我的表中的 facebook id 和用户 id。然后我将用户重定向到主页。

        /*
         * 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();

如果我在重定向之前执行echo var_dump($_SESSION),我会得到:

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

所以我将用户重定向到homepage.php,在这里我只需输入:

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();     

结果是:

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

$_SESSION['fb_id'] 不知何故消失了?怎么会发生这种事?

最佳答案

在设置 $_SESSION['fb_id'] 等之前,请确保脚本的最顶部有 session_start() 。如果没有启动 session ,当您转储 $_SESSION 时,值将在那里,但作为常规变量 - 它们不会在页面加载之间持续存在。

关于php - 重定向后丢失 session 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5756119/

相关文章:

mysql - 如何计算 3 个不同表格之间的利润

mysql - 如何将时间滚过 24 :00

php - 多维数组转JSON

php - mysql表无数据时获取消息而不是表

php - 使用许多不同长度的数组从 foreach 将数据插入 db

php - Mysql 加入 1 行与多行 - 组 concat 或 php?

php - 在 foreach 循环中使用数组变量

php - 通过 jQuery AJAX 传递表单数组数据

mysql - 如何组合2个select语句并将结果输出到2列

c - 在c中实现维特比算法哪种更好?