$_SESSION over two + pages

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nathj
    Recognized Expert Contributor
    • May 2007
    • 937

    $_SESSION over two + pages

    Hi,

    I am trying to get $_SESSION to work on my site. In order to learn this an dunderstand it better I have built two very simple test pages to see if i can access $_SESSION on both pages.

    Page 1
    [PHP]
    <?php /* Created on: 03/07/2007 */
    session_start() ;?>
    <html>
    <body>
    <?php
    $_SESSION['item1'] = "First session variable";
    echo $_SESSION['item1'];
    echo '<br /> <a href="session2. php"> click here </a>';

    ?>
    </body>
    </html>
    [/PHP]

    This displays the $_SESSION['item1'] correctly and the link takes you to the second page.

    Page 2
    [PHP]
    <?php /* Created on: 03/07/2007 */
    session_start() ;?>
    <html>
    <body>
    <?php
    echo 'My first variable: ' . $_SESSION['item1'];
    ?>
    </body>
    </html>
    [/PHP]

    This however, only displays the basic text and nothing comes out from the $_SESSION['item1'].

    I am really ew to this idea and I need to understand this is order to get the site I am developing to be properly interactive.

    Can anybody see what am missing with this simple example?

    Cheers
    nathj
  • kovik
    Recognized Expert Top Contributor
    • Jun 2007
    • 1044

    #2
    You're not missing anything. It sounds like a problem with a local installation. Is this a local test?

    Comment

    • nathj
      Recognized Expert Contributor
      • May 2007
      • 937

      #3
      Originally posted by volectricity
      You're not missing anything. It sounds like a problem with a local installation. Is this a local test?
      volectricity,
      Yep, it's a local test. I have checked my browsers and cookies are enabled, though I was led to beleive that $_SESSION was completely server side and so did not need cookies. What else could it be? Is there something in the ini file that I need?

      Cheers
      nathj

      Comment

      • kovik
        Recognized Expert Top Contributor
        • Jun 2007
        • 1044

        #4
        Well, it'd actually be more a filesystem issue.

        Here, put this on both pages and tell me if you get the same result:

        [code=php]echo session_id();[/code]

        Comment

        • phpmike
          New Member
          • May 2007
          • 5

          #5
          Originally posted by nathj
          Hi,

          I am trying to get $_SESSION to work on my site. In order to learn this an dunderstand it better I have built two very simple test pages to see if i can access $_SESSION on both pages.

          Page 1
          [PHP]
          <?php /* Created on: 03/07/2007 */
          session_start() ;?>
          <html>
          <body>
          <?php
          $_SESSION['item1'] = "First session variable";
          echo $_SESSION['item1'];
          echo '<br /> <a href="session2. php"> click here </a>';

          ?>
          </body>
          </html>
          [/PHP]

          This displays the $_SESSION['item1'] correctly and the link takes you to the second page.

          Page 2
          [PHP]
          <?php /* Created on: 03/07/2007 */
          session_start() ;?>
          <html>
          <body>
          <?php
          echo 'My first variable: ' . $_SESSION['item1'];
          ?>
          </body>
          </html>
          [/PHP]

          This however, only displays the basic text and nothing comes out from the $_SESSION['item1'].

          I am really ew to this idea and I need to understand this is order to get the site I am developing to be properly interactive.

          Can anybody see what am missing with this simple example?

          Cheers
          nathj

          Just a shot in the dark. It happened to me once.

          If you first page lets say you call it session.php is called from http://example.com/session.php
          and your second page is
          http://www.example.com/session2.php

          The session is not carried over. see the www and the non www. Make sure both pages are www or both non www.

          Comment

          • kovik
            Recognized Expert Top Contributor
            • Jun 2007
            • 1044

            #6
            Originally posted by phpmike
            The session is not carried over. see the www and the non www. Make sure both pages are www or both non www.
            That sounds like it could cause problems. Actually, it's more of issue of the fact that you have the http:// included when you do it.

            However, in this post, the sample code uses a relative link.

            Comment

            • ak1dnar
              Recognized Expert Top Contributor
              • Jan 2007
              • 1584

              #7
              Hi,
              Have you tried to run ,

              [PHP]phpinfo();[/PHP]
              and double check your settings.

              Comment

              • nathj
                Recognized Expert Contributor
                • May 2007
                • 937

                #8
                Originally posted by ajaxrand
                Hi,
                Have you tried to run ,

                [PHP]phpinfo();[/PHP]
                and double check your settings.
                I have looked at the phpinfo() results and can see that sessions are enabled and everything there looks like the sample you gave, except hash_bits_per_c haracter = 5 on my installation, I don't know if that has anything to do with it?

                Cheers
                nathj

                Comment

                • nathj
                  Recognized Expert Contributor
                  • May 2007
                  • 937

                  #9
                  Originally posted by volectricity
                  Well, it'd actually be more a filesystem issue.

                  Here, put this on both pages and tell me if you get the same result:

                  [code=php]echo session_id();[/code]
                  The session id is the same on both pages.

                  As for the www I am building this at present on localhost. I have tried using complete and relative paths and this does not affect the current behaviour. I will however, bear that in mind when I come to the production stage of the project.

                  nathj

                  Comment

                  • Atli
                    Recognized Expert Expert
                    • Nov 2006
                    • 5062

                    #10
                    If your server is running on Windows, you must set the 'session.save_p ath' in your php.ini to a place Apache has permission to use.
                    This is the folder PHP will use to store the session data, and if it is not set it will not be able to save the data, which causes it to regenerate the session_id every time you call the session_start() method.

                    Comment

                    • nathj
                      Recognized Expert Contributor
                      • May 2007
                      • 937

                      #11
                      Originally posted by Atli
                      If your server is running on Windows, you must set the 'session.save_p ath' in your php.ini to a place Apache has permission to use.
                      This is the folder PHP will use to store the session data, and if it is not set it will not be able to save the data, which causes it to regenerate the session_id every time you call the session_start() method.
                      Some technical information then:
                      OS: Windows Vista Business (sorry it's not my fault)
                      Webserver: IIS 7.0
                      PHP Version: 5.2.3
                      Session: Enable and the save_path = "C:\Users\user\ AppData\Local\T emp\php\session " which I do have full permission on.

                      Interestingly I am also having trouble with php_GD2.dll but one thing at a time I guess.

                      Thanks for all the help so far, I'm sure we'll get this thing sorted soon.

                      Cheers
                      nathj

                      Comment

                      • kovik
                        Recognized Expert Top Contributor
                        • Jun 2007
                        • 1044

                        #12
                        Well, check your session_save_pa th() folder to see if the session files are actually there. If not, you don't have valid permissions.

                        Comment

                        • Atli
                          Recognized Expert Expert
                          • Nov 2006
                          • 5062

                          #13
                          Originally posted by nathj
                          Session: Enable and the save_path = "C:\Users\user\ AppData\Local\T emp\php\session " which I do have full permission on.
                          I would recommend moving this out of the Temp thing, to a place like, for example "C:\Inetpub\php sessions\".
                          It doesn't really matter whether you have access, Apache must have access to it.
                          Try giving users like 'System' and 'Local Service' access to it, see if that works. You can also try giving everybody access, just to see if this is really the problem.

                          Comment

                          • Purple
                            Recognized Expert Contributor
                            • May 2007
                            • 404

                            #14
                            Hi Nathj

                            Session: Enable and the save_path = "C:\Users\user\ AppData\Local\T emp\php\session " which I do have full permission on.
                            can you see session records being stored within this location ?

                            I am running the same config on my development laptop and had more than a few issues with the new security settings in Vista..

                            Purple

                            Comment

                            • nathj
                              Recognized Expert Contributor
                              • May 2007
                              • 937

                              #15
                              debug info

                              Hi Everyone,

                              First of all thanks for all your help so far. I will now attempt to answer all the questions you have asked:

                              1) (volectricity) Is there anything in the path specified - No, there's not. It is completely empty.

                              2) (Atli) Apache having access and moving it to another location: I tried moving it to a different location and changed the variable in the ini file but that didnt' make any difference. Also I do not have Apache installed anywhere, I am using IIS 7 as the webserver

                              3) (Purple) Session records: I'm not entirely sure what I would be looking for here but the path specified is empty. What security issues did you encouter with Vista? I am so fed up with Vista that I am happy to blame it for anything and everything.

                              Also I thought I should post the debug information as this may help some more:

                              Warning: Unknown: open(C:\Users\u ser\AppData\Loc al\Temp\php\ses sion\sess_diuuc 9l68mjcpr0tfpla phb2t3, O_RDWR) failed: Permission denied (13) in Unknown on line 0

                              Warning: Unknown: Failed to write session data (files). Please verify that the current setting of session.save_pa th is correct (C:\Users\user\ AppData\Local\T emp\php\session ) in Unknown on line 0


                              I think I have aswered the questions asked. Thanks for the help so far I really appreciate it.
                              nathj
                              Last edited by nathj; Jul 5 '07, 06:49 AM. Reason: Adding information

                              Comment

                              Working...