NYCPHP Meetup

NYPHP.org

[nycphp-talk] simple problem with sessions

Chris Shiflett shiflett at php.net
Fri Oct 8 22:48:00 EDT 2004


--- Aaron Fischer <agfische at email.smith.edu> wrote:
> I'm having trouble getting sessions to work on a new server. At this 
> point I have tried to back my code down to the simplest use of sessions 
> that I can think of, but still to no avail. My hosting company hasn't 
> been much help to date.  I'm wondering if the following code should 
> work?  (The server is running PHP 4.3.6.)
> 
> first page:
> 
> session_start();
> $_SESSION['user'] = "Frank";
> header("Location: full_url.php");

That's an incorrect use of the Location header - it requires an absolute
URL, not a relative one. This misuse has been known to cause problems with
certain browsers where cookies are not included in the request for the new
URL. This might be your problem.

> second page:
> 
> session_start();
> echo $_SESSION['user'];
> 
> also later on in this page:
> <pre><? print_r($_SESSION); ?></pre>
> 
> The output produces nothing reflected from the echo statement and the 
> print_r produces an empty array:
> Array
> (
> )

When debugging these types of problems, it's usually best to assume that
PHP is not making an error. :-) How could it - PHP is perfect! Just
kidding, but it is the least likely source of the problem.

What is the problem then? Well, 9 times out of 10, it's that the browser
doesn't properly identify itself. The best way to determine this is to
dump both the $_GET and $_COOKIE superglobals. I have a feeling this will
reveal the problem, and it might be the improper use of the Location
header that is causing it.

Hope that helps.

Chris

=====
Chris Shiflett - http://shiflett.org/

PHP Security - O'Reilly     HTTP Developer's Handbook - Sams
Coming December 2004        http://httphandbook.org/



More information about the talk mailing list