Page 1 of 1

confusing Problem with Session Variable

Posted: Mon Oct 21, 2002 9:50 am
by horgh
Hey i gotta question concerning Sessions:

in one function i have this:

Code: Select all

if ($_SESSIONї'u_lifc'] > 0) {
   echo "Login fails: ".$_SESSIONї'u_lifc'];
   $_SESSIONї'u_lifc'] = 0;
}
So if my Session Var is greater than 0 he doesn't write the ECHO-Line :evil:
Why the hell doesn't he ?? if i leave this $_SESSION['u_lifc'] = 0; out than he does. Why does PHP set the session var to 0 before it outputs my Echo-Line!?! :cry:

Posted: Mon Oct 21, 2002 9:57 am
by protokol
Don't forget that in each script that you use $_SESSION, you need to have the line:

session_start();

at the very top of the script in order for PHP to remember the values of the session variables

Posted: Mon Oct 21, 2002 10:02 am
by horgh
i have session_start() at the top.

php knows my variable because when i leave this

Code: Select all

$_SESSIONї'u_lifc'] = 0;
out it echoes my message :(

Posted: Mon Oct 21, 2002 10:11 am
by twigletmac
Why do you want to set the variable to 0 after you've checked that it's greater than zero?

Mac

Posted: Mon Oct 21, 2002 10:13 am
by horgh
because i only want to notify the user about that fact once he's seeing my page. when he then clicks on some link it should disappear in the next run of the script

should be only a small notification on login, not a message standing there for the whole session :/

Posted: Mon Oct 21, 2002 10:26 am
by twigletmac
Have you tried doing:

Code: Select all

echo '<pre>';
print_r($_SESSION);
echo '</pre>';
to check what's in the session array?

Mac

Posted: Mon Oct 21, 2002 11:56 am
by horgh
yes of course. I don't understand what's happening! 8O

i have this:

Code: Select all

// $_SESSIONї'u_lifc'] is "1"
echo "before IF: ";
echo '<pre>'; 
print_r($_SESSION); 
echo '</pre>';
if ($_SESSIONї'u_lifc'] > 0 ) {
	echo "first in  IF: ";
                echo '<pre>'; 
	print_r($_SESSION); 
	echo '</pre>';
	$query = "UPDATE users SET u_lifc=0 WHERE u_name = '".$_SESSIONї'u_name']."'";
	mysql_query($query);
	$_SESSIONї'u_lifc'] = 0;
	echo "after setting zero in IF: ";
	echo '<pre>'; 
	print_r($_SESSION); 
	echo '</pre>';
}
i only get this:
...
[u_lifc] => 0
...
i absolutely don't understand that :(((

if i comment the $_SESSION['u_lifc'] = 0; Line out then i get all the three echo-parts with [u_lifc] => 1 ...but thats clear

someone's got a hint ?!

Posted: Mon Oct 21, 2002 1:29 pm
by Heavy
Give us the circumstances:

PHP version
./configure string
OS
Webserver etc...
Your session-settings in php.ini

...and whatever might be useful.

Posted: Mon Oct 21, 2002 3:08 pm
by horgh
i have

Apache 1.3.26 on a Debian Linux Server

PHP Version 4.1.2 cuz there isn't a newer Debian Package available and i want to stay using the debian packet management

the session settings are:
[Session]
session.save_handler = files
session.save_path = /tmp
session.use_cookies = 1
session.name = sid
session.auto_start = 0
session.cookie_lifetime = 0
session.cookie_path = /
session.cookie_domain =
session.serialize_handler = php
session.gc_probability = 1
session.gc_maxlifetime = 1440
session.referer_check =
session.entropy_length = 0
session.entropy_file =
session.cache_limiter = nocache
session.cache_expire = 180
session.use_trans_sid = 1
url_rewriter.tags="a href,area=href,frame=src,input=src,form=fakeentry"

if there are any better settings i should make, you can also tell me!

Posted: Tue Oct 22, 2002 5:19 am
by rev
Are you certain that your session variable has a value of '1'?

If it's a string, and not an integer, it will not prove greater than zero.
horgh wrote:Why the hell doesn't he ?? if i leave this $_SESSION['u_lifc'] = 0; out than he does. Why does PHP set the session var to 0 before it outputs my Echo-Line!?! :cry:
Secondly, how do you know your function is a he and not a she?

Posted: Tue Oct 22, 2002 10:21 am
by horgh
yes its value is an integer ...1,2, etc... cause it comes directly from a database entry. its datatype is INTEGER.
that isn't the point, 'cause when i leave this $_SESSION['u_lifc'] = 0; line out then it does prove that its greater than zero.
secondly, i meant the PHP Compiler and in german its a HE, so excuse me for not saying IT :D

Posted: Tue Oct 22, 2002 1:54 pm
by rev
I'd try your script on another server and see if you get the same output, because per the logic you have described thus far I put together a quick, little test script and the output was sound.

Code: Select all

<?php
session_start();

$_SESSIONї'test'] = 1;

if($_SESSIONї'test'] > 0) {
	echo "It is greater than zero.<br>";
	$_SESSIONї'test'] = 0;
} else {
	echo "It is not greater than zero.<br>";
}

if($_SESSIONї'test'] > 0) {
	echo "It is greater than zero.<br>";
	$_SESSIONї'test'] = 0;
} else {
	echo "It is not greater than zero.<br>";
}
?>
This output to the browser:
It is greater than zero.
It is not greater than zero.

I am running PHP 4.2.3.

Thus, if you run your script on a subsidiary machine and run into the same problems you can be 99.9% sure it's something in your script because it's apparently not an issue in PHP.

Posted: Tue Oct 22, 2002 3:38 pm
by horgh
yes i tested a similar script and it worked as well as i wanted my script to do that. it's logical that php puts out that message and then sets it to 0.

i have a function which tests another function called checklogin() which sets up the Sessionvariables if the user logs in correctly.

Code: Select all

function login() {
	GLOBAL $PHP_SELF,$login_name,$login_pass,$errormsg;
	if(!checklogin($login_name,$login_pass)) {
		$errormsg = "Login fehlgeschlagen!";
		mysql_query("UPDATE users SET u_lifc=u_lifc+1 WHERE u_name = '$login_name'");
	}
	//else header("Location: ".$PHP_SELF);
}
i leave this else header.... out now and it works! I dont know why it works now, cause in both cases (with header and without) the next step in the script is the execution of that function which created this confusing output.

what has that header todo with my session variables ?!