homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

$ SESSION troubles

 11:50 am on Dec 29, 2008 (gmt 0)

I am using $_SESSION to store a few details when a page loads. I refresh this page every 2 minutes and the $_SESSION variables get re-used without causing the whole to crash because on refresh you lose all the post data.

Now if the user wants to go back and add something, and then continue on to the page where the $_SESSION gets created and stored, the $_SESSION stays the same as the old one and the new addition is nowhere to be found.

Is there a way I can re-create or update a session?

My first check does -
if(!empty($_POST) && !isset($_SESSION))
set a whole bunch of $_SESSION vars with session_register('varname');

$_SESSION['var'] = $_SESSION['var'];


even if close the browser, say an old session had 3 items and I go and do one with 1 item, when I print_r($_SESSION); it will have 3 instead of 1...

Getting a bit frustrated here and would really appreciate any help anyone can provide. Thanks!



 12:02 pm on Dec 29, 2008 (gmt 0)

...the problem is in this part of the code:

if(!empty($_POST) && !isset($_SESSION))

It is set once, so the second time it just goes to the else.

You probably don't need to see if the session is set, so the following line
would be sufficient.


 12:13 pm on Dec 29, 2008 (gmt 0)

...and btw session_register is long deprecated now, you can use something similar to $_SESSION['anything'] = $_POST['something']; to create a session.


 12:21 pm on Dec 29, 2008 (gmt 0)

yeah I know about the session_register bit but my server is not on 5.3 or above so will get rid of that once the server is upgraded.

Thanks for your help, still working on it!


 12:22 pm on Dec 29, 2008 (gmt 0)

Um, you are doing a session_start, right?


 12:30 pm on Dec 29, 2008 (gmt 0)

Use of $_SESSION is preferred, as of PHP 4.1.0

If your script uses session_register(), it will not work in environments where the PHP directive register_globals is disabled.

As of PHP 4.2.0, the default value for the PHP directive register_globals is off . . . The PHP community discourages developers from relying on this directive, and encourages the use of other means, such as the superglobals.


 12:34 pm on Dec 29, 2008 (gmt 0)

gotcha, have removed them, see how that works in a bit but first to fix this $_SESSION issue. Driving me up the wall!


 12:43 pm on Dec 29, 2008 (gmt 0)

Got it! Thanks guys! Removed all session_registers and all seems well.

Great stuff!

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved