George Irimiciuc
George Irimiciuc

Reputation: 4633

Get exact match

I want to find usernames and passwords of exact match, but these queries let me log in even if I write the first characters of the values(for instance, if the password is "jelly", just writing "jel" will let me log in). What's the problem?

    //conn db
    $conn = new mysqli($DBServer, $DBUser, $DBPass, $DBName);

    /* check connection */
    if ($conn->connect_errno) {
        printf("Connect failed: %s\n", $conn->connect_error);
        exit();
    }
    //query
    $result = $conn->query("SELECT userName FROM Users WHERE userName='" . $_POST["user"] . "'");

    //verif user
    if (!$result->num_rows) {
        echo '<span>Username ' . $_POST["user"] . ' doesn\'t exist in the database. Do you want to  <a class="underl" href="register.php">register a new one?</a></span><br>';
    } //verific daca parola e buna(+recover pass)
    //parola goala?
    else if (empty($_POST["pwd"])) echo '<span>Please enter the password associated to your username to log in. If you forgot it you can <a class="underl" href="recover.php">recover it.</a></span><br>';
    else {//verific daca parola e buna
        /* free result set */
        $result->close();
        //query
        $result = $conn->query("SELECT password FROM Users WHERE password='" . $_POST["pwd"] . "'");
        //verif
        if (!$result->num_rows)
            echo '<span>Entered password doesn\'t match for the username ' . $_POST["user"] . '. If you forgot it you can <a class="underl" href="recover.php">recover it.</a></span><br>';
else //login...}

Upvotes: 0

Views: 113

Answers (1)

Joseph
Joseph

Reputation: 5160

Your second query knows nothing about which user you're checking against. This pretty much allows the user to log in as anyone if they know at least 1 password in the db.

$result = $conn->query("SELECT password FROM Users WHERE password='" . $_POST["pwd"] . "' AND username = '...'");

Upvotes: 1

Related Questions