dongqiang8058 2013-05-11 01:19 采纳率: 100%
浏览 104
已采纳

重定向后,会话变量消失

I have been fighting with this problem all day. I have read numerous SO and forum posts where so many others had this same problem, and the posts spanned years.

My problem was part of a back end system I wrote to allow very basic alterations to database data, New entries could be added, updated or deleted. Pretty standard issue stuff. For simplicity, each function, insert, update, delete and an overall view of the database contents were on separate pages (insert.php, update.php, depete.php).

When adding a new entry, editing or deleting an existing entry, a redirect followed that would take the user back to the view,php page to show the updated data list. Problem is, the redirect wasn't working. The session variable was somehow discarded during the redirect which, due to my code, tossed the user back to the login page.

Here was my code:

if ($done || !isset($_GET['client_id'])) {
header('Location: http://website.com/admin/view.php');
exit;
}

Many thanks to all of you! It checked to make sure the updated data was posted and if all was well, redirected to view.php.

But it wouldn't, and yes, my pages all started with the necessary <?php session_start(); ?>. So after hours of scouring the web, I came across a nine-year old entry in the PHP manual that I felt was worthy of sharing:

http://www.php.net/manual/en/ref.session.php#37555

In it, the poster mentions, "Be aware of the fact that absolute URLs are NOT automatically rewritten to contain the SID. "

He suggested, "Skipping the 'http:' did the job." so I removed it from my code as such:

if ($done || !isset($_GET['client_id'])) {
header('Location: view.php');
exit;
}

And it WORKED. This topic has been a headbanger for many of us and I wanted to share it for what it's worth.

HOWEVER, I also do have a question, and that is, what would the proper procedure be to allow an absolute URL to be written that did contain the SID?

  • 写回答

1条回答 默认 最新

  • duanpai9945 2013-05-11 15:38
    关注

    DO NOT pass the session ID in from the URL.

    http://en.wikipedia.org/wiki/Session_fixation

    Use cookies. You can perform a best-effort same-origin check on the session ID given to you by storing the creator's IP address for example. Cookies are harder to tamper with than a simple URL. If they "disappear" then that means your user cleared their cookies and does not want you to track their session anymore.

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

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路