By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,729 Members | 1,386 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,729 IT Pros & Developers. It's quick & easy.

Session : Simple question

P: n/a
Hi there,

Sorry for this beginner's question, but it's a real problem for me...

Who can tell me why the variable Foo in this page ... :

----------------------------------
<?
session_start();
?>
<html>
<head></head>
<body>
<?
if ($Foo=="ON" || strlen($Foo == 0))
$Foo="OFF";
else
$Foo="ON";
session_register ("Foo");
echo $Foo;
?>
</body>
</html>
-----------------------------------

.... contains nothing on the first time (Normal), but "OFF" every
reloading of this page ?

(I want OFF, ON, OFF, ON ,... at every reload in fact)

Very very thanks a lot by advance !!!
Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Info 3000 wrote:
[snip]

... contains nothing on the first time (Normal), but "OFF" every
reloading of this page ?

(I want OFF, ON, OFF, ON ,... at every reload in fact)

Very very thanks a lot by advance !!!

Try removing session_register, and change all instances of $Foo to
$_SESSION['Foo'].

Regards
Craig
Jul 17 '05 #2

P: n/a
Info 3000 wrote:
<?
session_start();
?>
<html>
<head></head>
<body>
<?
if ($Foo=="ON" || strlen($Foo == 0))
$Foo="OFF";
else
$Foo="ON";
session_register ("Foo");
echo $Foo;
?>
</body>
</html>
-----------------------------------

Who can tell me why the variable Foo in this page ... :
... contains nothing on the first time (Normal), but "OFF" every
reloading of this page ?


Because your if is wrong :)

strlen($Foo == 0)

means
1. compare $Foo to 0
2. take the length of that comparison

1. will return 0 or 1
2. will *always* return 1

you better redo your parenthesis
--
I have a spam filter working.
To mail me include "urkxvq" (with or without the quotes)
in the subject line, or your mail will be ruthlessly discarded.
Jul 17 '05 #3

P: n/a
tr********@hotmail.com (Info 3000) schrieb:
<?
session_start();
?>
<html>
<head></head>
<body>
<?
if ($Foo=="ON" || strlen($Foo == 0))
$Foo="OFF";
else
$Foo="ON";
session_register ("Foo");
echo $Foo;
?>
</body>
</html>


Try

<?php
session_start();
if (!isset($_SESSION['Foo']) OR $_SESSION['Foo'] == 'ON') {
$_SESSION['Foo'] = 'OFF';
}
else {
$_SESSION['Foo'] = 'ON';
}
?>
<html>
<head></head>
<body>
<php echo($_SESSION['Foo']) ?>
</body>
</html>

session_register is obsolete. You are encouraged to use the superglobal
$_SESSION array instead.

Regards,
Matthias
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.