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

前端 未结 7 1973
情话喂你
情话喂你 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条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-11 11:27

    Make sure no content has been echoed (not even a whitespace) before calling session_start().
    To be safe, put the code as the first code of whatever template you used for the page. The function will not work if content has been sent to the browser.
    To test and see where the problem is, call the page as a stand-alone, instead of through AJAX and ensure that it works before AJAXing it.

提交回复
热议问题