Why can't I access session variables from my AJAX-called PHP script?

前端 未结 7 2026
情话喂你
情话喂你 2021-01-11 11:03

I have one PHP script with a session variable, set like so:

$_SESSION[\'VAR1\'] = \"test\"

Now, I am using AJAX via a jQuery-initiated POST

7条回答
  •  旧时难觅i
    2021-01-11 11:40

    You need do this on every page that accesses the session before you access it:

    session_start();
    

    That means on both the page that sets the session variable and the AJAX page that tries to retrieve it. Both need to call session_start().

    As long as the AJAX request calls a script in the same domain (and thus gets access to the session cookie) there is no reason why it couldn't get access to the session variables. An AJAX request after all is just another HTTP request.

提交回复
热议问题