PDA

View Full Version : PHP session variable help



Flying Mullet
06-06-2006, 04:06 PM
I haven't used PHP session variables before, and I'm trying to now without much success.

From what I've read online, you need to create/access the session at the beginning of your page. Then you register variables with the session and afterwards any page that accesses the session can call these variables.

So here's my first page's code:

// access/register the session
session_start();
// initialize the variable
$test = "This is a test";
// register the variable in the session
session_register ("test");

Then I submit the first page to a new page, which accesses the session at the beginning:

// access the session
session_start();

And then displays the contents of the session variable on the page:

echo "The variable value is :".$test.".";

But I am getting an empty string on the resulting page, which leads me to assume that it's not finding $test in the session and is treating it as a new, local variable.

Also, I have seen some examples set the value of the session variable as such:

$HTTP_SESSION_VARS ["test"] = $test;
in regards to forms, but I'm not sure if I need to do this or what it does, although I know that right now it doesn't help me out at all.

Any help is appreciated. :yellowkin

<b>UPDATE:</b>

Okay, so I found a different example that works, but it doesn't seem as clean. The difference is that ratehr than registering the variable, I set it via the following code:

$_SESSION['test'] = $test;

And access it in a similar manner:

$test = $_SESSION['test'];

This works, but I had read that by registering the variables (as in the code in the first part of my post), you only have to have any pages start the session and then they can access the variables directly without any extra code, i.e. you don't have to pull $test from $_SESSION, you can just reference $test directly. This seems a much cleaner approach, but I can't get it to work.

crono_logical
06-06-2006, 06:53 PM
It would be interesting to see if there's any plugins for Firefox to let you see all session data and stuff currently stored and allow you to manipulate it, much like you can for cookies :p

Renmiri
06-06-2006, 07:23 PM
This is a good site to see sintax and examples
http://us3.php.net/manual/en/function.htmlspecialchars.php

Flying Mullet
06-06-2006, 08:25 PM
This is a good site to see sintax and examples
http://us3.php.net/manual/en/function.htmlspecialchars.php
:confused:

Renmiri
06-06-2006, 10:19 PM
Oh!

Just search for it

http://us3.php.net/manual-lookup.php?pattern=session+variables&lang=en

This guy had a note about what may have caused your problem


Make sure you put session_start() at the beggining of your script.

My sessions kept unsetting and I finally figured out why.

http://us3.php.net/manual/en/function.session-register.php

Endless
06-06-2006, 10:20 PM
It helps to read the manual ;)

http://us3.php.net/manual/en/function.session-register.php

Samuraid
06-07-2006, 01:48 AM
I'm not sure specifically what the issue is from a quick read of your first post, but I have a few comments and ideas for you.

First, don't use $HTTP_SESSION_VARS (which is deprecated), use $_SESSION.

Secondly, it sounds like you have register_globals on, BAD idea. It's a bad (and often disabled) feature of PHP.
http://us2.php.net/register_globals

I would suggest as the cleanest approach, always set your session variables with:

$_SESSION['name'] = value;
and always read the variables as

$name = $_SESSION['name'];
and always use a session_write_close(); when you are done manipulating the session data instead of letting PHP do so automatically when the script terminates.


you only have to have any pages start the session and then they can access the variables directly without any extra code, i.e. you don't have to pull $test from $_SESSION, you can just reference $test directly.
You can only do this if register_globals is on. And it's a bad idea because if the session is not actually started, then anyone can inject a value for $test as GET data in the query string, which is great example why register_globals should never be used. :D

Flying Mullet
06-07-2006, 01:09 PM
Oh!

Just search for it

http://us3.php.net/manual-lookup.php?pattern=session+variables&lang=en

This guy had a note about what may have caused your problem


Make sure you put session_start() at the beggining of your script.

My sessions kept unsetting and I finally figured out why.

http://us3.php.net/manual/en/function.session-register.php
You mean like I have session_start() at the beginning of each script? :p

It helps to read the manual ;)

http://us3.php.net/manual/en/function.session-register.php
I misunderstood the manual on session_start() and thought it called session_register(), not the other way around. :P

And Samuraid, I didn't realize that being able to access the variables through the anywhere approach was because they were global, I figured that session_start()/register() did the dirty work under the scenes of accessing everything from the session. I'll leave it as I have it now using $_SESSION. Thanks :)

Flying Mullet
06-21-2006, 01:57 PM
Okay, I have a new problem.

I have the following code that certain pages call when they first load:


function doCommonPageLoad()
{
session_start();
$isAuthorized = $_SESSION['isAuthorized'];
if($isAuthorized == "")
{
$_SESSION['isAuthorized'] = "authorized";
session_write_close();
}
}


The problem is that when I check the value of $_SESSION['isAuthorized'] later, it doesn't exist.

I read up some more on sessions and session variables and found a couple of others having the same problem here: http://us3.php.net/manual/en/function.session-register.php
Two things that they mention:
1) Make sure you put session_start() at the beggining of your script.
2) Call session_start() before you write html, because once html has been outputed, session_start() can't use the header function to set cookies, hence session_start() fails and no session can be started.

So I checked that I'm not writing any html before calling doCommonPageLoad() and I call session_start() at the beginning of it. The documentation says that you are not supposed to use session_register() if you are accessing the session variables with $_SESSION['xxxx'].

Does anyone have any idea as to why $_SESSION['isAuthorized'] is not being saved in the session?

Samuraid
06-22-2006, 01:55 AM
My guess is that $_SESSION may not be available after session_write_close(). I haven't tested this theory yet, but I do know that none of my PHP in the past has tried to use $_SESSION after calling session_write_close().

Flying Mullet
06-22-2006, 01:31 PM
I tried commenting session_write_close() out as well and it still didn't work. I can always try calling it at the end of my page too. Don't know if that would help, but you never know.

Samuraid
06-23-2006, 01:55 AM
Before bipper undeletes his post... :D

If you are using frames at all, keep this in mind:

Session data is usually stored after your script terminated without the need to call session_write_close(), but as session data is locked to prevent concurrent writes only one script may operate on a session at any time. When using framesets together with sessions you will experience the frames loading one by one due to this locking. You can reduce the time needed to load all the frames by ending the session as soon as all changes to session variables are done.

Flying Mullet
06-23-2006, 01:26 PM
God no. Frames are the Devil.

Aww, I missed bipper's post. :(

Samuraid
06-24-2006, 07:46 PM
He said something similar and was going to edit his post to explain further.