Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

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.

like image 748
Foo Avatar asked Mar 19 '10 05:03

Foo


1 Answers

Unbelievable!

After three hours of debugging, I've figured out the problem

HostGator, my no-longer-beloved host, runs their default ini with register_globals ON

I can't believe this.

So my variables of $type was being overwritten with $_SESSION['type'] and they were being treated as the same. So it was a server issue causing the problem, not my coding. That's great to know, but I want my 5 hours of pulling my hair out back.

I hope this helps someone down the road whose using HostGator and is confused as hell.

like image 161
Foo Avatar answered Sep 29 '22 07:09

Foo