session problem

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

session problem

Post 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??
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

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 »

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!!
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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?
yaron
Forum Contributor
Posts: 157
Joined: Fri Aug 22, 2003 8:40 am

Post 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
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post 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 :D.

-Nay
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

http://de.php.net/manual/en/reserved.variables.php#reserved.variables.session
Session variables: $_SESSION

Note: Introduced in 4.1.0.
sorry ;)
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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;
...
yaron
Forum Contributor
Posts: 157
Joined: Fri Aug 22, 2003 8:40 am

Post 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!!
yaron
Forum Contributor
Posts: 157
Joined: Fri Aug 22, 2003 8:40 am

Post 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!!
maldar
Forum Commoner
Posts: 49
Joined: Mon Aug 18, 2003 4:39 pm

Post by maldar »

Hi, This will work correctly:

Code: Select all

<?php
    session_start();
   $test='Session is working!!!';
   $_SESSION&#1111;test]=$test;
   echo "<BR>session=".$_SESSION&#1111;'test'];
?>
note that you will not need to use single quote inside [ symbol to define associative index in line :

Code: Select all

$_SESSION&#1111;test]=$test;
.

hope that this be useful. :)
yaron
Forum Contributor
Posts: 157
Joined: Fri Aug 22, 2003 8:40 am

Post 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
maldar
Forum Commoner
Posts: 49
Joined: Mon Aug 18, 2003 4:39 pm

Post 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']
yaron
Forum Contributor
Posts: 157
Joined: Fri Aug 22, 2003 8:40 am

Post by yaron »

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 »

probably a bad configuration setting in php.ini
what is your setting?
Post Reply