Arkanto5 Posted September 6, 2011 Share Posted September 6, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/246573-store-access-data-in-a-session/ Share on other sites More sharing options...
WebStyles Posted September 6, 2011 Share Posted September 6, 2011 just load the appropriate user's information when they login. after a successful login, on the same page, you can grab all other relevant info from the database before they proceed (name, permissions, etc...), then you'll always have that info available. Quote Link to comment https://forums.phpfreaks.com/topic/246573-store-access-data-in-a-session/#findComment-1266179 Share on other sites More sharing options...
Arkanto5 Posted September 9, 2011 Author Share Posted September 9, 2011 And how exactly do I do that? I get the idea, but I can't figure out to put it in some coding... Quote Link to comment https://forums.phpfreaks.com/topic/246573-store-access-data-in-a-session/#findComment-1267312 Share on other sites More sharing options...
Adam Posted September 9, 2011 Share Posted September 9, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/246573-store-access-data-in-a-session/#findComment-1267322 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.