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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

print_r($_session);

Status
Not open for further replies.

aarrgghh

Technical User
Sep 5, 2002
60
US
I am having problems keeping session variables. Whne I use print_r($_session) on the first page I can see my variable values, but when I go to the next page and try to access the variables using the same function, print_r($_session). I get an empty Array(). Does anyone know whats going on?

Here is the flow:
LogIn.html - variables passed to the login process page via post. (page 1)

LoginProcess.php - Session is started and variables are registered. (page 2)

Intro.php - Session Variables are accessed easily. (page 3)

Page2.php - Session variables are gone. Returns Array(). (page 4)

Thanks in advance for the help.
 
Are you running start_session() at the beginning of every page which uses the $_SESSION array?

-Rob
 
Ah yeah, session_start :)

Is there a change in the domain?

For example on an intranet are you at one point specifying the domain and at one point not? Have you passed through a page which didn't start the session? Have you changed directory levels?

The first I know can screw with things from experience, the others I'm just trying to give you ideas to debug on. I don't know the answer but figured it might help.

-Rob
 
Are you referencing the variable as $_SESSION or $_session in your code?

PHP is case-specific with variable names. It could be that PHP is instantiating a local array called $_session, which would not be a part of the session variable system

Want the best answers? Ask the best questions: TANSTAAFL!
 
When you are generating page4.php is it in the same window?
Do you - at any time - open a new window and load page4.php in there?

If so, some browsers create a new instance and a new session when a new window is opened. This behavior is inconsistent between browsers.

Browsers that offer tabs, such as Mozilla, keep the same session ID.

To check for that print also the PHPSESSID from the cookie:
Code:
print($_COOKIE[session_name()]);
and compare it between the pages where you lose the values.
 
skiflyer,

I am not specifying a domain, at all. I was leaving that to the default, which is &quot;no value&quot; according to phpinfo. I have not gone to another page w/out starting session_start(). I have not changed directories. My links are simple: <a href=&quot;link.php&quot;>

sleipnir214,

I am using $_SESSION to access variables in all instances.

DRJ478,

I am staying in the same window.

all,

For debugging I am passing the session_name() and session_id() from page to page too. These are accessable and remain constant. Thanks for the quick responses. I have been working on this for hours and hours. I have tried everything I can think of and your ideas are giving me a little more hope that I will resolve this problem today...hopefully very soon, as I have a lot more to do.

Thanks,
Patrick
 
Well, then,

The next question would be:
Are you doing anything to the session vars when page 3 is generated? I surmise there is something going on there.

It seems odd that you would lose the session vars just like that.

Also, how does page 3 go to page 4?

Sorry for so many questions, but that's the only way!
 
Don't appologize. These questions are good! Like you said its the only way without writing a huge post and confusing everything/everyone.

Yes, I am attempting to manipulate the variables. This is at the top of page 3 and 4:

<?php
session_start();

if ($_SESSION['currentPage'] > $_SESSION['lastPage'])
{
$_SESSION['currentPage'] = $_SESSION['lastPage'];
}
echo session_name().&quot;<br>&quot;;
echo session_id().&quot;<br>&quot;;
$registered = session_is_registered('firstName');

if ($registered)
{
echo &quot;Variables<br>&quot;;
print_r($_SESSION);
}
else{echo &quot;Lost the variable&quot;;}
$_SESSION['currentPage'] = 0; /*or 1 or 2 (depending on the page)*/
?>

This web page reqires me to be able to keep track of the users current location as well as the farthest point the user has reached in the web site. Hence, the currentPage cannot be farther (>) than the lastPage. This would cause me to lose my variables?
 
Result for page three with above code:

PHPSESSID
892b5d1a71c06ed0b2ad046cf714328e
Variables
Array ( [firstName] => Patrick [lastName] => lastName [currentPage] => 0 [lastPage] => 0 ) Array

Result for page 4 with above code:

PHPSESSID
892b5d1a71c06ed0b2ad046cf714328e
Variables
Array ( )

Thanks again for everyone's help.

Patrick
 
The variables also disapear when you hit the back button. That will be a problem too.
 
I disabled the first if statement to see if that was the problem...I still get the same result.

<?php
session_start();

/*if ($_SESSION['currentPage'] > $_SESSION['lastPage'])
{
$_SESSION['currentPage'] = $_SESSION['lastPage'];
}*/

echo session_name().&quot;<br>&quot;;
echo session_id().&quot;<br>&quot;;
$registered = session_is_registered('firstName');

if ($registered)
{
echo &quot;Variables<br>&quot;;
print_r($_SESSION);
}
else{echo &quot;Lost the variable&quot;;}
$_SESSION['currentPage'] = 0; /*or 1 or 2 (depending on the page)*/
?>

Whats interesting to me is that &quot;Variables&quot; prints, but nothing else.
 
Ok,

I see nothing wrong with what you are doing. However, please tell me how page three goes to page four.
Is there a form that has page four as the action or is there a link?

Also, when you look at the posted output we can see that the session as such is working, hence the session ID stays the same and $_SESSION is an array, not &quot;Lost the variable&quot;.

Try to inspect the vars after manipulation, before going to the next page:
Code:
if ($registered)
{
echo &quot;Variables<br>&quot;;
print_r($_SESSION);
}
else{echo &quot;Lost the variable&quot;;}
$_SESSION['currentPage'] = 0; /*or 1 or 2 (depending on the page)*/
Code:
echo &quot;After:&quot;;
print_r($_SESSION);
?>

 
I am going to page four via a link. No forms involved. I tried your suggestion. Here is the result on page four:

PHPSESSID
892b5d1a71c06ed0b2ad046cf714328e
Variables
Array ( ) After:Array ( [currentPage] => 1 )

Thanks for the ideas/help.

Patrick
 
More questions:

1. What PHP Version (4.1.2 eh?)
2. Platform

I tried your code in my environment and there is no trouble.
 
This won't help the issue any, but as an aside... session_is_registered() is not meant to be used with the $_SESSION array. From what I can surmise it's a more or less deprecated function.

As to the rest I'm scratching my head at the moment.
 
I am using php 4.3.0 with apache 2.0. SO now I have droped all other code from my pages in order to figure this out, and now I can pass the variables, but I cannot update them.

Here my code in its entirety:

1.php---
<?php
session_start();

$_SESSION['firstName'] = &quot;firstName&quot;;
$_SESSION['lastName'] = &quot;lastName&quot;;
$_SESSION['Cert'] = 987654321;
$_SESSION['currentPage'] = 0;
$_SESSION['lastPage'] = 0;

?>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;>
</head>

<body>
<?php
print_r($_SESSION);
?>

<A HREF=&quot;2.php&quot;>Link to page 2</A>
</body>
</html>

2.php----

<?php
session_start();
$_SESSION['currentPage'] = 1;
if ($_SESSION['currentPage'] > $_SESSION['lastPage'])
{$_SESSION['currentPage'] = $_SESSION['lastPage'];}
?>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;>
</head>

<body>
<?php
print_r($_SESSION);
?>

<A HREF=&quot;3.php&quot;>Link to page 3</A>
</body>
</html>

3.php----

<?php
session_start();
?>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;>
</head>

<body>
<?php
print_r($_SESSION);
?>

<A HREF=&quot;4.php&quot;>Link to page 4</A>
</body>
</html>

4.php----

<?php
session_start();
session_unset();
session_destroy();
?>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;>
</head>

<body>
<?php
print_r($_SESSION);
?>
Should be empty<br>
<A HREF=&quot;5.php&quot;>Link to page 5</A>
</body>
</html>

5.php-----

<?php
session_start();
session_unset();
session_destroy();
?>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;>
</head>

<body>
<?php
print_r($_SESSION);
?>
Should be empty<br>
<A HREF=&quot;1.php&quot;>Try again page 1</A>
</body>
</html>


On page 4 the variables are destroyed as expected. Page 2, however, does nothing.
 
I'm confused... what do you mean it does nothing?

These pages do everything you expect right? Just you don't get anything useful because you took all the code out... right?

What I'd expect is page one prints your populated array, page two does the same, page 3 does the same, then pages four and five show empty arrays... no?

Again as an aside... I'd point to the note on

session_unset shouldn't be used with the $_SESSION variable.
 
Your right. It does everything you just said, except that currentPage and lastPage remain at 0, when they should change to 1 if I am not mistaken.

Thanks again for everyones help.
 
No, it should stay at 0.

You say, current = 0, last = 0.
Then you say current = 1

Then you say
Code:
if current > last (which it is
then
current = last

so, current = 0 when it's all done.

-Rob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top