Jump to content

question about session_start function


saeed_violinist

Recommended Posts

Hi,

 

I designed my site in a way that the template is loaded from a php file that included in every page of my site. Now I want to know if I put SESSION_START() function in this template file so I can access the session variables in every page, when every page is accessed from theme.php a new sesion_start will be generated.

 

So, this session starts in every page , will this cause session variables in last page to be deleted? and If your answer is YES, what you suggest instead of putting session start in main template code.

Link to comment
Share on other sites

session_start always checks whether there was a valid session id used previously. The session id is normally stored in a cookie. This id is relates to a session file stored on the server. PHP then compares those two to see if the session is still valid, eg the session has not timed out or whether the session file exists.

 

You can call session_start as many times as you like, eg:

<?php
session_start();

$_SESSION['blah'] = 'hello';

session_start();

The second call to start the session will just get ignored. PHP will not reset any of the session variables unless you have told it to kill the session using either session_destroy or unset. Or whether the session has been timed out or when you delete your cookies on your computer/browser.

Link to comment
Share on other sites

I know what you mean. A code snippet I have always used to not worry about sessions this way is to do this:

 

if(session_id() == "")

  {

    session_start();

  }

 

before the start of any page where there is a need for session info. That way I knda have both ends of the bargain neatly covered.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.