andreasb Posted November 10, 2009 Share Posted November 10, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/181016-solved-couple-of-sessions-related-questions/ Share on other sites More sharing options...
Adam Posted November 10, 2009 Share Posted November 10, 2009 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"); } Quote Link to comment https://forums.phpfreaks.com/topic/181016-solved-couple-of-sessions-related-questions/#findComment-955033 Share on other sites More sharing options...
KevinM1 Posted November 10, 2009 Share Posted November 10, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/181016-solved-couple-of-sessions-related-questions/#findComment-955034 Share on other sites More sharing options...
andreasb Posted November 10, 2009 Author Share Posted November 10, 2009 Thanks to both of you! Now I'm able to work even further on my website. If there's anything more I need help to do, I'll post back here again ! Quote Link to comment https://forums.phpfreaks.com/topic/181016-solved-couple-of-sessions-related-questions/#findComment-955037 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.