PDA

View Full Version : $_SESSION - doesn't work?



sarr
10-09-2004, 07:21 PM
I'm working on some PHP-based web pages. The pages are dependent upon $_SESSION variables, but they don't seem to be working here. I've reproduced it with a simple counter script which works on my own system but fails here.


#!/bin/php
<?php
ini_set("error_log", "/php_error_log");
ini_set("log_errors", 1);

print "Content-type: text/html\n\n";
print "<head>\n<title>Counter Test</title>\n</head>\n<body>\n";
print "<hr>\n<pre>\nGLOBALS info:\n";
print_r($GLOBALS);
print "</pre>\n<hr>\n";

session_start();
$count = 0;
if (isset($_SESSION['counter']))
{
print "Pulling counter from SESSION variables<br>\n";
$count = unserialize($_SESSION['counter']);
}

$count = $count + 1;

$_SESSION['counter'] = serialize($count);

print "Counter = $count<br>\n\n</body>\n";
exit;


?>


When I reload this on my home system, the counter increments. When I load it at WH, the variable doesn't work. Can anyone tell me what I'm doing wrong (or what I'm forgetting to set in PHP.INI or Apache's httpd.conf?

Thanks,
-- Steve G. (sarr)

wildjokerdesign
10-09-2004, 08:13 PM
Have you tried $HTTP_SESSION_VARS

Here is a link that may help you further:
http://www.phpfreaks.com/forums/index.php?showtopic=31047&st=15&#entry157705

sarr
10-09-2004, 10:16 PM
I'm still working on this, but I think I'm outta here for the night.

It seems that the session info is being created, but the session ID doesn't propagate from one instance to another. Here's what the start of my script looks like (after the <?php):



ini_set("error_log", "/php_error_log");
ini_set("log_errors", 1);

$notes = '';
header("Cache-control: private"); // IE 6 Fix.
if (isset($_COOKIE['sessionid']))
{
$s_id = $_COOKIE['sessionid'];
$notes .= "Set1: s_id = $s_id\n";
}
if (isset($_COOKIES['sessionid']))
{
$s_id = $_COOKIES['sessionid'];
$notes .= "Set2: s_id = $s_id\n";
}
if (isset($HTTP_COOKIE_VARS['sessionid']))
{
$s_id = $HTTP_COOKIE_VARS['sessionid'];
$notes .= "Set3: s_id = $s_id\n";
}
session_start();
if (!isset($s_id))
{
$s_id = session_id();
$notes .= "Set4: s_id = $s_id\n";
}
$s_name = session_name();

// try ANYTHING
setcookie('sessionid', $s_id);
$_COOKIES['sessionid'] = $s_id;
$_COOKIE['sessionid'] = $s_id;



So, wise ones, I'd love to know how to get a session ID to pass along. $_SESSION isn't working... maybe the coffee isn't working either. Any suggestions?

TIA,
-- Steve G. (sarr)

Tom Howard
10-09-2004, 10:55 PM
If I remember right, you have to put the session_name() at the very beginning and session_start() right after it?

sarr
10-14-2004, 07:28 AM
I thought I'd post a follow-up to this. $_SESSION works within the standard /www/html folder, but fails to work under /www/cgi-bin. I'm not sure why this happens, but if you're having similar problems you might want to consider moving your code to the HTML area.