Page 1 of 2
session problem
Posted: Fri Oct 03, 2003 7:09 am
by yaron
Hello all,
I'm having troble making my sessions work.
I get no error but the session remains blank.
here is the code:
Code: Select all
<?php
session_start();
$test='Session is working!!!';
session_register("test");
echo "<BR>session=".$_SESSION['test'];
?>
What seems to be the problem that $_SESSION['test'] is blank??
Posted: Fri Oct 03, 2003 7:12 am
by volka
do not mix session_register() and $_SESSION
Code: Select all
$_SESSION['test']='Session is working!!!';
Posted: Fri Oct 03, 2003 7:16 am
by yaron
It acts like a regular variable.
exists on this page but if I go another page and try to type $_SESSION['test'] I get blank again!!
Posted: Fri Oct 03, 2003 7:19 am
by volka
which version of php?
what's your error_reporting level?
is display_errors enabled or do you check the server's error log?
Do you call session-start() in the script of the second document as well?
Posted: Fri Oct 03, 2003 7:23 am
by yaron
PHP - 4.0.5
I had to register the session path manually.
before I did that I used to get an error of path not found.
I tried to call session_start on the second script but same results
Posted: Fri Oct 03, 2003 7:26 am
by Nay
Code: Select all
<?php
session_start();
$_SESSION['test'] = "Session is working!!";
echo "<BR>session=" . $_SESSION['test'];
?>
I was just guessing, try that.
Hope it works

.
-Nay
Posted: Fri Oct 03, 2003 7:27 am
by volka
Posted: Fri Oct 03, 2003 7:30 am
by volka
but the quote was only half of the truth
In earlier versions, use $HTTP_SESSION_VARS.
you can use
Code: Select all
<?php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
session_start();
if(!isset($HTTP_SESSION_VARS))
die('php ' . phpversion() . ' does not seem to support $HTTP_SESSION_VARS');
if (isset($HTTP_SESSION_VARS['counter']))
$HTTP_SESSION_VARS['counter'] += 1;
else
$HTTP_SESSION_VARS['counter'] = 1;
?>
<html>
<head><title>session test</title></head>
<body>
<p>
current session-counter's value: <?php echo $_SESSION['counter']; ?>
</p>
<p>
<a href="<?php echo $_SERVER['PHP_SELF']; ?>">reload page</a>
</p>
</body>
</html>
But $HTTP_... is not superglobal
if you want to use it within a function you have to
import it from the global scope
Code: Select all
function xyz() {
global HTTP_SESSION_VARS;
...
Posted: Fri Oct 03, 2003 7:37 am
by yaron
still no luck!
I have checked my register_variable value in php.ini and it is on.
everything seems to be ok but the session is empty!!
Posted: Fri Oct 03, 2003 7:51 am
by yaron
volka,
In your example it keeps typeing 1 and that is because :if (isset($HTTP_SESSION_VARS['counter'])) is false on every refresh although it should be true!!
Posted: Fri Oct 03, 2003 7:57 am
by maldar
Hi, This will work correctly:
Code: Select all
<?php
session_start();
$test='Session is working!!!';
$_SESSIONїtest]=$test;
echo "<BR>session=".$_SESSIONї'test'];
?>
note that you will not need to use single quote inside [ symbol to define associative index in line :
.
hope that this be useful.

Posted: Fri Oct 03, 2003 8:02 am
by yaron
Works only for currnet page.
like a variable.
When I go to another page the value of $_SESSION['test']; is still blank and that is the whole idea of sessions - to be global to all pages
Posted: Fri Oct 03, 2003 8:04 am
by maldar
you must use session_start() at the beginnig of every page you want use session variables.
try this $_SESSION[test] NOT $_SESSION['test']
Posted: Fri Oct 03, 2003 8:07 am
by yaron
I am using session_start() on all pages!!
Have no idea what is the problem !!
Posted: Fri Oct 03, 2003 8:12 am
by maldar
probably a bad configuration setting in php.ini
what is your setting?