Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Feb 2006
    Posts
    18
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Exclamation Session not persisting between pages

    First off, I'm using:

    MAMP
    PHP Version 5.2.11
    Apache/2.0.63 (Unix)
    MySQL 5.1.37

    I have register globals turned off via ini_set("REGISTER_GLOBALS", "0");, and session_start() at the beginning of my page. Both pages are chmod to 755.

    I have a signup form (/signup.php), which posts to /process/signup.php. When the script finds an error, I save the post data to a session var ($_SESSION['OLD_POST']), and the errors to $_SESSION['errs'] and redirect to the signup form via header(). But when I run:

    Code:
    print_r($_SESSION);
    on the signup form I get an empty array.

    The weird thing is, if I run $_SESSION['some_var'] = 'foo'; on the process page, I get nothing on the form page. But when I set the session var on the form page, the variable does persist through the redirect, and is set on both pages.

    I've tried serializing the values, it made no difference.

    I've been debugging this for quite a while, so any help would be GREATLY appreciated, for the sake of meeting an important deadline.

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,146
    Thanks
    2
    Thanked 333 Times in 325 Posts
    /signup.php, which posts to /process/signup.php
    You are changing the path between the two pages. You need to set the session.cookie_path to '/' (which is the default on servers using the recommended php.ini settings) so that the browser will send the session cookie for all paths on your site, not just the one where it was set at.


    Just an FYI, you cannot change register_globals using ini_set()
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • Users who have thanked CFMaBiSmAd for this post:

    codyodell (10-24-2009)

  • #3
    New Coder
    Join Date
    Feb 2006
    Posts
    18
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Oops, I just checked phpinfo() and register globals is already turned off , thanks for the heads up.

    however, I ran:
    Code:
    print_r(session_get_cookie_params());
    on both pages, and they returned:
    Code:
    Array
    (
        [lifetime] => 0
        [path] => /
        [domain] => 
        [secure] => 
        [httponly] => 
    )

  • #4
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,146
    Thanks
    2
    Thanked 333 Times in 325 Posts
    Then you are probably redirecting between URL's with www. and without www. on them and the session.cookie_domain is not set up to match both.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #5
    New Coder
    Join Date
    Feb 2006
    Posts
    18
    Thanks
    2
    Thanked 2 Times in 2 Posts
    The redirect url is relative, and I put:

    Code:
    ini_set('session.cookie_domain', 'localhost:8888');
    at the beginning of both pages, still nothing.

    BTW, my session ID is the same on both pages.

    Here is a screenshot of my session settings:
    Last edited by codyodell; 10-24-2009 at 09:41 PM.

  • #6
    New Coder
    Join Date
    Feb 2006
    Posts
    18
    Thanks
    2
    Thanked 2 Times in 2 Posts
    This may help narrow things down.

    There is a working form using the exact same functionality, except the form is in a sub-directory and not in the root.

  • #7
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    The cookie domain can only be a hostname, with or without a `.` prefix. But if you're using `localhost`, then that can't be the problem

    On a development or production server with a valid second-level-or-higher domain, you would use:
    PHP Code:
    ini_set'session.cookie_domain''example.com' ); 
    This will allow the session to persist only within example.com

    Or you could prefix it with `.`
    PHP Code:
    ini_set'session.cookie_domain''.example.com' ); 
    This will allow the session to persist between example.com, www.example.com, and any other sub domains of example.com

    This must also be set prior to session_start().

  • #8
    New Coder
    Join Date
    Feb 2006
    Posts
    18
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Again, this same exact method is being used, and is working. The only difference is that one file is in a folder (the one that is working), and one file is not (the one that is not working).

    Let me clarify that a little more:

    Form 1 (signup, not working)
    /signup.php posts to /process/signup.php

    Form 2 (add a meeting, works perfectly)
    /backoffice/add_meeting.php posts to /process/add_meeting.php

    Thanks for the info, but I don't think it is a problem with settings or they would both not work. But hey, I could be wrong.

  • #9
    New Coder
    Join Date
    Feb 2006
    Posts
    18
    Thanks
    2
    Thanked 2 Times in 2 Posts
    No other ideas?

  • #10
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,146
    Thanks
    2
    Thanked 333 Times in 325 Posts
    BTW, my session ID is the same on both pages.
    Then it is likely you have an error in your logic and the variables are either not being set or they are being overwritten by your logic. Without seeing the actual code responsible for the symptoms, it is not really possible to determine what it might be doing.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •