how to pass session id between MULTIPLE pages

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • malcolmk
    New Member
    • Sep 2010
    • 79

    #1

    how to pass session id between MULTIPLE pages

    Hi, been scratching my head and looking all over but cant get session data to pass to multiple pages.
    The scenario is like this.
    I present a splash page with an option to login at the bottom via form. Across the top is a menu strip, site access is open to all but if logged in and validatd as a member then selecting "downloads" or "articles" from the menu strip gives access to different content. I need my user to only login / validate once on the splash screen and then hold the session data (string stating member or nonmember retrieved from a db) to be accessable from any page within the site.
    my test pages are as follows.

    login screen
    ---------------
    Code:
    <html>
    <head><title>Title here!</title></head>
    <body>
    <form action = "user.php"  method="post">
    <input type="text" name="status">
    <input type="submit" >
    </form>
    <a href="download.php">Downloads</a>
    </body>
    </html>
    On submit I call my second page which in reality would check username against database and assign either "member" or "nonmember" to session_id
    Code:
    <html>
    <head><title>Title here!</title></head>
    <body>
    <?php
    session_start();
    $x= $_POST['status'];
    session_register("status");
    $status=$x;
    // check session id should be text entered on login screen for test purposes
    echo 'session id is '.$status;
    // re display login screen 
    include('base.htm');
    ?>
    </body>
    </html>
    Now I may navigate around the site a little but if I choose "downloads" I need to be able to read the session_id member or nonmember ?
    Code:
    <html>
    <head><title>Title here!</title></head>
    <body>
    <?php
    session_start();
    global $status;
    echo 'session id is '.$status;
    ?>
    </body>
    </html>
    Problem is that going to "download.p hp" produces no session_id ?
    any help greatly appreciated.
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Turn on Error Reporting*, and see this article.

    * Use -1 instead of E_ALL.

    Comment

    • malcolmk
      New Member
      • Sep 2010
      • 79

      #3
      no joy?

      Thanks for the swift reply but; I can't understand what is going wrong.
      I modified the base.htm by placing
      Code:
      <?php
      session_start();
      session_id('nonmember');
      echo session_id);
      ?>
      at the very top of the doc.
      This shows me that session_id has indeed been set to "non member"
      When submitting the form I get the session_id changed to whatever I submitted but when clicking download link on base.htm when download.php appears it has a new session_id.
      I really dont understand, there must be a simple solution? But I can't see it, help please.
      Last edited by malcolmk; Sep 3 '10, 02:27 PM. Reason: typos

      Comment

      • kovik
        Recognized Expert Top Contributor
        • Jun 2007
        • 1044

        #4
        What, pray tell, is the purpose of allowing your users to select their own session IDs? So that it's easier for them to attempt to hijack another session?

        Comment

        • malcolmk
          New Member
          • Sep 2010
          • 79

          #5
          session id's

          In reality it does not matter what the session id is, as I stated in my original post certain parts of the site have content available only to members. For example you access the articles page, all articles are listed but if you are not a member only some are active links, if you are a member all articles have active links. I require the viewer to log in or not on the splash screen so that as each new page is accessed content can be displayed as to if you are a member or not.
          Logging in with your user name / password calls a script to check against a db of members and sets a session id accordingly. It may be that a result of non member sets session_id to "non" whilst a result of "member" sets session_id to a random string (as is I think the default).
          The above code is just my test or scratch code to get the principles working and give me known results easier for checking.

          Comment

          • malcolmk
            New Member
            • Sep 2010
            • 79

            #6
            Sorted

            Well I got it sorted by using simplified code and by making sure that $_SESSION was in upper case.
            test coding is now as follows.

            log in / out / downloads
            Code:
            <html>
            <head>
                   <title>Title here!</title>
            </head>
            <body >
            <form action = "user.php"  method="post">
            <input type="text" name="status" value="">
            <input type="submit" >
            </form>
            <a href="download.php">Downloads</a>
            <a href="kill.php">logout</a>
            </body>
            </html>
            assign session id (remember in real we don't use the entered name but check against db, if member then generate session id as normal, if not a member set session id to "non member".
            Code:
            <?php
            session_start();
            error_reporting(-1);
            ini_set('display_errors', true);
            $x= $_POST['status'];
            if (!isset($_SESSION["user"]))
            {
            $_SESSION["user"]=$x;
            echo 'session id is '.$_SESSION["user"];}
            else {echo 'session id is '.$_SESSION["user"];}
            include('base.htm');
            ?>
            download page has carried the session id.
            In real if session id is "non member" display content x if session id anything else then must be member so display content y.
            Code:
            <?php
            session_start();
            error_reporting(-1);
            ini_set('display_errors', true);
            echo 'session id is '.$_SESSION["user"];
            ?>
            to log out or kill the session then
            Code:
            <?php
            session_start();
            session_destroy();
            include("base.htm");
            ?>
            Hope this helps someone else, there seems to be a lot of questions about passing session id's on the net and I found out myself it's not all plain sailing.

            Comment

            • kovik
              Recognized Expert Top Contributor
              • Jun 2007
              • 1044

              #7
              What you are doing has nothing to do with session IDs. The session ID is a randomly-generated unique string that identifies an individual session. What you are doing is setting session data. The ID stays that same.

              As a side note, it's good practice to call session_regener ate_id() whenever a user's permissions change (i.e. when they log in and become a member instead of a guest).

              Comment

              • malcolmk
                New Member
                • Sep 2010
                • 79

                #8
                Well thanks for that, I guess I got my terminologu wrong with session id is $_SESSION["user"] but I tried first off doing it by trying to transfer session_id() between pages and could not get it working so I ended up with this session variable instead($_SESSI ON["user"]).
                Can you tell me why it is good practice to regen the session_id() if I am not actually using that value? are there advantages / disadvantages?
                I would like to use good programming practice from the start of my induction into php.
                Thanks for your input.
                Last edited by malcolmk; Sep 4 '10, 02:28 PM. Reason: typo

                Comment

                • malcolmk
                  New Member
                  • Sep 2010
                  • 79

                  #9
                  Just read explanation on wiki session fixation attack, I now will use session_regener ate_id() as a matter of course. Good article easy to understand. ;)

                  Comment

                  • kovik
                    Recognized Expert Top Contributor
                    • Jun 2007
                    • 1044

                    #10
                    By the way, you are passing the session ID, you just don't realize it. It is set in a cookie on their end.

                    A session instance is like a locked apartment, and the session ID is the key. Their cookie contains this session ID which allows them to open the locked door and get inside.

                    Comment

                    • malcolmk
                      New Member
                      • Sep 2010
                      • 79

                      #11
                      So is there a more correct way to do this? To set and retain a value throughout a session for a given user?

                      Comment

                      • kovik
                        Recognized Expert Top Contributor
                        • Jun 2007
                        • 1044

                        #12
                        Nope, you are doing it correctly. The $_SESSION superglobal array is our method of updating session information.

                        Comment

                        • malcolmk
                          New Member
                          • Sep 2010
                          • 79

                          #13
                          Thanks, guidance is much appreciated.

                          Comment

                          • kovik
                            Recognized Expert Top Contributor
                            • Jun 2007
                            • 1044

                            #14
                            It's what we're here for. :)

                            Comment

                            Working...