Can I choose an exact password for the username in the database?

I created a database with a table ( UserPass

) which essentially stores usernames and passwords.

Now in my form, I want to ask the user for a username and password, and by checking this, I realized that I could enter any username from the database and any password to login.

Can I select a password in the SQL query that is on the same line as the username?

I've tried something like:

$username = $_POST['username'];
$sql = "SELECT Password FROM UserPass WHERE Username = $username";

      

But the following failed mysqli_query

:

$query = mysqli_query($cxn, $sql);

      

So here's the whole action.php

script:

<?php
include "info.php";
include "god.php";
session_start();
if($_POST['god'] == $god)
{   

    header( "refresh:0;url=../web.html" );
}

else if(empty($_POST['god']))
{

}

else 
{
    echo "Can't you read: DON'T TRY!!!!!!!";
    exit();
}

$cxn = mysqli_connect($host, $user, $password, $dbname) or die("Go");
//check username
$userI = $_POST["username"];
$userSql = "SELECT Username FROM UserPass ";
$result = mysqli_query($cxn, $userSql) or die("Query failed!");
while($line = mysqli_fetch_assoc($result))
{
    extract($line);

    foreach ($line as $key => $val)
    {
        if($_POST['username'] == $val)
        {
            //check for password 
            $username = $_POST['username'];
            $pass = $_POST['password'];
            $sql = "SELECT Password FROM UserPass";
            $passres = mysqli_query($cxn, $sql) or die("Request cannot be handled now.");
            while ($passline = mysqli_fetch_assoc($passres))
            {
                extract($passline);
                foreach ($passline as $k => $v) 
                {
                    if($_POST['password'] == $v)
                    {
                        header( "refresh:0;url=../web.html");


                    }

                    else 
                    {
                        session_destroy();
                    }

                }
            }

        }
    }
}
/*
if($userI == $line['Username'])
{
    //check for password
    $pass = $_POST['password'];
    $sql = "SELECT * FROM UserPass";
    $res = mysqli_query($cxn, $sql) or die("Pass query failed");
    $passline = mysqli_fetch_assoc($res);
    if($pass == $passline['Password'])
    {
        header( "refresh:4;url=../web.html");
        session_start();
        echo "Login succesful, session started, session id: ";
    }
}
*/


    /*
    if($_POST['username'] == $val)
    {
        //check for password
        $b = $_POST['username'];
        $pass = $_POST['password'];
        $sql = "SELECT * FROM UserPass";
        $passres = mysqli_query($cxn, $sql);
        $passline = mysqli_fetch_row($passres);
        foreach ($passline as $k => $v ) 
        {
            if($_POST['password'] == $v)
            {
                header( "refresh:0;url=../web.html");
                session_start();
            }
        }


    }
    */
    /*
    else 
    {
        print("Destroying Laptop...US Government...Destroying Laptop...\n");
        exit();
    }
    */              
?>

      

+3


source to share


2 answers


You just need to check if there is an entry containing both the username and password of the same user:

$password = mysqli_real_escape_string($password);
$username = mysqli_real_escape_string($username);
$sql = "SELECT Password FROM UserPass WHERE Username = '$username' AND Password = '$password'";

      



if there is 1 such result, that's ok.

By the way, you shouldn't store passwords in plain text - use a one-way hashing function instead and only compare password hashes.

0


source


Your SQL query must contain "AND" like this:   

$link = mysqli_connect("myhost","myuser","mypassw","mybd") or die("Error " . mysqli_error($link));

$username = mysqli_real_escape_string($link, $_POST['username']);
$password = mysqli_real_escape_string($link, $_POST['password']);

$sql = "SELECT * FROM UserPass WHERE username = '{username }' AND password = '{$password}' LIMIT 1";

$query = mysqli_query($link, $sql);

if ($query && mysqli_num_rows($query)>0) {
     //user is authenticated
}
?>

      



Using the logical AND operator , your query must meet two conditions in order to give you an answer. These conditions should only be known to users.

Also don't leave the password field as clear text in the database. It's not safe. You have to use sha1 hash. For more information on this please take a look here http://en.wikipedia.org/wiki/SHA-1

0


source







All Articles