Tomy02 Posted March 28, 2013 Share Posted March 28, 2013 (edited) Im developing a site a that has login function, i have about 5 users with different user id. The problem is the code im using logs all the users as the first user in my database table, this means im essentially logged in as a another user. // function that gets users id from table users function user_id_from_username( $username ){ $username = sanitize( $username ); // do the query first $result = mysql_query( "SELECT user_id FROM users WHERE username = '$username'" )or die("Could not perform select query - " . mysql_error());;;; // if there was an error, or no results, $result will be FALSE. // if $result is not false, then you know there was a row returned $num_rows = mysql_num_rows($result); if($num_rows == 1) { return true; } else { return false; } } // function that checks the credentials and should return the correct user_id. function login($username, $password) { $user_id = user_id_from_username($username); $username = sanitize($username); $password = ($password); $query = mysql_query("SELECT COUNT(`user_id`) FROM guys WHERE username='$username' AND password='$password'"); return (mysql_result($query, 0) == 1) ? $user_id : false; } // finally logging the user and redirecting $login = login($username, $password); if ($login == false) { $errors[] = 'This username and password combination is incorrect'; }else { $_SESSION ['user_id'] = $login; header ('Location: index.php'); exit(); } function logged_in() { return (isset($_SESSION['user_id'])) ? true : false; } How can i log in the others users with the correct user id, and not just the first user ? Edited March 28, 2013 by Tomy02 Quote Link to comment https://forums.phpfreaks.com/topic/276258-logged-in-as-a-another-user/ Share on other sites More sharing options...
DaveyK Posted March 28, 2013 Share Posted March 28, 2013 You dont technically have to run a login function, because you can fake your session. $_SESSION['user_id'] = 2 // user_id of the user you want to log in as. You have to place that somewhere in your header (like a header.php file if you have one). Also, keep in mind that above code will log in ALL the users and guests to that specific user, so it would be wise to use: if ($_SESSION['user_id'] == 1) // If you are logged in, guessing that YOUR id is "1" { $_SESSION['user_id'] = 2 // user_id of the user you want to log in as. } So if you are logged in (ID = 1), change the session user_id to 2. Voila Quote Link to comment https://forums.phpfreaks.com/topic/276258-logged-in-as-a-another-user/#findComment-1421581 Share on other sites More sharing options...
PaulRyan Posted March 28, 2013 Share Posted March 28, 2013 @DaveyK, I think his issue is that he can't login with a different account. Say he logs in as user 3, it always shows him logged in as user 1. His login function doesn't look right to me, but I don't have the time to look at it properly right now. Quote Link to comment https://forums.phpfreaks.com/topic/276258-logged-in-as-a-another-user/#findComment-1421586 Share on other sites More sharing options...
Solution KevinM1 Posted March 28, 2013 Solution Share Posted March 28, 2013 You never actually return the user id from user_id_from_username() Quote Link to comment https://forums.phpfreaks.com/topic/276258-logged-in-as-a-another-user/#findComment-1421587 Share on other sites More sharing options...
Tomy02 Posted March 28, 2013 Author Share Posted March 28, 2013 You are right KevinM1, i got it to work with this line. return mysql_result(mysql_query("SELECT (user_id) FROM users WHERE username='$username'"), 0, 'user_id'); Was getting a boolean error with that line earlier but it works now. Thanks everyone. Quote Link to comment https://forums.phpfreaks.com/topic/276258-logged-in-as-a-another-user/#findComment-1421591 Share on other sites More sharing options...
DaveyK Posted March 28, 2013 Share Posted March 28, 2013 I misread the question, sorry! Quote Link to comment https://forums.phpfreaks.com/topic/276258-logged-in-as-a-another-user/#findComment-1421602 Share on other sites More sharing options...
Tomy02 Posted March 29, 2013 Author Share Posted March 29, 2013 I misread the question, sorry! Dont worry about it, appreciate all input. Quote Link to comment https://forums.phpfreaks.com/topic/276258-logged-in-as-a-another-user/#findComment-1421765 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.