Page 1 of 1

Variable always stuck as True???????

Posted: Thu Jan 13, 2005 6:52 pm
by JAB Creations
Here is my php script... it's supposed to create a session and be used throughtout my site to remember if the user choose Broadband or Dialup so I can when neccesary have the PHP print the hyperlink that is most suitable for their bandwidth.

The problem is that the variable is always passed as true.
Someone helped me make this code on a different forum and is talking to me like I have any idea of how to do anything besides create a static variable or how to set an includes.

For me to learn from this I have to be able to compare apples to oranges so if you just post a snippet of code I wont progress at all. I think this has to do with index2.php as I put that code together ... so if it is please post the full PHP code for that page and if his cde is bad please post the full PHP code for those file.


INDEX.PHP
__________
<?php include("config.php");
if($_GET['bb'] == false)
{ unset($_SESSION['bb']); } ?>

<html><head></head><body>
<a href="index2.php?bb=true">Broadband</a> ~ <a href="index2.php?bb=false">Dial-Up</a>
</body></html>


CONFIG.PHP
__________
<?
session_start();

if($_GET['bb'] == false)
{
unset($_SESSION['bb']);
}

if (empty($_GET['bb']) && empty($_SESSION['bb']))
{
$_SESSION['bb'] = false;
}
else
{
$_SESSION['bb'] = true;
}
?>

INDEX2.PHP
__________
<html>
<head>
</head>

<body>

<?php include("config.php");
if ($_SESSION['bb'] == true)
{
echo 'Broadband';
}
else {
echo 'Dialup';
}
?>
</body>
</html>

Posted: Thu Jan 13, 2005 7:11 pm
by feyd
you are passing the text 'true' and 'false' .. no the boolean values true and false.

Posted: Thu Jan 13, 2005 7:16 pm
by JAB Creations
:?: :?: :?:

If you can't mod it and show me some code that works better you're just going to confuse me even more :?

Posted: Thu Jan 13, 2005 7:19 pm
by feyd
Your code here:

Code: Select all

<a href="index2.php?bb=true">Broadband</a> ~ <a href="index2.php?bb=false">Dial-Up</a>
sets $_GET['bb'] to 'true' or 'false'.. which is always true, because they are text.

You need to check the value passed more like this:

Code: Select all

if(!isset($_GET&#1111;'bb']) || $_GET&#1111;'bb'] == 'false')
  unset(.....);
else
  $_SESSION....

Posted: Thu Jan 13, 2005 7:35 pm
by JAB Creations
Here is the original thread...

I tried that snippet of code and nothing changed.

Until it WORKS I just copy and paste stuff where people tell me to.

Posted: Fri Jan 14, 2005 10:53 am
by Black Unicorn
Try this:

Code: Select all

INDEX.PHP
__________
<?php include("config.php");
if($_GET&#1111;'bb'] == 'false')
&#123; unset($_SESSION&#1111;'bb']); &#125; ?>

<html><head></head><body>
<a href="index2.php?bb=true">Broadband</a> ~ <a href="index2.php?bb=false">Dial-Up</a>
</body></html>


CONFIG.PHP
__________
<?
session_start();

if($_GET&#1111;'bb'] == 'false')
&#123;
unset($_SESSION&#1111;'bb']);
&#125;

if (!isset($_GET&#1111;'bb']) && !isset($_SESSION&#1111;'bb']))
&#123;
$_SESSION&#1111;'bb'] = 'false';
&#125;
else
&#123;
$_SESSION&#1111;'bb'] = 'true';
&#125;
?>

INDEX2.PHP
__________
<html>
<head>
</head>

<body>

<?php include("config.php");
if ($_SESSION&#1111;'bb'] == 'true')
&#123;
echo 'Broadband';
&#125;
else &#123;
echo 'Dialup';
&#125;
?>
</body>
</html>

Posted: Fri Jan 14, 2005 12:16 pm
by JAB Creations
Nope, still says Broadband and I tried a different varibles too. I'm totally lost. Thanks for the attempt though.

Posted: Fri Jan 14, 2005 12:25 pm
by JAB Creations
Wait, I'm no genius but it looks like when it detects the variable as false it resets the session?

Posted: Fri Jan 14, 2005 1:14 pm
by Kedo
Hi !
You can try to convert the string-variable into a boolean one with '(bool)', e.g.:

if ((bool)$_GET['bb'])
{
// Other instructions
}

Posted: Fri Jan 14, 2005 1:24 pm
by feyd
with the values sent, it'd always be true.