编程语言
首页 > 编程语言> > php-网站登录系统

php-网站登录系统

作者:互联网

我正在一个网站上,我希望允许用户登录该网站.我是一名自学成才的开发人员,因此我不确定确切的最佳实践是什么.我以前在我的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);

?>

解决方法:

您的方法适合进行密码检查,javascript根本不会公开正确的密码或用户名.但是,请勿使用Cookie,而应使用会话.然后,在每个经过身份验证的用户的页面上,只需包含一个脚本来检查会话以查看他们是否已通过身份验证.这样,即使他们硬链接到受保护的页面或尝试使用javascript,会话也将捕获它们.如果用户在会话中未通过身份验证,则只需将其退回登录即可.

标签:ajax,login,bcrypt,php
来源: https://codeday.me/bug/20191101/1979965.html