I’m working on an old site ad the login function takes forever. I’m trying to get to the bottom of this only im unsure whats causing it.
The login function uses AJAX.
AJAX
$password = md5($_POST['thepassword']);
$user = $_POST['theusername'];
$loginVar = $usersClass->login($user, $password);
if(is_array($loginVar))
{
$_SESSION['loggedIn'] = $loginVar;
@session_regenerate_id(true);
print "success";
}else{
print "Whoops, something went wrong! Try again.";
}
PHP Class
public function login($username, $password)
{
$rs = mysql_query("SELECT `id`,`active` from `$this->usersTable` WHERE
`username` = '".mysql_real_escape_string($username)."' AND
`password` = '".mysql_real_escape_string($password)."'");
if($rs) {
$row = @mysql_fetch_object($rs);
return $this->userInfo($row->id);
}else{
return false;
}
}
Since you have no index, I will suggest adding a composite index over
usernameandpassword:Review the MySQL
CREATE INDEXsyntax for full details.As I mentioned in the comment thread, there is nothing inherently slow about your code as it is.
I will note some things to be aware of (and I suspect you know this since you have been an SO member for a long time). It is recommended to remove the
@error suppression operators. You do have error checking on$rsalready, so there’s no need for additional suppression here.And I know you are already familiar with PDO and prepared statements from other questions, so no need to go into that…