MortimerJazz Posted August 8, 2006 Share Posted August 8, 2006 When using sessions in your site, do you need to "re-register" the variables that you're storing every time a user visits a new page.For example, I've written a script that checks a users login details against those stored in a database, and if they're correct then it registers various variables to be used throughout the site. It then re-directs the user to a welcome page:[code] session_register('first_name'); $_SESSION['first_name'] = $first_name; session_register('last_name'); $_SESSION['last_name'] = $last_name; session_register('username'); $_SESSION['username'] = $username;[/code]Now these variables register absolutely fine in the welcome page that the user is the re-directed too:[code]echo "Welcome ". $_SESSION['first_name'] ." ". $_SESSION['last_name'][/code]Now the problem comes, if I load up a second page. I'm currently testing to see whether the user is logged in by using:[code]if(!isset($_SESSION['first_name'])){echo "You need to be registered with our site...[/code]The issue is that this error message is coming up every time I load up the page ... whether I've logged in or not. So my question is do I need to "re-register" the variables on every page (using the first block of code above) rather than just once at the beginning and assuming that they would automatically be sent every time a new page is loaded?Thanks a lot for your help with this. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted August 8, 2006 Share Posted August 8, 2006 You do not need to register the sessions at all.On your login page, simply use:$_SESSION['first_name'] = $first_name;To store it in the seession. There is no need to register it first Quote Link to comment Share on other sites More sharing options...
MortimerJazz Posted August 8, 2006 Author Share Posted August 8, 2006 Thanks alot for the reply GingerRobot.Do I need to use[code]$_SESSION['first_name'] = $first_name;[/code]on every page of the site then? I'm only asking, as clicking through to the second page still brings up the error message telling me that I'm not logged in.Thanks again Quote Link to comment Share on other sites More sharing options...
king arthur Posted August 8, 2006 Share Posted August 8, 2006 No you only do that the first time you want to store it, that is the whole idea of having session variables. Do you have session_start() at the top of every page? Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted August 8, 2006 Share Posted August 8, 2006 No, on your login page use:$_SESSION['first_name'] = $first_name;Then, on the subsequent pages, you can either access the contents in $_SESSION['first_name'] directly, or assign it to a shorter variable e.g. $first_name by doing:$first_name = $_SESSION['first_name'];So, both of these examples would work:[code]<?phpsession_start();echo $_SESSION['first_name'];?>[/code]And[code]<?phpsession_start();$first name = $_SESSION['first_name'];echo $first_name;?>[/code] Quote Link to comment Share on other sites More sharing options...
MortimerJazz Posted August 8, 2006 Author Share Posted August 8, 2006 [quote author=king arthur link=topic=103383.msg411615#msg411615 date=1155032403] Do you have session_start() at the top of every page?[/quote]Ah, that was the issue. Sorry, I was being a muppet.Thank you both for your help though. 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.