duanjing2013 2011-04-22 13:52
浏览 56
已采纳

重定向后丢失会话变量

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?

  • 写回答

2条回答 默认 最新

  • duanbei1709 2011-04-22 13:58
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集