As the subject says, if your website uses sessions to keep track of a user session,

(1) does the code session_start(); need to be on every page of the website?

(2) I hear about having no white spaces before <?php session_start(); ?>. So this should then go before the opening <html> tag?

(3) How do you tell if a session is already started? Cause I assume that I would not want to call session_start(); on a page that already had a user session running.

Thanks.
Jonathan

    1. If you want to use sessions you have to use session_start() on every page that uses them.

    2. Mostly the session id is stored as a cookie so information has to be read / written with the page header which must happen before any output, so yes, as good practise call session start as the first thing, but as you'll see in a second, output buffering can save you in a struggle (but I don't recommend it - just code well in the first place).

    3 To know if you've already called session_start() then check if the $_SESSION superglobal exists.

    <?php
    ob_start();
    
    if (isset($_SESSION))
    {
    	echo 'Session exists<br />';
    }
    else
    {
    	echo 'NO Session exists<br />';
    }
    
    session_start();
    
    
    if (isset($_SESSION))
    {
    	echo 'Session exists<br />';
    }
    else
    {
    	echo 'NO Session exists<br />';
    }
    ?>

    As you can see here I've done output before starting the session, but ob_start bufferrs the output. Comment out the line and you'll see the badness.

    Use $_SESSION['something'] = $value; to store values, don't go messing with that session register tripe - it's deprecated old cack that went out ages ago but still exists in old tutorials that sites are too lazy to delete.

      Drakla wrote:

      1. If you want to use sessions you have to use session_start() on every page that uses them.

      So would I just want to put the session code on every page then. What happens if a user goes to the homepage, a session is started, then fills out some form and the form data is saved in the session array, and then the user clicks to other pages within the same site and those pages do not have the session coding on them. Will the data in the session array be lost? Or will it still be there and available even though the user went to a page that did nto have the session_start() coding on it?

      Also, assuming that I put the session_start() on every page (don't see why this hurts), just want to confirm if I can keep it simple and code with the following at the VERY TOP of the page, even before the opening <html> tag:

      <?php
      session_start();
      ?>

      Thanks.

        Will the data in the session array be lost?

        No... not unless the client closes the broswer, or the session times out.

        And yes... putting session_start() at the very top of all pages is acceptable. session_start() itself will check if a session already exists and if not, create one.

          Write a Reply...