Jump to content

session data not refreshing


phppup

Recommended Posts

Trying to create a simple login page (and my code is embarassingly disgraceful at this point).

In simple terms, I have start_session on three pages.

The first page asks for name, the second for password, the third welcomes user.

The second page contains

$name=$POST['name'];
$_SESSION['name']= $name;

When I access the page 2, the name is echoed with a greeting and request for password.

But after a password submission (whether correct or incorrect) the $name disappears.

I am at a loss for why it is not being refreshed or maintained in the session.

Any ideas for me?

Link to comment
Share on other sites

Mostly because I wanted to learn more about passing variables from page to page.

Does each session variable need to be re-introduced on every page or are they contained in the session after the first introduction?

Should a session variable be readily available everytime a page is refreshed (attempts at password entry) or are they removed after the first attempt?

 

Link to comment
Share on other sites

Yes, at top of ALL pages. 

I have success when I hardcode the name variable in my SELECT statement, but as a passed variable if is present to say "Hello John, please enter password."

Then I enter password and I get a FAILURE.

echo statement for troubleshooting have shown that "John" is no longer present and the password is available.

Naturally, I am requiring BOTH criteria for validation.

Trying an IF statement as a work-around seems tro be incorrect also.

But I'm certain there's something simple that is missing.

Link to comment
Share on other sites

 $name = $_POST['name'];
 $_SESSION['name'] = $name;

Apparently the value of $name  is being lost or erased rather than stored in the session because I have created echo's to troubleshoot.

After submission of the password, the $name value is gone.

 

Link to comment
Share on other sites

Perhaps my problem is with implimentation.

Does establishing a form variable for SESSION require connecting it to $_POST?

What needs to be done to pass the SESSION variable when the page is refreshed (in an instance like submitting an incorrect password)?

 

Link to comment
Share on other sites

You have 3 pages. What is the exact code on top of each page? If all of your pages have "$name = $_POST['name']; $_SESSION['name']= $name;" on top, but you're posting the name only on the first page, then $_POST["name"] will be null on your 3rd page, and thus $_SESSION['name'] will also be null.

If you put this line of code on top of your pages, you'll see your errors:
error_reporting(E_ALL); ini_set('display_errors', 1);

My guess is on that 3rd page, you are having "undefinied index" error because of the above.

 

Link to comment
Share on other sites

@Steve Oliver:. That is not EXACTLY the set-up, but the problem is the same. I am actually establishing the session variable on page 2, but I believe it is getting set to NULL when the page reloads. 

The question now, is how to escape the problem and create a solution.

Edited by phppup
Link to comment
Share on other sites

@Barand:  Glad to see your doing well.

I won't have access to the code until weekend, but after struggling with it Sunday, I had an idea while driving in the car that might resolve my issue.

Of course, any additional hints and tips will be welcomed.

Stay safe.

Link to comment
Share on other sites

Session variables are stored (by default) in files on the server.  PHP automagically (re)connects you to that data via a cookie with the name PHPSESSID. 

It is an extremely simple and effective mechanism.  The actual contents of the data in the session file(s) is the $_SESSION array that has been serialized by the php serialize() function.  

I hope that helps clarify what is going on.

I did notice that you mentioned the word "select" in one of your replies, suggesting that there is some database involved.  

Simply stated, when you start a session, any changes you make to the the $_SESSION array in terms of adding or removing elements, is stored in the session file, and will be available upon session_start() on any future visits to the server where the browser presents the same cookie.  If things aren't working on page #3 AND you are not overwriting or removing the value from the $_SESSION array, then you probably have a problem somewhere else.  

If you just wanted a simple example system, then I would steer clear of introducing a database into it, although I understand that a typical next step for people is often to make a login system.

Not sure if this is the case for you, but your use of the word "select" made me curious as to what you are actually doing, and whether or not this has anything at all to do with sessions.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.