-1

The "Wrong Email and Password" message doesn't show up when I incorrectly fill out the form.

I have ob_start(); in the beginning of my code - does this cause anything? If I erase ob_start();, I have errors of:

Cannot modify header information

How do I fix this?

<?php
if (isset($_POST['login'])) {

    $email = $password = $error = "";
    $email = $_POST['email'];
    $password = md5($_POST['password']);

    if (empty($email) || empty($password)) {
        $error = "Please enter your Email and Password!";
    } else {
        $sql = "SELECT * FROM tbl_users WHERE email='$email' && password='$password'";
        $res = mysql_query($sql);
        $user = mysql_num_rows($res);

        if ($user['blocked'] == 'YES') {
            $error = "Your Account has been blocked. Please contact us for more info";
        } else {
            if ($user == 1) {
                $_SESSION['current_user'] = $email;
                header("Location: edit_profile.php");
                exit();
            } else {
                $error = "Wrong Email and Password";
            }
        }

    }
}
?>
user3071284
  • 6,955
  • 6
  • 43
  • 57

1 Answers1

0

ob_start() turns on output buffering; nothing will display to the screen until you expressly retrieve the contents of the output buffer and stop buffering (e.g., with ob_end_clean() or a similar function). You haven't done that, so you don't get any output, even though you used echo.

You need to tweak that if/else block like this:

        if ($user == 1) {
            $_SESSION['current_user'] = $email;
            header("Location: edit_profile.php");
            exit();
        } else {
            ob_end_clean(); // add this line
            $error = "Wrong Email and Password";
        }

Also, your code has several big problems:

  • Please don't use mysql_*; the mysql_* functions are outdated, deprecated, and insecure. Use MySQLi or PDO instead.
  • You are wide open to SQL injection. Right now, it's trivial to log in as the admin, even without proper permissions.
  • You're using the insecure and broken MD5 to hash your passwords. Don't do this. Use a safe and modern library like bcrypt.
Community
  • 1
  • 1
elixenide
  • 44,308
  • 16
  • 74
  • 100