1

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 ?

1
  • 1
    That's what sessions (over multiple requests) are for :-) Commented Jun 7, 2012 at 11:01

2 Answers 2

6

Yes it should be as long as you call session_start() in each page

http://php.net/manual/en/function.session-start.php

Sign up to request clarification or add additional context in comments.

8 Comments

In each page meaning in each PHP file ?
@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.
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.
@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.
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 ?
|
1

Yes it will !!
but in another file you have to call session_start(); meth0d again to access session variables

1 Comment

Please have a look at the discussion on Andrew Hall's answer. Any inputs would be great.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.