Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

testing for $_SESSION variable w/ isset

Status
Not open for further replies.

DaveC426913

Programmer
Jul 28, 2003
274
CA
I want to ensure that, when I first come into the script I know or set the value of variable 'debug'. I first need to know if it's set.


if (isset($_SESSION['debug'])){
$debug=$_SESSION['debug'];
}
else{
$debug=0;
}

This gives me a warning on the first line:

Notice: Undefined index: debug
 
I think you had another problem in your script.

Single quotes will work in the place you specified, and I use singlequotes in such a way all the time.



Want the best answers? Ask the best questions! TANSTAAFL!
 
I guess I do have another problem, because it's not storing the session variable across pages.

I've got this code <?php session_start(); ?> at the top of each page, and this code in an include on each page:

echo "Before: ".$_SESSION["debug"].",<br>";
if (isset($_GET["debug"])){
$debug=$_GET["debug"];
$_SESSION["debug"] = $debug;
echo "Setting :".$_SESSION["debug"]."<br>";
}
else{
if (isset($_SESSION["debug"])){
$debug=$_SESSION["debug"];
}
else{
$debug=0;
}
}
echo "Now: ".$_SESSION["debug"]."<br>";


So, on pg1, I append ?debug=1 to the URL and refresh.
It should pick up the debug=1 with the $_GET and assign it to $_SESSION["debug"], which it does.

Before: 0,
Setting :1
Now: 1



That way, when I go to the next page, $_SESSION should be set and my debug will continue. But when I get to that second page, $_SESSION["debug"] is set to 0, not 1.

Before: 0,
Now: 0

What gives?
 
Second script? There's only one script, in an include, called from both pages. It both gets and sets the session variable.

When the second page processes that script, the first thing it does is echo the state of $_SESSION["debug"] (i.e. Before:). Despite the fact that it was just set to 1 on the first page, we can see that it is now set to 0.

 
Let's get our nomenclature on the same page.

A PHP script produces output, which can be HTML. The web server transmits the HTML produced by PHP to a web browser. The user interacts with the rendered page.


I have the following files:

test_foo.php:
Code:
<?php
session_start();

include ('test_include.php');
?>

test_bar.php:
Code:
<?php
session_start();

include ('test_include.php');
?>

test_include.php:
Code:
<?php
echo 'Before: ' . $_SESSION['debug'] . ',<br>';

if (isset($_GET['debug']))
{
    $_SESSION['debug'] = $_GET['debug'];
    echo 'Setting :' . $_SESSION['debug'] . '<br>';
}

echo 'Now: ' . $_SESSION['debug'] . '<br>';
?>

When I first point my browser to test_foo.php, that script produces the outout:

[tt]Notice: Undefined index: debug in /home/sites/test/html/test_include.php on line 2
Before: ,

Notice: Undefined index: debug in /home/sites/test/html/test_include.php on line 10
Now:[/tt]


If I then point my browser to test_foo.php?debug=3, I get:
[tt]Notice: Undefined index: debug in /home/sites/test/html/test_include.php on line 2
Before: ,
Setting :3
Now: 3[/tt]

If I then point my browser to test_bar.php, I get:
[tt]Before: 3,
Now: 3[/tt]


Do my actions sound right, even though my output is different?



Want the best answers? Ask the best questions! TANSTAAFL!
 
Correct. Except for the very last step.

For me, if I then point my browser to test_bar.php, I get:
Before: 0,
Now: 0
 
Let's go to a simpler case...

Given the scripts:

test_foo.php:
Code:
<?php
session_start();

$_SESSION['foo'] = 3;

print '<html><body><a href="test_bar.php">click here.</a></body></html>';
?>

and

test_bar.php:
Code:
<?php
session_start();

if (isset($_SESSION['foo']))
{
	print '$_SESSION[\'foo\'] = ' . $_SESSION['foo'];
}
else
{
	print '$_SESSION[\'foo\'] not found.';
}
?>

When I point my browser to test_foo.php on my server, I get just the link. When I click on the link, I get:

[tt]$_SESSION['foo'] = 3[/tt]


What do you get?



Want the best answers? Ask the best questions! TANSTAAFL!
 
OK, I simplified it to its core and I am getting correct results. (oops, I see you've posted since then, I'm still one post behind.)

p1 and 2:

<?php session_start(); ?>
echo "(1 or 2)<br>";
include_once("inc_functions.php");
?>
<form action="test2.php" method="post"><input type="submit"></form>




echo "Before: ".$_SESSION["debug"].",<br>";
if (isset($_GET["debug"])){
$debug=$_GET["debug"];
$_SESSION["debug"] = $debug;
echo "Setting :".$_SESSION["debug"]."<br>";
}
else{
if (isset($_SESSION["debug"])){
$debug=$_SESSION["debug"];
}
else{
$debug=0;
}
}
echo "Now: ".$_SESSION['debug']."<br>";

So I have to figure out what's interfering with it in my real pages.
 
Well, though I swear I've changed nothing, it is now working.

The one thing I did do was move one file into the same folder as the other (to reserve my sanity when uploading incremental changes while testing). Originally, the start file was in a parent folder. It wasn't a pathing issue, as everything was working quite well before I started this debugging thing.

I sure hope folder locations isn't the problem, since I'm going to have to move the file back once I'm done.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top