note 59251 added to function.session-start
| From: | RamonS at osu1 dot php dot net | Date: | Wed, 30 Nov 2005 18:41:24 +0000 |
| Subject: | note 59251 added to function.session-start | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-99374@lists.php.net to get a copy of this message | ||
After banging my head around for hours with PHP 5.0.2, 5.0.4 and 5.0.5 I found that on 5.0.2 this
works:
<?php
session_start();
$text = "";
$text = $_SESSION['text'];
print("$text");
?>
..but it fails on 5.0.4 and 5.0.5. The settings for session support are identical on each PHP
installation. The array $_SESSION has no elements, thus $text is empty.
It magically works everywhere with all versions if I change the script to this:
<?php
$text = "";
session_start();
$text = $_SESSION['text'];
print("$text");
?>
This time $text is exactly what was stored in the session array and the session array has all
elements that were previously added to it. Anyone has an idea why this behaves this oddly from
version to version ?
It seems that the directive "first session, then output" needs to be more explicit:
"first variable initialization, then session, then output". Generally, one can go without
variable initialization, but I prefer to do this especially when getting data for inserting or
modifying database records as I can set the variable values to something that will either fail or
just be obvious enough to point out that some input is not there.
----
Manual Page -- http://www.php.net/manual/en/function.session-start.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+59251
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+59251&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+59251&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+59251&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+59251&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+59251&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+59251&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+59251&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+59251&report=yes
Search -- http://master.php.net/manage/user-notes.php