Session works in 5.1.2 but not in 4.4.2?

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

Post Reply
jkies00
Forum Newbie
Posts: 6
Joined: Mon Feb 27, 2006 8:05 am

Session works in 5.1.2 but not in 4.4.2?

Post by jkies00 »

Hi All,

I wrote a simple session based image rotator that was intended to work as follows:
  • - When the user visits the site a session is created
    - A random image is selected and the name of the image is stored in the session
    - This image is displayed on the home page of the site
    - The user may navigate away from the home page, but on subsequent visits to the home page within the same session, the same image is displayed.
This works fine on my local machine which has PHP 5.1.2, but doesn't work properly on my host, which is 4.4.2. On the host, a random image is displayed each time the user visits the home page that runs the random image script, instead of remembering the same image for the entire session. I'm a newbie, so please forgive my ignorance...

Code: Select all

<?php
session_start();

$imagesArray = array("img01.jpg", "img02.jpg", "img03.jpg");

if(isset($_SESSION["newImg"])){ /* do nothing */ }
else{ $_SESSION["newImg"] = $imagesArray[rand(0,(sizeof($imagesArray)-1))]; }

?>

<img src="<?php echo $_SESSION["newImg"];?>"  />
I think it may be the version difference because I'm only using really simple session functionality and I read in the php manual that:
As of PHP 4.1.0, $_SESSION is available as a global variable just like $_POST, $_GET, $_REQUEST and so on...
I compared the phpinfo page on my local install against the host, and the settings are very much the same. 5.1.2 seems to have a few more variables, but other than that, no glaring differences. Has anyone ever had a similar problem?

Thanks,
~jeff
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

do you have another variable called $newImg ???

I had a very simmilar problem with sessions doing this and I am perdy sure it was that version you are having trouble with. Try renaming the session variable to somthing else and see if that fixes the problem.
jkies00
Forum Newbie
Posts: 6
Joined: Mon Feb 27, 2006 8:05 am

Post by jkies00 »

It's definitely not the variable - that's the only one I use with a name of $newImg. Do you recall what the version problem was? Here are the settings, compared side-by-side:

http://addelement.com/jennings/phpsettings.htm
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

session_write_close()?

Maybe there's an error that's getting hidden somehow?

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

Code: Select all

<?php

$ve = phpversion();
$os = PHP_OS;
$er = intval(error_reporting());
$rg = (in_array(strtolower(ini_get('register_globals')), array(0, false, '', null, 'off')) ? 'Off' : 'On');
$de = (in_array(strtolower(ini_get('display_errors')), array(0, false, '', null, 'off')) ? 'Off' : 'On');
$eol = (isset($_SERVER['HTTP_HOST']) ? "<br />\n" : "\n");

$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;
}
}
$er = $er . ' (' . implode(' | ', $e) . ')';

echo 'PHP Version: ' . $ve . $eol;
echo 'PHP OS: ' . $os . $eol;
echo 'Error Reporting: ' . $er . $eol;
echo 'Register Globals: ' . $rg . $eol;
echo 'Display Errors: ' . $de . $eol;

?>
jkies00
Forum Newbie
Posts: 6
Joined: Mon Feb 27, 2006 8:05 am

Post by jkies00 »

I'm not sure how i would use session_write_close(). After my if statement?

Thanks for your script, I ran it, and here are the resutls:

Code: Select all

PHP Version: 4.4.2
PHP OS: Linux
Error Reporting: 2037 (E_USER_NOTICE | E_USER_WARNING | E_USER_ERROR | E_COMPILE_WARNING | E_COMPILE_ERROR | E_CORE_WARNING | E_CORE_ERROR | E_PARSE | E_ERROR)
Register Globals: On
Display Errors: On
What should I be looking for in these results from your script?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it's actually for us to look at.. :P

Your server has E_NOTICE off, call error_reporting(E_ALL) at the top of your script. It may illuminate an issue I'm guessing is happening..
jkies00
Forum Newbie
Posts: 6
Joined: Mon Feb 27, 2006 8:05 am

Post by jkies00 »

Thanks again.

Here's the error output:

Code: Select all

Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /homepages/43/d137981233/htdocs/index-test.php:2) in /homepages/43/d137981233/htdocs/_includes/components/grabber-test.php on line 5

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /homepages/43/d137981233/htdocs/index-test.php:2) in /homepages/43/d137981233/htdocs/_includes/components/grabber-test.php on line 5
and here's the code it's referencing:

Code: Select all

<div id="grabber">
<?php
error_reporting(E_ALL);
session_start();

$imagesArray = array(
"/_includes/images/grabber01-getConnected.jpg",
"/_includes/images/grabber02-networkCovered.jpg",
"/_includes/images/grabber03-outdatedSystems.jpg",
"/_includes/images/grabber06-tired.jpg",
"/_includes/images/grabber07-frustrated.jpg"
);

if(isset($_SESSION["newImg"])){ /* do nothing */ }
else{ $_SESSION["newImg"] = $imagesArray[rand(0,(sizeof($imagesArray)-1))]; }

?>

<a href="/services/index.php">
<img alt="Find out more" title="Find out more" src="<?php echo $_SESSION["newImg"];?>" height="213" width="778" id="grabberImg" /></a>
</div><!-- end div grabber -->
Maybe session_start() needs to be on the page itself, not the include?

~jeff
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

ah ha, my suspicions were correct. :)

session_start() must be called before any output, in php or not. So moving it above that <div> is a start, but may not fix the issue as you may have output from elsewhere later.

Read this for more details if neccessary: viewtopic.php?t=1157



55
jkies00
Forum Newbie
Posts: 6
Joined: Mon Feb 27, 2006 8:05 am

Post by jkies00 »

I put the session_start() function at the very top of the first page, and all is working well. Thanks so much for your help, Feyd. :):):):)

~jeff
Post Reply