Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First of all, you really should use mysqli. Standard <code>mysql_</code> functions are now deprecated, and for very solid reasons. Your code would look something like this, if it was rewritten using mysqli:</p> <pre><code>&lt;?php //Connect to database $con = new mysqli('host', 'user', 'PW', 'dB'); if( $con-&gt;connect_errno &gt; 0 ) { die( 'Unable to connect to dB!' ); } //Grab User submitted information $name = $_POST['username']; //login name $pass = $_POST['password']; //login password // Make sure you do some validation of the $_POST data here //Fetch user record from members table where username = inputed name $statement = $con-&gt;prepare("SELECT password FROM members WHERE username = ?"); // Binds $name as a string ('s') parameter to the first ? found in the statement $statement-&gt;bind_param('s', $name); $statement-&gt;execute(); //check to see if that username exists in DB &amp; if not; stop, inform user &amp; ask if he wants to register if ( $statement-&gt;num_rows === 0 ) { die('User does not exist. &lt;a href="register.php"&gt;Click Here to Register&lt;/a&gt;'); } // Bind variables to the result; you need a variable for every column that is selected $statement-&gt;bindResult($dbPassword); $statement-&gt;fetch(); //check password, if pw entered != pw from pulled record then stop &amp; inform user if ($pass != $dbPassword) { die('Incorrect password, please try again.'); } $statement-&gt;close(); $con-&gt;close(); </code></pre> <p>If you are not using mysqli after all, know that the code that you wrote is not working because of 2 issues:</p> <ul> <li>You spelled <code>$name = $_POST["usersname"];</code> as <code>usersname</code> instead of <code>username</code></li> <li>On the line <code>$result = mysql_query("SELECT * FROM members WHERE username = $name");</code> you need to escape the string like this: <code>username = " . mysql_real_escape_string( $name ) . "</code> otherwise quotes will not be added and the string will be misinterpreted as a token (also, there are SQL injection concerns)</li> </ul> <p>Besides these points, it is important that you store passwords correctly in the database. You must not store passwords in plain text, as it is an enormous security risk. You need to store passwords as hashes, which are one-way functions that make it impossible (or at least harder) to obtain the password. I suggest you look into the <a href="http://dk1.php.net/manual/en/function.crypt.php" rel="nofollow">Crypt function</a> in PHP. Used with the Blowfish algorithm and a suitable salt, it can provide excellent security, but further details about this are outside the scope of this post. You can find more information in the comments on that page.</p> <p>EDIT: Updated the code to not use get_result()</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. This table or related slice is empty.
    1. 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