adam291086 Posted November 21, 2007 Share Posted November 21, 2007 Hello, I have a login script working. All is fine. This works by querying a database and if the result = 1 then set a session. What i want to do is get all the information relating to the user currently logged in. When i echo the session i get the value 1. How do i query the database to get all info on the user logged in? Quote Link to comment https://forums.phpfreaks.com/topic/78235-solved-session-variables/ Share on other sites More sharing options...
trq Posted November 21, 2007 Share Posted November 21, 2007 Do you want to store these details within the $_SESSION array? Post your current login code. Quote Link to comment https://forums.phpfreaks.com/topic/78235-solved-session-variables/#findComment-395909 Share on other sites More sharing options...
adam291086 Posted November 21, 2007 Author Share Posted November 21, 2007 This is the login code <?php // we must never forget to start the session session_start(); $errorMessage = ''; if (isset($_POST['txtUserId']) && isset($_POST['txtPassword'])) { include 'config.php'; $userId = $_POST['txtUserId']; $password = $_POST['txtPassword']; // check if the user id and password combination exist in database $sql = "SELECT user_id FROM tbl_auth_user WHERE user_id = '$userId' AND user_password = PASSWORD('$password')"; $result = mysql_query($sql) or die('Query failed. ' . mysql_error()); if (mysql_num_rows($result) == 1) { // the user id and password match, // set the session $_SESSION['db_is_logged_in'] = true; // after login we move to the main page header('Location: main.php'); exit; } else { $errorMessage = 'Sorry, wrong user id / password'; } include 'library/closedb.php'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/78235-solved-session-variables/#findComment-395926 Share on other sites More sharing options...
LemonInflux Posted November 21, 2007 Share Posted November 21, 2007 <?php // Get the user $sql = "SELECT * FROM tbl_auth_user WHERE `username_column_name` = '". $_SESSION['db_is_logged_in'] ."'") or die(mysql_error()); // Save it in var's $row = mysql_fetch_row($sql); ?> The data is now in an array. ($row[number]). Quote Link to comment https://forums.phpfreaks.com/topic/78235-solved-session-variables/#findComment-395930 Share on other sites More sharing options...
trq Posted November 21, 2007 Share Posted November 21, 2007 All you need do is change your query to get the extra data you want (in this example - username and email) and then place them in the $_SESSION array. <?php // we must never forget to start the session session_start(); $errorMessage = ''; if (isset($_POST['txtUserId']) && isset($_POST['txtPassword'])) { include 'config.php'; $userId = $_POST['txtUserId']; $password = $_POST['txtPassword']; // check if the user id and password combination exist in database $sql = "SELECT user_id,username,email FROM tbl_auth_user WHERE user_id = '$userId' AND user_password = PASSWORD('$password')"; $result = mysql_query($sql) or die('Query failed. ' . mysql_error()); if (mysql_num_rows($result) == 1) { // the user id and password match, // set the session $_SESSION['db_is_logged_in'] = true; // I added this... $row = mysql_fetch_assoc($result); $_SESSION['username'] = $row['username']; $_SESSION['email'] = $row['email']; // after login we move to the main page header('Location: main.php'); exit; } else { $errorMessage = 'Sorry, wrong user id / password'; } include 'library/closedb.php'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/78235-solved-session-variables/#findComment-395940 Share on other sites More sharing options...
adam291086 Posted November 21, 2007 Author Share Posted November 21, 2007 Thanks Thorpe worked a treat Quote Link to comment https://forums.phpfreaks.com/topic/78235-solved-session-variables/#findComment-395958 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.