我继承了一个要求用户登录的网站。
登录仅使用 Ajax 和 PHP,当只有 4000 个注册用户时,此登录过程可能需要一分钟才能完成。
这可能是由于登录的编码方式造成的吗?
AJAX
<form name="login-form" onsubmit="return false">
<input type="text" id="the_username" value="Username" onfocus="emptyUsername(this);" onblur="clickrecall(this,'Username')" class="focusfield input push" />
<input type="password" id="the_password" value="Password" class="input" onfocus="emptyPassword(this);" onblur="clickrecall(this,'Password')" />
<span id="dialog-login-fail" title="Login failed"></span>
<input type="submit" onclick="javascript:void(0);" id="loginBtnBre" class="sign-in signin-submit-btn" value="Login Now" />
</form>
PHP
<?php
require 'config.inc.php';
foreach($_POST as $k=>$v)
{
$_POST[$k] = trim($v);
}
if(!isset($_POST['theusername']) or !isset($_POST['thepassword']))
{
print "Please use all fields";
}elseif(empty($_POST['theusername'])){
print "Please enter a username";
}elseif(empty($_POST['thepassword'])){
print "Please enter a password";
}elseif($_POST['theusername'] == "username" && $_POST['thepassword'] == "password")
{
print "Password & User cannot be the ones already listed";
}elseif(!preg_match("/^[a-z0-9]+$/i", $_POST['theusername']))
{
print "Please use only characters and numbers for username, no spaces, dashes or others!";
}else{
$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.";
}
}
?>
询问
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;
}