Jump to content

Why isn't the session maintained?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
acrionx

acrionx

    Newbie

  • Members
  • Pip
  • 2 posts
I have written out a script that let's you define a session variable called "colors". Once it is defined and you close the page and then go back to it, it is suppose to remember and tell you the color you have set. But it is not doing that.

Here is my html and php code.

Here is the actual page.

When you first visit this url, you are suppose to type in a color or whatever you want to type in. When you close the window and go back to the same url, it isn't suppose to give you that text box. It is suppose to remember the $_SESSION['color'] variable and simply print that out . I can't figure out why it's not doing this. It's like a new session starts. Why isn't my preveious session variable maintained? I think it has to do with the fact that the PHP is embedded in HTML, because when I have another script that did work and that one has only PHP codes and no HTML

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
session_start() calls an HTTP header to the client, thus it must be run before any content is sent out (incliding whitespace). Place it at the top if you script, i.e.
<?php session_start();?>
Also note you output the HTML form regardless of it the session element is present or not.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
acrionx

acrionx

    Newbie

  • Members
  • Pip
  • 2 posts
Thanks Nullworm, it worked.

Here is the modified code if anyone wants to see it.