doujiangqu2823 2014-11-03 01:19
浏览 43
已采纳

PHP - 丢失$ _SESSION变量索引

I want to pass an array to another page, then use elements in that array to make JSON, and then echo it out for the first page to access it.

Right now I can send the array to the other page, I then format it how I want, I echo it out for the first page to get, but then when I try to echo fetch_get_contents of the url I'm echoing the info on, it all of a sudden breaks the $_SESSION variable.

This is in my first file

$_SESSION['map'] = $rowContentArray;
$url2 = "http:....php";

in my second file I can get the array fine and I do everything I want with it I echo out the info I want from my other page, then when i try to access the info on the first page again, it breaks

just by adding this last line

$_SESSION['map'] = $rowContentArray;
$url2 = "http:.....php";
echo file_get_contents($url2, true);

I get the error that the index i'm using to access the array in the $_SESSION variable is undefined in my second file

$map = $_SESSION['map'];

error:

Notice: Undefined index: map

On the first file, I can echo out any random string after the session stuff and everything still works..but when i try to echo the url contents it breaks.

I am very confused as to what could be causing the session variable to be lost.

Thank you in advance for the help.

  • 写回答

1条回答 默认 最新

  • duanfang7757 2014-11-03 05:56
    关注

    Mate, there is no any errors in your scripts! Actually this is exactly the expected behavior.

    When you access the page from your browser a specific COOKIE is set and the $_SESSION array is bind to this cookie.

    When you access the page with file_get_contents($url2, true); The server is assigning another COOKIEwith brand new $_SESSION array bind to it.

    Obviously the second $_SESSION array do not have map key set, so as expected you are getting
    Notice: Undefined index: map

    Keep in mind that the variable $_SESSION is session specific, so if you have say 1000 simultaneously opened sessions to the server you have 1000 different versions of $_SESSION variable.

    Check this article to get the things clearly: http://en.wikipedia.org/wiki/HTTP_cookie#Session_cookie

    If you can define what is your specific use case we will be able to help you more.

    The usual approach is to use GET or POST variables when you need to transfer data between different sessions. For example in your case you can try:
    file_get_contents($url2 . '?map=' . json_encode($rowContentArray));

    Then instead in the $_SESSION['map'] you will get your array in $_GET['map']

    If the array $rowContentArray is big enough not to fit as a GET parameter, then you will have to use POST method.

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

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀