我在一个网站上工作,我想让用户能够登录到该网站。我是一名自学成才的开发人员,所以我不确定最佳实践是什么。我之前在我的 java 应用程序中使用过 bcrypt,并在下面的密码 php 文件中实现了它(只有真正重要的行在类的末尾)。
到目前为止,我现在对登录系统唯一关心的是,我使用的 ajax 是可见的,并显示了正在运行什么脚本来检查密码。这仍然安全吗?如果不是应该怎么做。
最后在我的 Password.php 脚本中,我假设我应该制作一个 cookie 以允许网站知道哪个帐户已成功登录,但如何使这个 cookie 安全?
我的代码在下面只是想知道这是否是一种危险的做事方式以及如何确保 cookie 的安全谢谢您的任何回答
阿贾克斯检查器
<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);
?>