Click here to Skip to main content
15,171,873 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a signup table that contains these columns:
id [AI], FirstName, LastName, Birthday, Gender, Email, Phone, username, password, admin.

I wrote the following codes for login system.

Can this system be reliable and secure? Can it be injectable?

What I have tried:

<?php
session_start();
class User
{
    public function CheckUser()
    {

        require "../app/core/database.php";
        if (isset($_POST['username']) && isset($_POST['pass'])) {

            $username = $_POST['username'];
            $sql = "SELECT * FROM signup WHERE username = '$username'";

            $log_result = mysqli_query($connection, $sql);
            $count = mysqli_num_rows($log_result);

            //to prevent sql injection
            if ($count != 1) {

                echo "<script>Invalid()</script>";
            } else {

                $username = stripcslashes($username);
                $username = mysqli_real_escape_string($connection, $username);

                $row = mysqli_fetch_assoc($log_result);
                $hash = $row['password'];
                $hashed_pass = password_verify($_POST['pass'], $hash);

                if ($hashed_pass == true) {

                    $_SESSION['loggedin'] = true;
                    $_SESSION['username'] = $username;
                    $_SESSION['is_admin'] = $row['admin'];
                    header("Location: ../home/index");
                } else {
                    echo "<script>Invalid()</script>";
                }
            }
        }
    }
}
?>
<script>
    function Invalid() {
        alert("Invalid user/password");
    }
</script>
Posted
Updated 10-Oct-21 6:11am
v2
Comments
Tony Hill 10-Oct-21 5:46am
   
That is vulnerable to SQL injection, the only way to be safe is to use parameterized queries.

Not even slightly.
As Tony has said, it's vulnerable to SQL Injection: Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

And SQL Injection on a login screen? That's just asking for trouble...

Counting the "affected rows" doesn't "prevent SQL injection", it might (if the attacker isn't at all careful) detect it after the fact, but your DB is toast by the point anyway ...

Secondly, You appear to be storing your passwords in plain text, which is about as safe as your code is against SQL injection ... Calling a variable "$hash" does not - in fact - hash it, or apply any salt to the user input to prevent "common lookup" attacks.

So the answer is an overwhelming "No!" - it's not safe. Not even slightly.
   
Comments
Alex Dunlop 10-Oct-21 9:44am
   
Thanks. No, I save hashed passwords in MySQL table when the user signup.
For example, user's pass is stored as below:
$2y$10$ogozJf4WuHZg.u7sKFeNOO0qCHPRzePHtjSaOlFGqCj9hu/8WEsyW
@OriginalGriff Thank you. I revised my Login system:
<?php
session_start();
class User
{
    public function CheckUser()
    {

        require "../app/core/database.php";
        if (isset($_POST['username']) && isset($_POST['pass'])) {

            $username = $_POST['username'];

            $data = $username;
            //Create a template
            $sql = "SELECT * FROM signup WHERE username =?;";
            //Create a prepared statement
            $stmt = mysqli_stmt_init($connection);
            //Prepare the prepared statement
            if(!mysqli_stmt_prepare($stmt, $sql)){

                echo "Statement failed";
                exit();
            }else{

                //Binding parameters to the placeholder
                mysqli_stmt_bind_param($stmt, "s", $data);
                //Run parameters inside database
                mysqli_stmt_execute($stmt);
                $log_result = mysqli_stmt_get_result($stmt);
                $count = mysqli_num_rows($log_result);
            }


            //to prevent sql injection
            if ($count != 1) {
                echo "<script>Invalid()</script>";
            } else {

                $row = mysqli_fetch_assoc($log_result);
                $hash = $row['password'];
                $hashed_pass = password_verify($_POST['pass'], $hash);

                if ($hashed_pass == true) {

                    $_SESSION['loggedin'] = true;
                    $_SESSION['username'] = $username;
                    $_SESSION['is_admin'] = $row['admin'];
                    header("Location: ../home/index");
                } else {
                    echo "<script>Invalid()</script>";
                }
            }
        }
    }
}
?>
<script>
    function Invalid() {
        alert("Invalid user/password");
    }
</script>
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900