Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

PHP and sessions question

  1. Feb 28, 2009 #1

    Math Is Hard

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    I'm a little confused about how PHP's session_start() function works.
    There's a tutorial I am reading here: http://www.tizag.com/phpT/phpsessions.php

    Here's an example they give of a counter script:

    Code (Text):
    <?php
    session_start();  
    if(isset($_SESSION['views']))
        $_SESSION['views'] = $_SESSION['views']+ 1;
    else
        $_SESSION['views'] = 1;

    echo "views = ". $_SESSION['views'];
    ?>
     
    It seems like every time I call this PHP script from a new page, a new session will start, because the first line is:

    session_start();

    Does PHP ignore this function if a session exists?

    Thanks.
     
  2. jcsd
  3. Mar 1, 2009 #2

    CompuChip

    User Avatar
    Science Advisor
    Homework Helper

    You have to call session_start() at each page. If no session is started yet, this will start a new session (i.e. write a cookie with the session ID). I believe that if you already have a session running, this will load the variables into $_SESSION so you can access them.

    So basically, session_start() is needed if you want to stay in the same session, if you don't call it you will get a new session all the time (or your variables just won't get stored, which is effectively the same).

    In short: I Don't know how it works exactly, just use it :smile:
     
  4. Mar 1, 2009 #3

    Math Is Hard

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Thanks, CompuChip! :smile:
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: PHP and sessions question
  1. PHP Help ? (Replies: 16)

  2. Simple PHP question (Replies: 6)

  3. PHP code question (Replies: 8)

  4. Php or c++ ? (Replies: 40)

Loading...