...

View Full Version : Session not persisting between pages



codyodell
10-24-2009, 08:57 PM
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:


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.

CFMaBiSmAd
10-24-2009, 09:07 PM
/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()

codyodell
10-24-2009, 09:19 PM
Oops, I just checked phpinfo() and register globals is already turned off :eek: , thanks for the heads up.

however, I ran:


print_r(session_get_cookie_params());


on both pages, and they returned:


Array
(
[lifetime] => 0
[path] => /
[domain] =>
[secure] =>
[httponly] =>
)

CFMaBiSmAd
10-24-2009, 09:22 PM
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.

codyodell
10-24-2009, 09:31 PM
The redirect url is relative, and I put:



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:
http://codyodell.com/images/Screen%20shot%202009-10-24%20at%202.38.35%20PM.png

codyodell
10-24-2009, 10:12 PM
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.

kbluhm
10-24-2009, 11:18 PM
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:


ini_set( 'session.cookie_domain', 'example.com' );

This will allow the session to persist only within example.com

Or you could prefix it with `.`


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().

codyodell
10-24-2009, 11:55 PM
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.

codyodell
10-26-2009, 04:03 PM
No other ideas?

CFMaBiSmAd
10-26-2009, 04:15 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum