Page 1 of 3

PHP 5.2 Sessions

Posted: Sun Dec 10, 2006 9:52 am
by audiodef
Has anyone gotten PHP 5.2 and Apache 2.2 on Windows XP SP2 working with sessions? This is far from my first post about this, and I have been searching for days, as well as attempting various configurations, all to no avail. If anyone else has gotten sessions working with these versions, please post here how you did it. I've had one person on another forum post that he just followed instructions, which is what I did in the first place, but sessions are still not working on my localhost. I can see session files being created, but session data is simply not being passed from one page to the next, and instead a new session file is created for every new page. I understand that PHP 5.2 handles sessions a little differently, in terms of GC in particular, but I still don't see that I've configured it wrong.

Posted: Sun Dec 10, 2006 9:55 am
by aaronhall
That's more or less my configuration, and I'm having no problems. Can you check the headers to see that the browser is saving the cookie and sending it back to the server?

Posted: Sun Dec 10, 2006 10:26 am
by audiodef
I'm pretty sure I'm gonna kick myself for asking... but how should I check the headers?

Posted: Sun Dec 10, 2006 10:33 am
by aaronhall
Firefox has an extension called HttpLiveHeaders that will show the request header. But you can just run the following script (in a context that is expecting the session cookie) and post its output here.

Code: Select all

print_r($_COOKIE);

Posted: Sun Dec 10, 2006 11:20 am
by audiodef
Thanks. What I got was Array(), which, since print_r prints the array struct, means it's empty. I changed php.ini to use, not use, scramble, bake, and deep-fry cookies, all with the same result. Firefox (I have 2) is set to use cookies and my firewall allows all cookies.

Are you using Apache 2.2.3 or an older version? Sometimes there are compatibility issues with the absolute latest of PHP and Apache. Maybe I just have a version combo that hasn't been patched yet.

Interestingly enough, I downloaded the PHP 5.2 .msi and tried an automatic installation - same result. So maybe it *is* an incompatible version.

Posted: Sun Dec 10, 2006 11:28 am
by aaronhall
My installation sounds exactly the same as yours (apache 2.2.x, php 5.2 installed with the msi). I'd really like to see the request/reponse headers when you first initiate the session. Get firefox and livehttpheaders if you can.

Posted: Sun Dec 10, 2006 11:46 am
by audiodef
[deleted due to brain death]

Posted: Sun Dec 10, 2006 12:01 pm
by aaronhall
A browser will only send cookies the server that originally set the cookie. These headers appear to be from a google page. Can you post the headers from the page that needs to use the session variables?

Posted: Sun Dec 10, 2006 12:19 pm
by audiodef
D'oh!

Here's the real header output. First from the test page where I set the session var, the second from the linked page that attempts to call that session var. Seems like cookies are just not part of the picture here.

http://localhost/dev/studio5/admin/test.php

GET /dev/studio5/admin/test.php HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1) Gecko/20061010 Firefox/2.0
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive

HTTP/1.x 200 OK
Date: Sun, 10 Dec 2006 18:14:56 GMT
Server: Apache/2.2.3 (Win32) PHP/5.2.0
X-Powered-By: PHP/5.2.0
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html
Transfer-Encoding: chunked
----------------------------------------------------------
http://localhost/dev/studio5/admin/test2.php

GET /dev/studio5/admin/test2.php HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1) Gecko/20061010 Firefox/2.0
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Referer: http://localhost/dev/studio5/admin/test.php

HTTP/1.x 200 OK
Date: Sun, 10 Dec 2006 18:14:58 GMT
Server: Apache/2.2.3 (Win32) PHP/5.2.0
X-Powered-By: PHP/5.2.0
Keep-Alive: timeout=5, max=99
Connection: Keep-Alive
Content-Type: text/html
Transfer-Encoding: chunked
----------------------------------------------------------

Posted: Sun Dec 10, 2006 12:23 pm
by aaronhall
And you're definitely calling session_start from the script (sorry, have to ask)

Posted: Sun Dec 10, 2006 12:31 pm
by audiodef
Yep. I can't even get this dirt-simple test to work:

test.php:

Code: Select all

<?
session_start();
$_SESSION["user_id"]=23;
?>
<a href="test2.php">go</a>
test2.php:

Code: Select all

<?
session_start();
echo $_SESSION["user_id"];
?>
The last time I installed PHP, I selected install as Apache 2.2 module and otherwise left all the defaults.

Posted: Sun Dec 10, 2006 12:34 pm
by feyd
Run the following in a new file 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');
}
$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;
}

?>

Posted: Sun Dec 10, 2006 12:37 pm
by audiodef
Results:

PHP Version: 5.2.0
PHP OS: WINNT
Error Reporting: 6143 (E_ALL)
Register Globals: Off
Short Tags: Off
Display Errors: On
Magic Quotes GPC: Off
Magic Quotes Runtime: Off
Magic Quotes Sybase: Off
Loaded Extensions:
bcmath calendar com_dotnet ctype
session filter ftp hash
iconv json odbc pcre
Reflection date libxml standard
tokenizer zlib SimpleXML dom
SPL wddx xml xmlreader
xmlwriter apache2handler

Posted: Sun Dec 10, 2006 12:38 pm
by feyd
Your code is using short tags, but PHP is set to ignore them. Use full php tags: <?php and ?>

Posted: Sun Dec 10, 2006 12:47 pm
by audiodef
With full php tags, this is what I get on test2.php:

Notice: Undefined index: user_id in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\dev\studio5\admin\test2.php on line 3