PHP Based session variable not retaining value. Works on localhost, but not on server.

Posted by Foo on Stack Overflow See other posts from Stack Overflow or by Foo
Published on 2010-03-19T05:12:44Z Indexed on 2010/03/19 5:21 UTC
Read the original article Hit count: 185

Filed under:
|

I've been trying to debug this problem for many hours, but to no avail. I've been using PHP for many years, and got back into it after long hiatus, so I'm still a bit rusty.

Anyways, my $_SESSION vars are not retaining their value for some reason that I can't figure out. The site worked on localhost perfectly, but uploading it to the server seemed to break it.

First thing I checked was the PHP.ini server settings. Everything seems fine. In fact, my login system is session based and it works perfectly. So now that I know $_SESSIONS are working properly and retaining their value for my login, I'm presuming the server is setup and the problem is in my script.

Here's a stripped version of the code that's causing a problem. $type, $order and $style are not being retained after they are set via a GET variable. The user clicks a link, which sets a variable via GET, and this variable is retained for the remainder of their session.

Is there some problem with my logic that I'm not seeing?

<?php
require_once('includes/top.php'); //first line includes a call to session_start();
require_once('includes/db.php');

$type = filter_input(INPUT_GET, 't', FILTER_VALIDATE_INT);
$order = filter_input(INPUT_GET, 'o', FILTER_VALIDATE_INT);
$style = filter_input(INPUT_GET, 's', FILTER_VALIDATE_INT);

/*
According to documentation, filter_input returns a NULL when variables
are undefined. So, if t, o, or s are not set via URL, $type, $order and $style
will be NULL. 
*/    

print_r($_SESSION);
/* 
All other sessions, such as the login session, etc. are displayed here. After 
the sessions are set below, they are displayed up here to... simply with no value. 
This leads me to believe the problem is with the code below, perhaps?
*/


// If $type is not null (meaning it WAS set via the get method above)
// or it's false because the validation failed for some reason,
// then set the session to the $type. I removed the false check for simplicity.
// This code is being successfully executed, and the data is being stored...
if(!is_null($type))
{
    $_SESSION['type'] = $type;
}
if(!is_null($order))
{
    $_SESSION['order'] = $order;
}
if(!is_null($style))
{
    $_SESSION['style'] = $style;
}


 $smarty->display($template);

?>

If anyone can point me in the right direction, I'd greatly appreciate it. Thanks.

© Stack Overflow or respective owner

Related posts about php

Related posts about php-session