-1

I am quite new in php language..currently i am working on a login and registration system.but i dont know why the users still can login to the although the email and password insert is wrong,since i already make all the validation.So,guys,pls help me to see my code,see whether where the problem is.

here is my code

<?php
include('config.php');

    session_start();

    $errors=array();

 if ($_SERVER["REQUEST_METHOD"] == "POST"){

    $email = $_POST['email'];
    $password = $_POST['password'];

    if($email&&$password){

        //declare variable
        $query = mysqli_query($con,"SELECT * FROM user WHERE Email='$email' Password=''$password");
        $numrows = mysqli_num_rows($query);

        //when user correct input,check the data 
        if($numrows !== 0) {
            while($row=mysqli_fetch_assoc($query)){
                $dbemail=$row['Email'];
                $dbpassword=$row['Password'];
            }
            //if username and password match
            if($dbemail=$email&&$dbpassword=$password)
            {
                $SESSION['$email']="$email";
                header('Location:user.html');

            }

            else
            {
                $errors['notcorrect'] = "Email or password not correct";
            }
        } 
        //when insert wrong data
        else{
            $errors['notexists'] = "This email doesn't exists";
        }
    }
    //when user didnt enter anything
    else{
        $errors['nothing'] = "Please enter your email and password";
    }
}

?>

any idea?

ken
  • 2,426
  • 5
  • 43
  • 98

5 Answers5

4

Let's examine these in detail:

  • Password=''$password"
  • $SESSION
  • if($dbemail=$email&&$dbpassword=$password)
  • WHERE Email='$email' Password=''$password")
  • $_SESSION['$email']="$email";

$password is outside your quotes.

Then $SESSION is missing an underscore between the $ and SESSION.

Then you're "assigning" using 1x = sign instead of "comparing" with if($dbemail=$email&&$dbpassword=$password)

Use 2x == signs.

You're missing an AND for WHERE Email='$email' Password=''$password")

WHERE Email='$email' AND Password='$password'");

You should also, and is recommended to add exit; after header.

header('Location:user.html');
exit;

Otherwise, your code risks in continuing to execute.

$_SESSION['$email']="$email"; there is a dollar sign in ['$email']

It needs to read as ['email'].


Sidenote:

Your present code is open to SQL injection. Use prepared statements, or PDO with prepared statements, they're much safer.


Footnote(s):

  1. In regards to Location:user.html are you sure you want to use an .html file? If you're not instructing Apache to treat .html files as PHP and with no conditional statement to check if the session is set and equal to what you've assigned to it, then anyone can access that file.

  2. I noticed you may be storing passwords in plain text. If this is the case, it is highly discouraged.

It is recommended to use CRYPT_BLOWFISH or PHP 5.5's password_hash() function.
For PHP < 5.5 use the password_hash() compatibility pack.


  • As the chinese proverb goes: "Show a man how to fish, feed him for life."
Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
1

Update this

 $query = mysqli_query($con,"SELECT * FROM user WHERE Email='$email' AND Password='$password' ");

And also correct the session super global from $SESSION to $_SESSION

Bhavya Shaktawat
  • 2,504
  • 1
  • 13
  • 11
0

You are doing an assignment here where you should be doing a comparison:

if($dbemail=$email&&$dbpassword=$password)

Should be

if($dbemail == $email && $dbpassword == $password)

Whitespace makes things more readable.

andrunix
  • 1,704
  • 1
  • 14
  • 23
0

the error in select query you forgot and operator

$query = mysqli_query($con,"SELECT * FROM user WHERE Email='{$email}' AND Password='{$password}' ");

And change the equle operator in if statment with === like this

if($dbemail===$email&&$dbpassword===$password)

I hope that works successfully

andrew
  • 9,313
  • 7
  • 30
  • 61
hamdy
  • 46
  • 4
  • I edited your answer, use 4 spaces for code blocks, and ` for smaller snippets. I didn't fix the spelling errors though – andrew Dec 19 '14 at 12:30
-1

Update your code

<?php
include('config.php');

    session_start();

    $errors=array();

 if ($_SERVER["REQUEST_METHOD"] == "POST"){

    $email = $_POST['email'];
    $password = $_POST['password'];

    if($email&&$password){

        //declare variable
        $query = mysqli_query($con,"SELECT * FROM user WHERE Email='$email' AND Password='$password'");
        $numrows = mysqli_num_rows($query);

        //when user correct input,check the data 
        if($numrows !== 0) {
            while($row=mysqli_fetch_assoc($query)){
                $dbemail=$row['Email'];
                $dbpassword=$row['Password'];
            }
            //if username and password match
            if($dbemail==$email && $dbpassword==$password)
            {
                $_SESSION['$email']="$email";
                header('Location:user.html');
                die();
            }

            else
            {
                $errors['notcorrect'] = "Email or password not correct";
            }
        } 
        //when insert wrong data
        else{
            $errors['notexists'] = "This email doesn't exists";
        }
    }
    //when user didnt enter anything
    else{
        $errors['nothing'] = "Please enter your email and password";
    }
}

?>
Luzan Baral
  • 3,678
  • 5
  • 37
  • 68