Jump to content

Store & access data in a session?


Arkanto5

Recommended Posts

Hello

 

I have included a simple login system into my website. When a user logs in, a session is started and he stays connected throughout the whole website until logging out again. The user logs in via his e-mailaddress and a password, which are stored in a mysql database.

 

Now I want to add a page on which a user that is logged in, can see and edit his own information (password, cellphone number, e-mail, etc.). Or easier: instead of showing "Welcome!" showing "Welcome, John!".

 

I thought about storing the e-mailaddress into the session, and then trying to access it to select the correct record (to edit afterwards). But so far, I haven't booked any success. I have to say that this is the first time I'm working with sessions as well, so this is very new to me.

 

Thank you in advance for any help!

Link to comment
Share on other sites

Getting/setting session variables is just like any other array, but PHP then saves the serialised contents into a file at the end of each request. Personally I would store the user data returned from your database within an array (excluding the password), within the $_SESSION array:

 

$_SESSION['user'] = $row;

 

That would create a session variable called 'user', populated with the user's data. On the edit profile page you're then able to access this data:

 

First name: <input type="text" name="first_name"<?php if (isset($_SESSION['user']['first_name'])): ?> value="<?php echo htmlentities($_SESSION['user']['first_name']); ?>"<?php endif; ?>>

 

Although you'd probably be better off storing the data within a variable to make the code more readable:

 

$user = $_SESSION['user'];

First name: <input type="text" name="first_name"<?php if (isset($user['first_name'])): ?> value="<?php echo htmlentities($user['first_name']); ?>"<?php endif; ?>>

 

Note the use of htmlentities.

 

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.