Jump to content

[SOLVED] Couple of "sessions" related questions!


Recommended Posts

Hello,

 

I'm currently making a website, but I need some help with my sessions.

 

First question:

Let's say that a user has logged in, and the session has been set, and saved as "$_SESSION['username']".

Now, if I want to write "Welcome, [uSERNAME]" it'll print the users username.

But how can I fetch other info from the users mysql row? Is there a way to use the session to fetch it?

 

Question number two:

So, the user has logged in and the session has been stored.

If I go back to index.php, which is the login page, it asks me to log in again.

How can I use sessions to redirect me to "profile.php" if I'm already logged in?

 

 

Thanks!

Q1:

 

Well you could store more of the user's data as session variables as they login, or you could use $_SESSION['username'] to look up more from the database. For example:

 

select some_field from users_table where username='{$_SESSION['username']}

 

Q2:

 

Check if a session variable -that is only set after login- 'isset()':

 

if (isset($_SESSION['username']))
{
    header("Location: profile.php");
}

Hello,

 

I'm currently making a website, but I need some help with my sessions.

 

First question:

Let's say that a user has logged in, and the session has been set, and saved as "$_SESSION['username']".

Now, if I want to write "Welcome, [uSERNAME]" it'll print the users username.

But how can I fetch other info from the users mysql row? Is there a way to use the session to fetch it?

 

Question number two:

So, the user has logged in and the session has been stored.

If I go back to index.php, which is the login page, it asks me to log in again.

How can I use sessions to redirect me to "profile.php" if I'm already logged in?

 

 

Thanks!

 

For the first question, remember that sessions are separate from whatever database scheme you're using to save user information.  It's probably easiest to think of them as variables that persist between pages.  So, you'll have to use the value in the session to query the database for more info, i.e.:

 

session_start();

if(isset($_SESSION['username']))
{
   $result = mysql_query("SELECT * FROM users WHERE username = {$_SESSION['username']}");
}

 

For the second question, something like:

 

session_start();

if(isset($_SESSION['loggedIn']))
{
   header("Location: profile.php");
}
else
{
   //not logged in
}

 

EDIT: D'oh, beat by a hair!

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.