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

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

Post Reply
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

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

Post 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>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you are passing the text 'true' and 'false' .. no the boolean values true and false.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Post 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 :?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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....
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Post 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.
Black Unicorn
Forum Commoner
Posts: 48
Joined: Mon Jun 16, 2003 9:19 am
Location: United Kingdom

Post 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>
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Post by JAB Creations »

Nope, still says Broadband and I tried a different varibles too. I'm totally lost. Thanks for the attempt though.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Post by JAB Creations »

Wait, I'm no genius but it looks like when it detects the variable as false it resets the session?
Kedo
Forum Newbie
Posts: 6
Joined: Tue Nov 30, 2004 12:04 am
Location: Cologne, Germany
Contact:

Post 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
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

with the values sent, it'd always be true.
Post Reply