Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The error itself tells you everything. The constant <code>SALT_LENGTH</code> is not a <code>long</code>. I suspect it's not defined at all, so PHP converts the bare string to a string (<code>"SALT_LENGTH"</code>) and passes <em>that</em> to <code>substr()</code>, which complains.</p> <p>That being said... This code is dangerously wrong:</p> <ol> <li><p><code>if(!isset($result))</code>: Really? This condition will <em>always</em> be false because <code>$result</code> will <em>always</em> be set (unless you run into a problem with <code>mysql_query()</code>, but that doesn't tell you anything about the valididty of the login). And since <code>mysql_query()</code> never returns <code>null</code>, <strong>no logins will ever be rejected.</strong></p></li> <li><p>This query:</p> <pre><code>SELECT * FROM users WHERE hashkey = '{$local_salt}' </code></pre> <p>Is invalid. <code>$local_salt = generateHash($password);</code>. From the <code>generateHash</code> function, if a salt is not given, one will be randomly created for you. Thus, every call to <code>generateHash</code> will generate a new hash, which means it can't be compared to anything in the database.</p></li> </ol> <p>On the basis of the two (very) egregious mistakes above, I would throw away this piece of code for good. </p> <hr> <p>The correct way to check for a valid hash when a salt is used is something like:</p> <pre><code>$_SESSION['user_logged'] = null; // fetch hashed pw from db, where username is the submitted username $result = mysqli_query("SELECT hash FROM users WHERE username = '{$username}'"); if ($result-&gt;num_rows != 0) { $row = $result-&gt;fetch_assoc(); $hash = $row['hash']; $salt = substr($hash, 0, SALT_LENGTH); // extract salt if (generateHash($password, $salt) == $hash) { // login successful. $_SESSION['user_logged'] = $username; // don't store passwords here } } // if $_SESSION['user_logged'] is not set, the login failed if (!isset($_SESSION['user_logged'])) { // you *don't* want to tell people which one (login or pw) is invalid echo 'Invalid login or password'; } </code></pre> <p>Note: It's very important that the <code>SALT_LENGTH</code> is at most 32, or this won't work because of the way <code>generateHash()</code> is implemented.</p>
    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