Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>No, its not secure / well-written. It assumes that register globals is enabled, and using global variables in place of function arguments makes the code very hard to port to different contexts. Leaving aside the bad formatting, the complete absence of any comments makes maintenance of the code difficult - good coding style is a pre-requisite to good programming - and hence security.</p> <p>There are also specific problems with the code itself. It assumes that $username is quoted, will run inefficiently. And since you compare the $username string with what is returned by the database, it's obviously not escaped properly, implying that the code is open to injection attacks. Since you're using PDO, the solution is to simply use a prepared statement / variable binding - which you have actually done with the INSERT!</p> <p>Iterating through every matching username does not make any sense and seriously undermines the behaviour. A better approach (but not the right one) would be:</p> <pre><code>/** * @param username string - a candidate username * @param DBH - connected PDO object referencing user database * @return bool - true if username does not exist already * * search the current list of users to see if the candidate username is available */ function checkName($username, $DBH){ $STH = $DBH-&gt;prepare('SELECT username FROM users WHERE username = :username'); $STH-&gt;execute(array(':username'=&gt;$username)); $STH-&gt;setFetchMode(PDO::FETCH_OBJ); $row = $STH-&gt;fetch(); if ($row === false) { die('whoops!'); } return $username!==$row-&gt;username; } </code></pre> <p>The right solution: Assuming your usernames are unique (and they really, REALLY should be), then don't bother checking if the username exists before the INSERT - create a unique index and check for duplicate key failures after the INSERT.</p> <p>Next, you use try/catch around the initial connection but do no error checking on the subsequent query. Having caught the exception, although you record and report an error you don't seem to address the flow of control at this point to prevent the rest of the code executing.</p> <p>Sorry - it's not good code, let alone secure.</p>
    singulars
    1. This table or related slice is empty.
    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