Jump to content

Nooby Question about Sessions


MortimerJazz

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.
Link to comment
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
Link to comment
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]
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.