phppup Posted April 20, 2020 Share Posted April 20, 2020 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? Quote Link to comment Share on other sites More sharing options...
gw1500se Posted April 20, 2020 Share Posted April 20, 2020 (edited) With no code showing how you set up the session variable on each page, we can't be much help. Also I don't understand why you are using 3 pages instead of one. Edited April 20, 2020 by gw1500se Quote Link to comment Share on other sites More sharing options...
phppup Posted April 20, 2020 Author Share Posted April 20, 2020 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? Quote Link to comment Share on other sites More sharing options...
Barand Posted April 20, 2020 Share Posted April 20, 2020 (edited) Are you calling session_start() on each of those pages? Edited April 20, 2020 by Barand Quote Link to comment Share on other sites More sharing options...
phppup Posted April 20, 2020 Author Share Posted April 20, 2020 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. Quote Link to comment Share on other sites More sharing options...
gw1500se Posted April 20, 2020 Share Posted April 20, 2020 You will need to show all your code related to setting and retrieving session data. Quote Link to comment Share on other sites More sharing options...
phppup Posted April 20, 2020 Author Share Posted April 20, 2020 $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. Quote Link to comment Share on other sites More sharing options...
phppup Posted April 20, 2020 Author Share Posted April 20, 2020 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)? Quote Link to comment Share on other sites More sharing options...
StevenOliver Posted April 21, 2020 Share Posted April 21, 2020 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. Quote Link to comment Share on other sites More sharing options...
phppup Posted April 21, 2020 Author Share Posted April 21, 2020 (edited) @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 April 21, 2020 by phppup Quote Link to comment Share on other sites More sharing options...
Barand Posted April 21, 2020 Share Posted April 21, 2020 8 hours ago, gw1500se said: You will need to show all your code related to setting and retrieving session data. Quote Link to comment Share on other sites More sharing options...
phppup Posted April 21, 2020 Author Share Posted April 21, 2020 @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. Quote Link to comment Share on other sites More sharing options...
gizmola Posted April 24, 2020 Share Posted April 24, 2020 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.