Writing to $_SESSION Seems to Not Stick

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jonofalltrades666
Forum Newbie
Posts: 7
Joined: Wed May 20, 2009 7:09 pm

Writing to $_SESSION Seems to Not Stick

Post by jonofalltrades666 »

Good afternoon! I'm trying to use $_SESSION for temporary storage between pages, but it seems that the write is being discarded. Can anyone tell me what I'm missing?

Code: Select all

if (isset($_SESSION["Media"]))
    $pos = count($_SESSION["Media"]);
else
    $pos = 0;
$_SESSION["Media[$pos]"] = new TempMedia($fileName, $mediaName, $mime);
throw new Exception("Uploaded media to SESSION[$pos]: " . $_SESSION["Media[$pos]"] . "; count is now " . count($_SESSION["Media"]));
Typical output (the content of the Exception) looks like this:
Uploaded media to SESSION[0]: test image.gif; count is now 0

...and indeed, on subsequent pages there will be no entries in $_SESSION["Media"]. I don't understand how $_SESSION["Media[0]"] can be set to a value, yet count($_SESSION["Media"]) remain zero. I could use a pseudo-array (Media1, Media2, etc.), but surely that's not necessary.

I have no problem getting the file information, or creating the TempMedia object, and there's no error to indicate that the problem is in writing to $_SESSION. I know that sessions are working in general, because my security is working fine (thanks to help from arborint and kaisellgren :)) This page is only accessible for signed-in users, so I know that a session exists.

Any ideas? Thanks!

Jonathan
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Writing to $_SESSION Seems to Not Stick

Post by Eric! »

You didn't post the full code, are you callng session_start first?
jonofalltrades666
Forum Newbie
Posts: 7
Joined: Wed May 20, 2009 7:09 pm

Re: Writing to $_SESSION Seems to Not Stick

Post by jonofalltrades666 »

The full code is rather extensive. Yes, each file calls session_start and accesses a user token. Aside from the user token, there are other variables stored in $_SESSION which transmit just fine; for example, I cache and verify the user's browser (HTTP_USER_AGENT), as a small mechnism to prevent session hijacking.

Thank you,

Jonathan
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Writing to $_SESSION Seems to Not Stick

Post by Eric! »

Sorry, it wasn't clear to me that the real problem was only with the session array and the other session data was fine.

I think your problem is that you are using associated arrays and trying to make the associatation the array.
I don't understand how $_SESSION["Media[0]"] can be set to a value, yet count($_SESSION["Media"]) remain zero.
Media[0] is a different label from Media. Try looking at $_SESSION["Media"][$index] as a way to setup your array.
jonofalltrades666
Forum Newbie
Posts: 7
Joined: Wed May 20, 2009 7:09 pm

Re: Writing to $_SESSION Seems to Not Stick

Post by jonofalltrades666 »

Hmm. Am I missing something in the way I'm accessing $_SESSION["Media"]? I've reorganized my code a little and it seems to be working, but if you can clarify where I went wrong I'd appreciate the deeper understanding.

This code is working for me:

Code: Select all

if (isset($_SESSION["Media"]))
    $tempMedia = $_SESSION["Media"];
else
    $tempMedia = array();
$tempMedia[] = new TempMedia($fileName, $mediaName, $mime);
$_SESSION["Media"] = $tempMedia;
Thanks for your help! I'm again impressed with the people on DevNetwork, usually forums are my last resort, but so far I've always gotten help here. Well, two out of two, but that's still 100%.

Thank you,

Jonathan
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Writing to $_SESSION Seems to Not Stick

Post by Eric! »

Yeah that would work too. You are now building an indexed array $tempMedia with your data then sticking it into a $_session array with an associated label of media. If this all sounds like jibberish, and it does to me too, try looking up info on associated arrays and indexed arrays and then look closely at how the $_session array was constructed when it wasn't working.
Post Reply