If i have several PHP files and im setting a session variable in one file, will the same set value be available in another file for same session ?
2 Answers
Yes it should be as long as you call session_start() in each page
8 Comments
Cygnus
In each page meaning in each PHP file ?
CodeCaster
@Cygnus no, you need to call
session_start() once per request. The best way is to put it in your entry point (i.e. index.php or a config.php that gets included for every request). After that all files you include (during the same request) will be able to access your session variables.Andrew Hall
It depends if you are using a framework etc. If you are using just plain PHP with no framework (and therefore no page that is included every request), you will need to include every page. The example on the PHP manual i sent explains.
Cygnus
@AndrewHall : I read that. Im still facing problem in my code though. I have a GetData.php file the script in which will be executed (it sets the session variables) when page client_show.php loads. Now, im using AJAX to retrieve data from another PHP file on a user action. In that other file, i have to access the variables set by GetData.php. But when i do, the session variable is turning up as empty.
Cygnus
OK, there seems to be some other problem altogether. In the file in which i'm accessing the session variable ( set in another file ), it says Undefined variable $_SESSION. Any hints on what could be wrong ?
|
Yes it will !!
but in another file you have to call session_start(); meth0d again to access session variables
1 Comment
Cygnus
Please have a look at the discussion on Andrew Hall's answer. Any inputs would be great.