laef laef - 1 year ago 64
PHP Question

Are PHP Sessions Associated with Particular Files

Is PHP session data associated with a particular web page or PHP file? In other words, if a session is created in foo.php, would it's session data be accessible from bar.php, assuming that neither reference the other and both are accessed directly from the web browser.

If it is file specific, how does this work with

s? For example, if
creates a session, then includes
, can the code in
access session data? How about if the session is created in
? Would the data then be specific to

Answer Source

Yes, session data is available across different files and requests, that is the whole point of sessions: to provide state where the HTTP protocol doesn't. You could look upon the session as a small storage on the web server for your code to store limited data in.

If your foo.php starts a session and then includes bar.php, the code in bar.php will indeed be able to access the session. If your bar.php executes the session_start() then the foo.php can access the session from then on (so after the include()).

Please do remember: a session is unique to a specific browser and volatile. I.e: close the browser and the session will become inactive, inaccessible and expire (usually after 30 minutes).

Note:- modern browsers have the ability to reconnect with session.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download