Jump to content

Recommended Posts

I've made a login script that checks it the username and or password is correct however it does not do anything if the username is incorecct

here's my code to check the username:

$checklogin2 = "SELECT * FROM users WHERE username = '". $_POST['username']. "'";
$checklogin = mysql_query($checklogin2) or die;
while ($row = mysql_fetch_array($checklogin, MYSQL_ASSOC)) {
if (mysql_num_rows($checklogin) == 0) {
$_SESSION['error'] = '1';
header("Location: ".$_SERVER['HTTP_REFERER']);
}

 

and here's the login form

echo '<h2 id="round">Login</h2>
<form method="post" action="login.php">
<fieldset>
<legend>Sign-In</legend>';
if($_SESSION['error'] == 1) {
echo "Your username or password is incorrect<br/><br/>";
unset($_SESSION['error']);
}
echo '<label for="username">Username:</label>
<input  type="text" name="username" value="" />
<label for="password">Password:</label>
<input  type="password" name="password" value="" />
<input type="submit" name="submit" value="Sign In" />

<p><a href="#">Forgot your password?</a><br/><a href="#">Register for a free account<a/></p>
</fieldset>
</form>
';

 

Link to comment
https://forums.phpfreaks.com/topic/66397-solved-username-check-does-not-work/
Share on other sites

Lets eliminate one problem at a time. Get rid of the redirect and use...

 

<?php

  $checklogin2 = "SELECT * FROM users WHERE username = '". $_POST['username']. "'";
  if ($checklogin = mysql_query($checklogin2)) {
    if (!mysql_num_rows($checklogin)) {
      echo "Username not found";
    } else {
      $row = mysql_fetch_assoc($checklogin);
    }
  } else {
    echo mysql_error();
  }

?>

 

Notice you need to call mysql_num_rows() before mysql_fetch* anyway. You'll get errors the way you had it.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.