title: Website login system
body: <p>I'm working on a website and I would like to allow users to be able to login to the site. I'm a self taught developer so I'm not exactly sure what the best practices are. I've used bcrypt before in my java applications and have implemented it below in the password php file below (only real important lines there are at the end after the class).</p>
  
  <p>My only concern right now with the login system so far is that the ajax I used is visible and shows what script is running for checking the password. Is this still all secure? If not how should this be done. </p>
  
  <p>Finally in my Password.php script I assume I should make a cookie to allow the site to know which account has successfully logged in but how do I make this cookie secure?</p>
  
  <p>My code is below just want to know if this is a dangerous way of doing things and how the cookie should be made secure thank you for any answers</p>
  
  <p><strong>Ajax Checker</strong></p>
  
  <pre><code>&lt;script type="text/javascript" src="jquery-1.7.1.min.js"/&gt;&lt;/script&gt;
  &lt;script type="text/javascript"&gt;
  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;
  }
  &lt;/script&gt;
  </code></pre>
  
  <p><strong>HTML Form</strong></p>
  
  <pre><code>&lt;form name="login" onsubmit="return passWord()" method="get"&gt;
  &lt;font color="black"&gt;&lt;br&gt;Username or e-mail address:
  &lt;br&gt;&lt;input type="text" size="40%" name="user" /&gt;
  &lt;br&gt;You may login with either your assigned username or your e-mail address.
  &lt;br&gt;&lt;br&gt;Password:&lt;br&gt;&lt;input type="password" size="40%" name="pass" /&gt;
  &lt;br&gt;The password field is case sensitive.&lt;br&gt;&lt;br&gt;&lt;/font&gt;
  &lt;input type="submit" value="Log In" /&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p><strong>Password Checker</strong></p>
  
  <pre><code>&lt;?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-&gt;rounds = $rounds;
    }
  
    public function hash($input) {
      $hash = crypt($input, $this-&gt;getSalt());
  
      if(strlen($hash) &gt; 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-&gt;rounds);
  
      $bytes = $this-&gt;getRandomBytes(16);
  
      $salt .= $this-&gt;encodeBytes($bytes);
  
      return $salt;
    }
  
    private $randomState;
    private function getRandomBytes($count) {
      $bytes = '';
  
      if(function_exists('openssl_random_pseudo_bytes') &amp;&amp;
          (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN')) { // OpenSSL slow on Win
        $bytes = openssl_random_pseudo_bytes($count);
      }
  
      if($bytes === '' &amp;&amp; is_readable('/dev/urandom') &amp;&amp;
         ($hRand = @fopen('/dev/urandom', 'rb')) !== FALSE) {
        $bytes = fread($hRand, $count);
        fclose($hRand);
      }
  
      if(strlen($bytes) &lt; $count) {
        $bytes = '';
  
        if($this-&gt;randomState === null) {
          $this-&gt;randomState = microtime();
          if(function_exists('getmypid')) {
            $this-&gt;randomState .= getmypid();
          }
        }
  
        for($i = 0; $i &lt; $count; $i += 16) {
          $this-&gt;randomState = md5(microtime() . $this-&gt;randomState);
  
          if (PHP_VERSION &gt;= '5') {
            $bytes .= md5($this-&gt;randomState, true);
          } else {
            $bytes .= pack('H*', md5($this-&gt;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 &gt;&gt; 2];
        $c1 = ($c1 &amp; 0x03) &lt;&lt; 4;
        if ($i &gt;= 16) {
          $output .= $itoa64[$c1];
          break;
        }
  
        $c2 = ord($input[$i++]);
        $c1 |= $c2 &gt;&gt; 4;
        $output .= $itoa64[$c1];
        $c1 = ($c2 &amp; 0x0f) &lt;&lt; 2;
  
        $c2 = ord($input[$i++]);
        $c1 |= $c2 &gt;&gt; 6;
        $output .= $itoa64[$c1];
        $output .= $itoa64[$c2 &amp; 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-&gt;verify($pass, $resultV['Password']);
  die($isGood);
  
  ?&gt;
  </code></pre>