Jump to content

Logged in as a another user


Go to solution Solved by KevinM1,

Recommended Posts

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 by Tomy02
Link to comment
https://forums.phpfreaks.com/topic/276258-logged-in-as-a-another-user/
Share on other sites

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

@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.

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.