Forum Moderators: coopster
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';
}
?>
<?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;
}
?>
# 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.
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...]
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.
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?
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 ... :)
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.
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]
<?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;?>