Sessions are lost on page redirect

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

fakhrizaki
Forum Newbie
Posts: 9
Joined: Tue Oct 02, 2007 6:15 am

Sessions are lost on page redirect

Post by fakhrizaki »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi,

I have 2 php scripts. The first one sets a value in a session variable and the 2nd displays it. Page1 has the hyperlink for page2.

The code is tested and works fine on my local machine and also on my other server but it is not at all working on one server. I don't whats wrong with that server because I compared all php settings with my other servers and its all same for the sessions, but its not working.

Here is the code:

===================
Page1 :

Code: Select all

<?php
session_start();
$_SESSION['var']='my variable';
?>
<a href='page2.php'>Page 2</a>


Page 2:

Code: Select all

<?php
session_start();
echo $_SESSION['var'];
?>
=================

Please help me out as I couldn't figure what the actual cause of this problem. The same script is working everywhere else.

Regards,
Fakhri.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Run the following in a new file on each server and tell us the results please.

Code: Select all

<?php

$neg = array('off', 0, false, '', null);
$flags = array(
	'Register Globals' => 'register_globals',
	'Short Tags' => 'short_open_tag',
	'Display Errors' => 'display_errors',
	'Magic Quotes GPC' => 'magic_quotes_gpc',
	'Magic Quotes Runtime' => 'magic_quotes_runtime',
	'Magic Quotes Sybase' => 'magic_quotes_sybase',
);
$ve = phpversion();
$os = PHP_OS;
$er = intval(error_reporting());
foreach ($flags as $n => $v)
{
	$flags[$n] = (in_array(strtolower(ini_get($v)), $neg) ? 'Off' : 'On');
}
$flags['Config file'] = get_cfg_var('cfg_file_path');
if (empty($flags['Config file']))
{
	$flags['Config file'] = '-';
}
$cli = (php_sapi_name() == 'cli');
$eol = "\n";

$gle = get_loaded_extensions();
$rows = array();
$le = '';
$wide = 4;
$j = count($gle);
$pad = $wide - $j % $wide;
$len = max(array_map('strlen', $gle));
$func = create_function('$a', 'return str_pad($a, ' . intval($len) . ');');
$gle = array_map($func, $gle);
for($i = 0; $i < $j; $i += $wide)
{
	$le .= '   ' . implode('   ', array_slice($gle, $i, $wide)) . $eol;
}

$ec = array(
	'E_STRICT' => 2048, 'E_ALL' => 2047, 'E_USER_NOTICE' => 1024,
	'E_USER_WARNING' => 512, 'E_USER_ERROR' => 256, 'E_COMPILE_WARNING' => 128,
	'E_COMPILE_ERROR' => 64, 'E_CORE_WARNING' => 32, 'E_CORE_ERROR' => 16,
	'E_NOTICE' => 8, 'E_PARSE' => 4, 'E_WARNING' => 2, 'E_ERROR' => 1,
);

$e = array();
$t = $er;
foreach ($ec as $n => $v)
{
	if (($t & $v) == $v)
	{
		$e[] = $n;
		$t ^= $v;
	}
}
if (ceil(count($ec) / 2) + 1 < count($e))
{
	$e2 = array();
	foreach ($ec as $n => $v)
	{
		if (!in_array($n, $e) and $n != 'E_ALL')
		{
			$e2[] = $n;
		}
	}
	$er = $er . ' ((E_ALL | E_STRICT) ^ ' . implode(' ^ ', $e2) . '))';
}
else
{
	$er = $er . ' (' . implode(' | ', $e) . ')';
}

if (!$cli)
{
	echo '<html><head><title>quick info</title></head><body><pre>', $eol;
}

echo 'PHP Version: ', $ve, $eol;
echo 'PHP OS: ', $os, $eol;
echo 'Error Reporting: ', $er, $eol;
foreach ($flags as $n => $v)
{
	echo $n, ': ', $v, $eol;
}
echo 'Loaded Extensions:', $eol, $le, $eol;

if (!$cli)
{
	echo '</pre></body></html>', $eol;
}

?>
fakhrizaki
Forum Newbie
Posts: 9
Joined: Tue Oct 02, 2007 6:15 am

Post by fakhrizaki »

Result of the script on the server in which sessions are working fine

PHP Version: 5.2.2
PHP OS: Linux
Error Reporting: 0 ()
Register Globals: Off
Short Tags: On
Display Errors: On
Magic Quotes GPC: On
Magic Quotes Runtime: Off
Magic Quotes Sybase: Off
Config file: /usr/local/lib/php.ini
Loaded Extensions:
xmlwriter libxml dom xmlreader
xml tokenizer session pcre
SimpleXML SPL PDO sockets
SQLite standard Reflection posix
pdo_sqlite mysqli mysql json
iconv hash gettext gd
ftp filter date ctype
calendar bcmath zlib apache


============================
Result of the script on the server where sessions are NOT working fine

PHP Version: 5.1.2
PHP OS: Linux
Error Reporting: 0 ()
Register Globals: On
Short Tags: On
Display Errors: On
Magic Quotes GPC: On
Magic Quotes Runtime: Off
Magic Quotes Sybase: Off
Config file: /usr/local/lib/php.ini
Loaded Extensions:
xmlwriter libxml dom xmlreader
xml tokenizer standard SimpleXML
SPL session sockets PDO
SQLite Reflection posix pdo_sqlite
mysqli mysql mcrypt mbstring
imap iconv hash gd
ftp date curl ctype
calendar bcmath zlib pcre
apache


Please help!
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

It looks like you have the session library on both. Perhaps you do not have write permission to the directory where the session files are stored?
(#10850)
fakhrizaki
Forum Newbie
Posts: 9
Joined: Tue Oct 02, 2007 6:15 am

Post by fakhrizaki »

But both the directories on both the servers have write permissions. I have already checked it. Still the problem is there :(
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

What are the session save paths for each of the servers and what are the permissions for each of these paths?
fakhrizaki
Forum Newbie
Posts: 9
Joined: Tue Oct 02, 2007 6:15 am

Post by fakhrizaki »

Paths for saving sessions are /tmp on both servers.

Please look at phpinfo for both servers.

Server 1 (where session ARE working fine) http://www.rivoligroup.com/phpinfo.php

Server 2 (where session are NOT working fine) http://www.dragonfeed.com/phpinfo.php
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I say crack up error reporting to E_ALL and rerun these failing scripts. Something tells me that an error would like to show, but can't.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

What are the permissions for /tmp on both machines?
fakhrizaki
Forum Newbie
Posts: 9
Joined: Tue Oct 02, 2007 6:15 am

Post by fakhrizaki »

In both machines it has 755 permissions. It's still not working if I change it to 777.

I am amazed that if there is something wrong with the sessions, it should not work on the page1 also where the variable is initialized by a value.
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Post by Stryks »

This isn't overly helpful I know, but I have had a similar situation with a host awhile back.

I never really found a solution, but just threw my hands up and switched to storing sessions in the database. Even if you don't have mySQL access, both servers appear to support SQLite databases which you could easily task for the job.

Just a thought, though you should probably try to nail this problem down rather than just jump to a workaround.

Cheers
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

fakhrizaki wrote:In both machines it has 755 permissions. It's still not working if I change it to 777.

I am amazed that if there is something wrong with the sessions, it should not work on the page1 also where the variable is initialized by a value.
I would agree with feyd at this point. Add the following lines to the session test pages and run them again on all servers to see if there is any response to the browser:

Code: Select all

<?php
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 1);
?>
fakhrizaki
Forum Newbie
Posts: 9
Joined: Tue Oct 02, 2007 6:15 am

Post by fakhrizaki »

Following error is returned by Page2 when error_reporting is turned on.

Notice: Undefined variable: _SESSION in /home/dragonf/public_html/dragonfeed/page2.php on line 4

Page2 :

Session Value:
Notice: Undefined variable: _SESSION in /home/dragonf/public_html/dragonfeed/page2.php on line 15
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

You get that error in the first page? The first page doesn't use the variable, it only creates it. Are you sure you are showing us the correct code?
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Post by Stryks »

I'd have to agree with that. Maybe you should repost the full code.

It seems odd getting notices on line 4 and 15 if the code for page2 is as shown originally. Not impossible, but odd.
Post Reply