Jump to content


Photo

Nooby Question about Sessions


  • Please log in to reply
5 replies to this topic

#1 MortimerJazz

MortimerJazz
  • Members
  • PipPipPip
  • Advanced Member
  • 60 posts
  • LocationBedfordshire

Posted 08 August 2006 - 10:04 AM

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:

        session_register('first_name');
        $_SESSION['first_name'] = $first_name;
        session_register('last_name');
        $_SESSION['last_name'] = $last_name;
	session_register('username');
        $_SESSION['username'] = $username;

Now these variables register absolutely fine in the welcome page that the user is the re-directed too:

echo "Welcome ". $_SESSION['first_name'] ." ". $_SESSION['last_name']

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:

if(!isset($_SESSION['first_name'])){
echo "You need to be registered with our site
...

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.

#2 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 08 August 2006 - 10:12 AM

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

#3 MortimerJazz

MortimerJazz
  • Members
  • PipPipPip
  • Advanced Member
  • 60 posts
  • LocationBedfordshire

Posted 08 August 2006 - 10:18 AM

Thanks alot for the reply GingerRobot.

Do I need to use

$_SESSION['first_name'] = $first_name;

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

#4 king arthur

king arthur
  • Members
  • PipPipPip
  • Advanced Member
  • 335 posts
  • LocationUK HQ

Posted 08 August 2006 - 10:20 AM

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?
Sir Isaac Newton said "If I have seen farther, it is by standing on the shoulders of giants". But it is not recorded as to whether he said it before or after he was hit on the head by a falling apple.

#5 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 08 August 2006 - 10:22 AM

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:
<?php
session_start();
echo $_SESSION['first_name'];
?>
And
<?php
session_start();
$first name = $_SESSION['first_name'];
echo $first_name;
?>


#6 MortimerJazz

MortimerJazz
  • Members
  • PipPipPip
  • Advanced Member
  • 60 posts
  • LocationBedfordshire

Posted 08 August 2006 - 10:31 AM

Do you have session_start() at the top of every page?


Ah, that was the issue. Sorry, I was being a muppet.

Thank you both for your help though.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users