我正在网站上工作,我希望用户能够登录该网站。我是一名自学成才的开发人员,所以我不确定最佳实践是什么。我以前在我的java应用程序中使用了bcrypt,并在下面的密码php文件中实现了它(在课程结束后只有真正重要的行)。
到目前为止我对登录系统唯一关注的是我使用的ajax是可见的,并显示正在运行的脚本以检查密码。这仍然是安全的吗?如果不是,应该怎么做。
最后在我的Password.php脚本中,我假设我应该制作一个cookie,让网站知道哪个帐户已成功登录,但如何使这个cookie安全?
我的代码如下所示,只是想知道这是一种危险的做事方式以及如何确保cookie的安全感谢您的任何答案
Ajax Checker
<script type="text/javascript" src="jquery-1.7.1.min.js"/></script>
<script type="text/javascript">
function passWord(){
var user = document.login.user.value;
var pass = document.login.pass.value;
$.ajax({
url: "Password.php",
type: "POST",
data: {'user':user, 'pass':pass},
success: function (response) {
if(response!=1){
//Wrong Password
$('#divResults').html("The password or username you entered is incorrect.");
}
}
});
return false;
}
</script>
HTML表单
<form name="login" onsubmit="return passWord()" method="get">
<font color="black"><br>Username or e-mail address:
<br><input type="text" size="40%" name="user" />
<br>You may login with either your assigned username or your e-mail address.
<br><br>Password:<br><input type="password" size="40%" name="pass" />
<br>The password field is case sensitive.<br><br></font>
<input type="submit" value="Log In" />
</form>
密码检查器
<?PHP
class Bcrypt {
private $rounds;
public function __construct($rounds = 12) {
if(CRYPT_BLOWFISH != 1) {
throw new Exception("bcrypt not supported in this installation. See http://php.net/crypt");
}
$this->rounds = $rounds;
}
public function hash($input) {
$hash = crypt($input, $this->getSalt());
if(strlen($hash) > 13)
return $hash;
return false;
}
public function verify($input, $existingHash) {
$hash = crypt($input, $existingHash);
return $hash === $existingHash;
}
private function getSalt() {
$salt = sprintf('$2a$%02d$', $this->rounds);
$bytes = $this->getRandomBytes(16);
$salt .= $this->encodeBytes($bytes);
return $salt;
}
private $randomState;
private function getRandomBytes($count) {
$bytes = '';
if(function_exists('openssl_random_pseudo_bytes') &&
(strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN')) { // OpenSSL slow on Win
$bytes = openssl_random_pseudo_bytes($count);
}
if($bytes === '' && is_readable('/dev/urandom') &&
($hRand = @fopen('/dev/urandom', 'rb')) !== FALSE) {
$bytes = fread($hRand, $count);
fclose($hRand);
}
if(strlen($bytes) < $count) {
$bytes = '';
if($this->randomState === null) {
$this->randomState = microtime();
if(function_exists('getmypid')) {
$this->randomState .= getmypid();
}
}
for($i = 0; $i < $count; $i += 16) {
$this->randomState = md5(microtime() . $this->randomState);
if (PHP_VERSION >= '5') {
$bytes .= md5($this->randomState, true);
} else {
$bytes .= pack('H*', md5($this->randomState));
}
}
$bytes = substr($bytes, 0, $count);
}
return $bytes;
}
private function encodeBytes($input) {
// The following is code from the PHP Password Hashing Framework
$itoa64 = './ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
$output = '';
$i = 0;
do {
$c1 = ord($input[$i++]);
$output .= $itoa64[$c1 >> 2];
$c1 = ($c1 & 0x03) << 4;
if ($i >= 16) {
$output .= $itoa64[$c1];
break;
}
$c2 = ord($input[$i++]);
$c1 |= $c2 >> 4;
$output .= $itoa64[$c1];
$c1 = ($c2 & 0x0f) << 2;
$c2 = ord($input[$i++]);
$c1 |= $c2 >> 6;
$output .= $itoa64[$c1];
$output .= $itoa64[$c2 & 0x3f];
} while (1);
return $output;
}
}
$user = $_REQUEST['user'];
$pass = $_REQUEST['pass'];
$link = mysql_connect('localhost', 'root', 'root');
mysql_select_db("Colleges");
$result = mysql_query("SELECT `Password` FROM `Users` WHERE `Username`=UPPER('" . $user . "')");
$resultV = mysql_fetch_array($result);
$bcrypt = new Bcrypt(10);
$isGood = $bcrypt->verify($pass, $resultV['Password']);
die($isGood);
?>
答案 0 :(得分:4)
您的方法适用于密码检查,javascript根本不会公开正确的密码或用户名。但是不要使用cookie,而是使用会话。然后,在针对经过身份验证的用户的每个页面上,您只需包含一个脚本来检查会话以查看它们是否经过身份验证。这种方式即使他们硬链接到受保护的页面或尝试使用javascript,会话将捕获它们。如果用户未在会话中进行身份验证,则只需将其退回到登录状态。