0

I've created a login script using php and I use MySQL database to store user data. Everything was working fine until I got to know that one of my friend ( whose account is also created like me in the database ) called me and said that my profile was automatically loaded on his phone. How can this be even possible as my session data or anyone's session cannot be created without the filling of the login form with their respective credentials!?

Help me with this! As this is a major loop with the security of a user data as without their consent, how could it be accessible from someone else phone!!

Session creation through login.php

session_start();
if (isset($_POST['email']) and isset($_POST['password'])){
$email = $_POST['email'];
$password = $_POST['password'];
$query = "SELECT * FROM `users` WHERE email='$email' and password='$password'";
$result = mysql_query($query) or die(mysql_error());
if(mysql_num_rows($result) > 0){
$_SESSION['email'] = $email;
$_SESSION['id'] = $id;
echo "You are logged in.";
header("Location: index.php?log=success");
unset($_POST);
}
else{
echo " Something went wrong ";
}
}

1 Answers1

1

You're assigning $_SESSION['id'] with a value of $id yet nowhere in your code are you specifying what the value of $id is. This is what's causing your sessions to mix-up as everyone is being given the same ID of null.

Get the actual User ID from the database and use that value within $_SESSION['id'] and your problem is fixed. See below for example:

/* Start the session */
session_start();

/* Check if the user has submitted the login form $_POST */
if (isset($_POST['email']) and isset($_POST['password'])){

    /* Get the values of $_POST */
    $email = $_POST['email'];
    $password = $_POST['password'];

    /* Create the SQL query - Note: you should use bindings to prevent SQL injection */
    $query = "SELECT * FROM `users` WHERE email='$email' and password='$password'";

    /* Get the MySQL result */
    $result = mysql_query($query) or die(mysql_error());

    /* Check if the user was found */
    if(mysql_num_rows($result) > 0){

        /* Get the User ID from the database */
        while($row = mysql_fetch_assoc($result)){
            $id = $row['THE NAME OF YOUR ID COLUMN']; // replace with the name of your ID column in your DB
        }

        /* Set the session values */
        $_SESSION['email'] = $email;
        $_SESSION['id'] = $id;

        /* Print output */
        echo "You are logged in.";

        /* Redirect to login page */
        header("Location: index.php?log=success");

        /* Unset POSTS */
        unset($_POST);

    } else {
        /* Show login error message */
        echo " Something went wrong ";
    }
}

Also, your code is currently susceptible to SQL injection attacks. To prevent such attacks I would advise you bind your parameters to your SQL query rather than inputting them directly. SQL Injection Info

Mark
  • 676
  • 6
  • 19
  • I'd recommend completely altering the focus of your answer to be **primarily** about the security implications, as-is, it's mentioned in a sort of "afterthought" mode. – random_user_name May 21 '19 at 16:38