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
yaron
Forum Contributor
Posts: 157 Joined: Fri Aug 22, 2003 8:40 am
Post
by yaron » Fri Oct 03, 2003 7:09 am
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??
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Fri Oct 03, 2003 7:12 am
do not mix session_register() and $_SESSION
Code: Select all
$_SESSION['test']='Session is working!!!';
yaron
Forum Contributor
Posts: 157 Joined: Fri Aug 22, 2003 8:40 am
Post
by yaron » Fri Oct 03, 2003 7:16 am
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!!
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Fri Oct 03, 2003 7:19 am
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?
yaron
Forum Contributor
Posts: 157 Joined: Fri Aug 22, 2003 8:40 am
Post
by yaron » Fri Oct 03, 2003 7:23 am
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
Nay
Forum Regular
Posts: 951 Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia
Post
by Nay » Fri Oct 03, 2003 7:26 am
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
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Fri Oct 03, 2003 7:27 am
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Fri Oct 03, 2003 7:30 am
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;
...
yaron
Forum Contributor
Posts: 157 Joined: Fri Aug 22, 2003 8:40 am
Post
by yaron » Fri Oct 03, 2003 7:37 am
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!!
yaron
Forum Contributor
Posts: 157 Joined: Fri Aug 22, 2003 8:40 am
Post
by yaron » Fri Oct 03, 2003 7:51 am
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!!
maldar
Forum Commoner
Posts: 49 Joined: Mon Aug 18, 2003 4:39 pm
Post
by maldar » Fri Oct 03, 2003 7:57 am
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.
yaron
Forum Contributor
Posts: 157 Joined: Fri Aug 22, 2003 8:40 am
Post
by yaron » Fri Oct 03, 2003 8:02 am
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
maldar
Forum Commoner
Posts: 49 Joined: Mon Aug 18, 2003 4:39 pm
Post
by maldar » Fri Oct 03, 2003 8:04 am
you must use session_start() at the beginnig of every page you want use session variables.
try this $_SESSION[test ] NOT $_SESSION['test' ]
yaron
Forum Contributor
Posts: 157 Joined: Fri Aug 22, 2003 8:40 am
Post
by yaron » Fri Oct 03, 2003 8:07 am
I am using session_start() on all pages!!
Have no idea what is the problem !!
maldar
Forum Commoner
Posts: 49 Joined: Mon Aug 18, 2003 4:39 pm
Post
by maldar » Fri Oct 03, 2003 8:12 am
probably a bad configuration setting in php.ini
what is your setting?