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.

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

报告相同问题?

悬赏问题

  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题