Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Edit 2017/11/09: Be sure to take a look at the answer from O Jones.</p> <p>First off MD5 isn't the greatest hashing method you could use for this try sha256 or sha512</p> <p>That said lets use <code>hash('sha256')</code> instead of <code>md5()</code> to represent the hashing part of the process.</p> <p>When you first create a username and password you will hash the raw password with some salt (some random extra characters added to each password to make them longer/stronger).</p> <p>Might look something like this coming in from the create user form:</p> <pre><code>$escapedName = mysql_real_escape_string($_POST['name']); # use whatever escaping function your db requires this is very important. $escapedPW = mysql_real_escape_string($_POST['password']); # generate a random salt to use for this account $salt = bin2hex(mcrypt_create_iv(32, MCRYPT_DEV_URANDOM)); $saltedPW = $escapedPW . $salt; $hashedPW = hash('sha256', $saltedPW); $query = "insert into user (name, password, salt) values ('$escapedName', '$hashedPW', '$salt'); "; </code></pre> <p>Then on login it'll look something like this:</p> <pre><code>$escapedName = mysql_real_escape_string($_POST['name']); $escapedPW = mysql_real_escape_string($_POST['password']); $saltQuery = "select salt from user where name = '$escapedName';"; $result = mysql_query($saltQuery); # you'll want some error handling in production code :) # see http://php.net/manual/en/function.mysql-query.php Example #2 for the general error handling template $row = mysql_fetch_assoc($result); $salt = $row['salt']; $saltedPW = $escapedPW . $salt; $hashedPW = hash('sha256', $saltedPW); $query = "select * from user where name = '$escapedName' and password = '$hashedPW'; "; # if nonzero query return then successful login </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload