doudiao2335 2014-06-14 13:19
浏览 43
已采纳

session_destroy()+ session_start()不在标题重定向之后

Trying to temporary store information in a multipage sign up. Because I don't want old sessions to mess with the new sign up data I'm trying to destroy the old session. The problem is the following.

Not working:

signup1.php

//Start new session
session_regenerate_id(TRUE);
session_destroy();
unset($_SESSION);
session_start();

//Store values in session
$_SESSION['created']    = time(); 

//Redirect to second step
header('Location: '.$settings->siteurl.'signup2.php');
exit();

signup2.php

<pre>
<?php 
    //Print $_SESSION (empty array)
    print_r($_SESSION); 
?>

Working (but returns old $_SESSION values + updated values):

//Start new session
session_regenerate_id(TRUE);

//Store values in session
$_SESSION['created']    = time(); 

//Redirect to second step
header('Location: '.$settings->siteurl.'signup2.php');
exit();

What could resolve the problem? First session_start(); is set in init.php but it doesn't matter if I place it above session_regenerate_id(TRUE), array stays empty.

  • 写回答

1条回答 默认 最新

  • dongyangzhi0687 2014-06-14 13:37
    关注

    You should use this first:

    session_start(); // Starts a new or resumes an existing session
    

    Then you may use:

    session_regenerate_id(TRUE); // regenerates the active session id
    

    The TRUE/delete_old_session parameter is used for:

    Whether to delete the old associated session file or not.

    The session_regenerate_id is useful to prevent session hijacking and it just regenerates a new id but keeps session data. This should be used when user's access level changes or using a time interval (i.e. after every 10 minutes) but before you regenerate another new session id you need to start the session first.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线