Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

php session_id()

  • 29-02-2012 08:55PM
    #1
    Registered Users, Registered Users 2 Posts: 42


    hello all, I have a 4 page form that I'm trying to save the form fields till the last page till I post it all to the database. I was thinking that $_SESSION would be the best way to go.

    Problem is that no matter how I do it, there is no info in this variable when I go to the next page. I checked the session_id() and it is different for each page that I go to. I'm using the <form id="form1" name="form1" method="post" action="page4.php"> at the beggining of the form and I finish with
    <tr>
    <td align="center"><input type="submit" value="Next Page" size="60" /></td>
    </tr>

    The php looks like for page 1
    </div>
    <?php
    session_start();
    $_SESSION = 1;
    echo "Pageviews = ". $_SESSION . '<br>';
    echo session_id().'<br>';
    $HTTP_SESSION_VARS = "hello world";
    echo 'The content of $HTTP_SESSION_VARS[\'sess_var\'] is '.$HTTP_SESSION_VARS. '<br>';
    $_SESSION = $_POST;
    echo $_SESSION;
    echo '<p>this is the end of php</p>';
    ?>
    </html>
    ************************************
    and page 2 looks like
    </body>
    <?php
    session_start();
    echo "Pageviews = ". $_SESSION . '<br>';
    echo session_id().'<br>';
    echo $_SESSION.'<br>';
    echo 'The content of $HTTP_SESSION_VARS[\'sess_var\'] is '.$HTTP_SESSION_VARS. '<br>';

    echo "Pageviews = ". $_SESSION.'<br>';
    echo $_SESSION.'<br>';
    echo '<p>this is the end of php</p>';
    ?>
    </html>
    *************************************************
    the results I get are for the first page
    Pageviews = 1
    a098a52a745dd51835618fd2d6d0d0b4
    The content of $HTTP_SESSION_VARS is hello world
    jack
    this is the end of php
    ***********************************
    for the second page
    Pageviews =


    The content of $HTTP_SESSION_VARS is
    Pageviews =


    this is the end of php


Comments

  • Registered Users, Registered Users 2 Posts: 42 christg


    I moved my <a href> after my php and I get the session id put into the url, which I don't like, and it seems to work. But if my <a href> is before my php code I get different session id's
    ********************************
    test1.php
    ********************************
    <html>
    <head>
    </head>
    <body>
    </body>
    <?php
    session_start();
    $_SESSION = 'chris';
    echo $_SESSION.'<br>';
    echo session_id().'<br>';
    ?>
    <a href='test2.php'>Next Page</a>
    </html>
    *****************************************
    test2.php
    *****************************************
    <html>
    <head>
    </head>
    <body>
    </body>
    <?php
    session_start();
    echo $_SESSION.'<br>';
    echo session_id().'<br>';
    ?>
    <a href='test3.php'>Next Page</a>
    </html>
    **************************************
    test3.php
    **************************************
    <html>
    <head>
    </head>
    <body>
    </body>
    <?php
    session_start();
    echo $_SESSION.'<br>';
    echo session_id().'<br>';
    ?>
    </html>
    ***********************************
    gives my the following results:
    http://www.cgreaney.com/school/test1.php
    ***********************************
    chris
    fe1ab197c9fde266b690a3e9cc657798
    Next Page
    ************************************
    http://www.cgreaney.com/school/test2.php?PHPSESSID=fe1ab197c9fde266b690a3e9cc657798
    *************************************
    chris
    fe1ab197c9fde266b690a3e9cc657798
    Next Page
    *************************************
    http://www.cgreaney.com/school/test3.php?PHPSESSID=fe1ab197c9fde266b690a3e9cc657798
    *************************************
    chris
    fe1ab197c9fde266b690a3e9cc657798
    *************************************
    so the results all look good, but I don't like the url having the session id in it.
    If I move my <a href> like:
    <html>
    <head>
    </head>
    <body>
    <a href='test2.php'>Next Page</a>
    </body>
    <?php
    session_start();
    $_SESSION = 'chris';
    echo $_SESSION.'<br>';
    echo session_id().'<br>';
    ?>
    </html>
    I get the following output:
    Next Page chris
    b5fbc524d4a8475f8bd609c0d25c1ab2
    ***********************************
    Next Page
    47c1a4acc4ffea036a9712ad80890a90
    ***********************************
    The url stays with just the page name, but I loose my $_SESSION variable and the session ID changes.


  • Closed Accounts Posts: 18,163 ✭✭✭✭Liam Byrne


    session_start should be before any output; in fact, if you didn't have errors & warning suppressed, it's probably throwing an error.


  • Registered Users, Registered Users 2 Posts: 42 christg


    By any output, are you meaning the html parts ? since I have the session_start() right after <?php.

    I was thinking that maybe I should move the php section to the top of the page before the html.

    Thanks for the timely reply !


  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    christg wrote: »
    By any output, are you meaning the html parts ? since I have the session_start() right after <?php.

    I was thinking that maybe I should move the php section to the top of the page before the html.

    Thanks for the timely reply !
    yes, since the session data is included in the http header, it's too late to include it in the header if HTML (in your case) is already outputted.

    Then there's ob_start if you want to cache the http content while sending the headers


  • Registered Users, Registered Users 2 Posts: 42 christg


    Thanks all, it makes sense now. I didn't see any warning on this in all of the tutorials I looked at.

    Does it make sense to use the session variables to store form information until the last page of the form where I input it into a database ?

    I thought I might be able to do it with the form action posting it to the last page.


  • Advertisement
Advertisement