Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
367 views
in Technique[技术] by (71.8m points)

php - Undefined variable: _SESSION when sending variables via post through JavaScript trigger

In my index.php file I call session_start() and set a couple of session variables. In a second PHP file I would like to access these session variables.

Thing is, this PHP file is purely a backend script and is POSTed to when a JavaScript function is triggered. When the POST call attempts to cause the script in the second PHP file to be executed the error log is reporting that:

_SESSION is an undefined variable.

I've tried calling start_session() and session_regenerate_id() at the top of the second PHP file but the issue has persisted.

I'm assuming what is going on is that because it's in a POST this PHP file is in its own session as I am still able to do this $_COOKIE[ini_get('session.name')].

The information that I am trying to pass to the second PHP file isn't anything that needs to be secured but it would be nice to know in the future how to do this: call a PHP file via a POST and still have my session variables.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There's nothing special whatsoever about POST requests and sessions.
You just need to call session_start at the top of every file request you want to use sessions in, that's it.

Try again with that in mind, it ought to work.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...