Forum Moderators: coopster

Message Too Old, No Replies

This HAS to be simple...

Variable always stuck as true...

         

JAB Creations

6:28 pm on Jan 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



ME and ergophobe were working on a script that takes a variable via a hyperlink (bb=true/false). However when we call the includes file it always tells us that the variable is true. He really made an effort but I'm totally lost and I need someone else to give me a different perspective.

There is his code and then I have somewhat different code from another person trying to help me out. I don't understand this first set but I have a guess at the second set. I think the second set of code detects false and then kills the session... if thats true then then no wonder it won't detect false.

The issue with this first set of code is that if empty...get bb....well HOW?

Here is the FIRST SET of code...

INDEX.PHP


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

INDEX2.PHP


<html>
<head>
</head>

<body>

<?php include("config.php");

if ($_SESSION['bb'] == true) {
echo 'Broadband';
} else {
echo 'Dialup';
}

?>

</body>
</html>

CONFIG.PHP


<?
session_start();

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

SECOND ATTEMPT
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>

INDEX2.PHP


<html>
<head>
</head>

<body>

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

CONFIG.PHP


<?
session_start();

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

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

ScottYardley

9:23 pm on Jan 14, 2005 (gmt 0)

10+ Year Member



Use 1 & 0 instead of true and false and it should work.

The session is having a problem setting when the value is set to false.

ScottYardley

9:26 pm on Jan 14, 2005 (gmt 0)

10+ Year Member



idex.php

<?php include("config.php");
?>
<html><head></head><body>
<a href="index2.php?bb=1">Broadband</a> ~ <a href="index2.php?bb=0">Dial-Up</a>
</body></html>

index2.php


<?php include("config.php");?>

<html>
<head>
</head>

<body>

<?

if ($_SESSION['bb'] == 0) {
echo 'Broadband';
} else {
echo 'Dialup';
}

?>

</body>
</html>

config.php


<?
session_start();

echo 'session:',$_SESSION['bb'],'<BR>';
echo 'get:',$_GET['bb'],'<BR>';
if ($_GET['bb'] && empty($_SESSION['bb']))
{
echo 'false<br>';
$_SESSION['bb'] = 0;
}
else
{
echo 'true<br>';
$_SESSION['bb'] = 1;
}
?>

jusdrum

10:04 pm on Jan 14, 2005 (gmt 0)

10+ Year Member



The problem with your original code could partly be that you are checking for true/false sometimes as a literal string, and sometimes as a boolean:

# Boolean
if ($_SESSION['bb'] == true) {

# Literal String
if($_GET['bb'] == 'false')

You have to pick one or the other. It would probably be best to check and assign it as a boolean.

ergophobe

10:19 pm on Jan 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



JAB,

false = 0 = "" (the last being an empty string)
true = 1 = "false" (the last being a string with a value).

$a = "false";
if ($a)

will always evaluate to true.

That said, your original code was correct in that respect. It just had other problems. I've been meaning to get back to you on that, but just have other things going on. Perhaps another good soul will jump in

[webmasterworld.com...]

StupidScript

10:55 pm on Jan 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've run into similar evaluation results, and gotten the results I expect by changing my operator. I'll paraphrase some ideas that others have offered:

if ($_GET["bb"]=true)

is not enough, as any value assigned to that URI parameter will cause it to evaluate to the boolean "true" (it has a value).

if ($_GET["bb"]==true)

is also not enough, as it evaluates in a similar fashion.
You're not asking if

bb=the-word-true
, but rather whether
bb=boolean-true
.

You might try either

if ($_GET["bb"]=="true")

or

if ($_GET["bb"]==="true")

The last one is most-likely to work, as it asks if

bb=exactly-the-word-true
, and won't mistake your query for evaluation against a boolean.

You might want to consider changing the values to something completely safe, i.e.

bb=fudge
means "true" and
bb=nuts
means "false".

You could also consider using "true" and "" as your values, where

bb=true
evaluates true and
bb=
evaluates false. Then you could use
if ($_GET["bb"])
without worrying about its value. If it has one, it's true. If no value, false.

ergophobe

4:29 pm on Jan 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Stupid - what are the situations where you had to do this?

Honestly, I can think of few situations where you would truly want to test for

$var == "true" or $var === "true"

rather than

$var = true

Exceptions that come to mind are things like text processing and possibly working with some other type of data that was not initially intended for use in a database or script.

If you find that you need to use former to make your script work, then what you should do is not put quotes around the word "true" but figure out where in your script you are using string values instead of boolean values and change them accordingly.

To put it rhetorically, why wait to fix a logic problem in your script until it's causing serious problems that are hard to track down, when you are getting a red flag that will help you fix it now?

StupidScript

6:27 pm on Jan 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's not so much a logic problem issue as a brain blip issue.

The

==
operator checks to see if the values are equal ... but it makes no distinction between types of data. The
===
operator checks for both the value and the type. (Of course, the
=
operator is an assignment operator, but I assume you were using it to indicate a simple boolean check rather than a value check. :)

From the PHP.net docs:

If you compare an integer with a string, the string is converted to a number. If you compare two numerical strings, they are compared as integers. These rules also apply to the switch statement.

Sometimes I use text-type database entries, for example, to store numeric sequences, perhaps with leading zeros. It's easy for me to forget the data type when I'm doing something quick while programming the page, later.

In the case of the "true" situation, I've tried not to use that particular word as a value, but I thought perhaps comparing the value drawn from the URI and PHP's interpretation of it might be more consistent with the additional security of the "identical" comparison operator instead of the "equals" operator.

It seems like it is less convenient for you to adjust the values being passed to the URI, hence the post-op, reading solution. If it is not a problem, I'm in agreement with ScottYardley's suggestion to use more concise boolean values.

If you take that direction, then simply checking for the "trueness" of the variable would suffice, without the need to check its value.

Y'know:

if ($_SESSION["bb"])

But I figger you've probably been trying a whole bunch of stuff for a while, now ... so ... :)

JAB Creations

11:51 pm on Jan 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



WOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO! IT WORKS! IT REALLY WORKS!


if ($_GET["bb"]==="true")

Thats what works now but there are three things to work on or take in to consideration...

********
1.) There is an error being generated ...


Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at index2.php:7) in /config.php on line 2

How do we resolve this?

********
2.) How long does the session last for? How can we set the expiration to as long or short as we want?

********
3.) My site uses frames (please no debate) and I am unsure if this will have any negative effect as I have not tried it with frames.

JAB Creations

11:56 pm on Jan 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have resolved the error by removing...

session_start();

... from the config file.

Here is the entire code that WORKS! I'm still unsure about when the session will expire and if I will have any issues because of the frames.

******
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
******

<?
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 ($_GET["bb"]==="true")
{
echo 'Broadband';
}
else {
echo 'Dialup';
}
?>
</body>
</html>

[edited by: ergophobe at 3:10 am (utc) on Jan. 31, 2005]
[edit reason] fixed unmatched quotes [/edit]

JAB Creations

1:39 am on Jan 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Nope, it refuses to work across frames. I don't know how to approach this one. I tried putting the variable in the file attribute inside the frame but that didn't work (and changed the frame file to a php file of course linking it to the config file.

eaden

12:54 pm on Jan 31, 2005 (gmt 0)

10+ Year Member



You have to put session_start() at the VERY top of all your PHP pages ( before it was on line 2, you probally had a newline in config.php ). This will fix both the error and the fact that it doesn't work across frames..

JAB Creations

3:40 pm on Jan 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is what I have in the index.php (FRAME file)...

<?php session_start();
include("/home/jabcreat/public_html/themes/connection.php");?>

It keeps telling me the redirect limit has been reached. I think this is the same error when you setup an apache 301 incorrectly.

Ok...deleted the .htaccess file and I don't get that error. However I still do not have the frame page recognize the session? It still says Dialup and we're trying to trigger Broadband.

My other pages look like this...can I condense the code a little?


<?php session_start()?>
<?php chdir("/home/jabcreat/public_html/community"); require_once('global.php');?>
<?php include("/home/jabcreat/public_html/themes/connection.php");?>
<?php include("/home/jabcreat/public_html/themes/includes.php");
print $dtd;?>