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条)

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集