PHP session variable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • twinklyblue
    New Member
    • Jun 2007
    • 37

    PHP session variable

    Hi team,

    I am just a beginner programmer on PHP and I was wondering if anybody can help me with this problem.

    I am trying to set-up the PHP session variable but I wasnt able to print the said session variable in the next page. Here's an excerpt from the origin page. This was placed in the start of the PHP file:

    <?php
    session_start() ;
    $_SESSION['session_var'] = "testing";
    ?>

    Then I was trying to print out the session variable in the second PHP page but nothing displayed:
    <?php
    session_start() ;
    $session_var = $_SESSION['session_var'];
    echo "session_va r =".$session_var ;
    ?>

    I am using PHP 5.2.2 and apache 2.2.4 in winXP. More FYI, I am running on Japanese OS (If this info can help). Thank you for all responses.
  • ak1dnar
    Recognized Expert Top Contributor
    • Jan 2007
    • 1584

    #2
    Is there any link from the first page to second page.

    [PHP]<?php
    session_start() ;
    $_SESSION['session_var'] = "testing";
    print('<a href="second_pa ge.php">second_ page.php</a>');
    ?>
    [/PHP]

    How did you call for the second page after executing first.

    Comment

    • twinklyblue
      New Member
      • Jun 2007
      • 37

      #3
      I have a form going from page1 to page2. Here's the code of the first page:

      <?php
      session_start() ;
      $_SESSION['session_var'] = "testing";
      session_registe r($_SESSION['session_var'] );
      ?>
      <html>
      <body>
      <p>This is a test of the sessions feature
      <form action="session Test2.php" method = "POST">
      <input type="text" name="form_var" value ="testing">
      <input type="submit" value="Go to the next page">
      </body>
      </html>

      Thanks for the reply :)

      Comment

      • twinklyblue
        New Member
        • Jun 2007
        • 37

        #4
        And here's the code for the second page (might as well put it) :)


        <?php
        session_start() ;
        ?>
        <html>
        <body>
        <?php
        $session_var = $_REQUEST['session_var'];
        echo "session_va r =".$session_var ."<br>\n";
        ?>
        </body>
        </html>

        Comment

        • ak1dnar
          Recognized Expert Top Contributor
          • Jan 2007
          • 1584

          #5
          Originally posted by twinklyblue
          And here's the code for the second page (might as well put it) :)


          <?php
          session_start() ;
          ?>
          <html>
          <body>
          <?php
          $session_var = $_REQUEST['session_var'];
          echo "session_va r =".$session_var ."<br>\n";
          ?>
          </body>
          </html>
          It seems like the probem is with your php configuration. In my end the script is working perfectly.
          run a phpinfo() and find out the session is enabled or not.

          Comment

          • jx2
            New Member
            • Feb 2007
            • 228

            #6
            page 1
            u SHOULDNT use session_registe r(); if you use $_SESSION[]
            session_registe r register variable $_SESSION[] and $_SESSION[] trying to register itself.... bad idea:-)

            get rid the line: session_registe r($_SESSION['session_var']);

            IT SHOULD WORK FINE
            i tried it :-)
            regards
            jx2

            Comment

            • twinklyblue
              New Member
              • Jun 2007
              • 37

              #7
              Originally posted by ajaxrand
              It seems like the probem is with your php configuration. In my end the script is working perfectly.
              run a phpinfo() and find out the session is enabled or not.
              Hi ajaxrand,
              Tried to check it.. but the set-up is okay. Here's what I have in my phpinfo.php:(

              session
              Session Support enabled
              Registered save handlers files user
              Registered serializer handlers php php_binary wddx

              Comment

              • twinklyblue
                New Member
                • Jun 2007
                • 37

                #8
                Originally posted by jx2
                page 1
                u SHOULDNT use session_registe r(); if you use $_SESSION[]
                session_registe r register variable $_SESSION[] and $_SESSION[] trying to register itself.... bad idea:-)

                get rid the line: session_registe r($_SESSION['session_var']);

                IT SHOULD WORK FINE
                i tried it :-)
                regards
                jx2
                Hi jx2,

                I also did what you suggested and it still didnt work. Apparently, the one that I pasted was the old code..sorry..

                Comment

                • jx2
                  New Member
                  • Feb 2007
                  • 228

                  #9
                  well
                  after i did "my"corecti on to your first file i tried it and it did worked :-)

                  what i do in a case like that i print all variables to see values - u will see when your $session_var get into truble

                  try a lot of echo statments
                  eg:
                  echo $session_var;
                  and move it in first file line by line (after $_SESSION) TO SEE WHATS GOING ON

                  good luck

                  Comment

                  • jx2
                    New Member
                    • Feb 2007
                    • 228

                    #10
                    and make sure there is no white space before <?php
                    did you get any error?

                    Comment

                    • ak1dnar
                      Recognized Expert Top Contributor
                      • Jan 2007
                      • 1584

                      #11
                      Originally posted by twinklyblue
                      Hi ajaxrand,
                      Tried to check it.. but the set-up is okay. Here's what I have in my phpinfo.php:(

                      session
                      Session Support enabled
                      Registered save handlers files user
                      Registered serializer handlers php php_binary wddx
                      what about the value for session.auto_st art is it on or off.

                      Comment

                      • twinklyblue
                        New Member
                        • Jun 2007
                        • 37

                        #12
                        Originally posted by ajaxrand
                        what about the value for session.auto_st art is it on or off.
                        It is set to 0. I tried to set it to 1 but still it didnt work so I reset it to 0 again :(

                        Comment

                        • twinklyblue
                          New Member
                          • Jun 2007
                          • 37

                          #13
                          Originally posted by jx2
                          and make sure there is no white space before <?php
                          did you get any error?
                          HI jx2,

                          I tried what you suggested. Here's the code I inserted in start of the second php file.

                          <?php
                          session_start() ;
                          echo "test ".$session_ var;
                          ?>

                          I would be glad if there is an error but unfortunately there was nothing displayed. Any more suggestions? :(

                          Comment

                          • jx2
                            New Member
                            • Feb 2007
                            • 228

                            #14
                            Originally posted by twinklyblue
                            HI jx2,

                            I tried what you suggested. Here's the code I inserted in start of the second php file.

                            <?php
                            session_start() ;
                            echo "test ".$session_ var;
                            ?>

                            I would be glad if there is an error but unfortunately there was nothing displayed. Any more suggestions? :(
                            nothing at all? it should show at least "test" on its own
                            my guess iss that u doung simple mistake:-) but u need to realise what is it :-)
                            try to type it at the end of the first file and see if there is something in $session_var

                            regards
                            jx2

                            Comment

                            • ak1dnar
                              Recognized Expert Top Contributor
                              • Jan 2007
                              • 1584

                              #15
                              Why don't you post the complete scripts for both the pages here in the forum.

                              Comment

                              Working...