Jump to content

Archived

This topic is now archived and is closed to further replies.

MortimerJazz

Nooby Question about Sessions

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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]
<?php
session_start();
echo $_SESSION['first_name'];
?>
[/code]
And
[code]
<?php
session_start();
$first name = $_SESSION['first_name'];
echo $first_name;
?>
[/code]

Share this post


Link to post
Share on other sites
[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.

Share this post


Link to post
Share on other sites

×

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.