Jump to content

Login Error


ecabrera

Recommended Posts

ok so when user get there email or password wrong are they just submit with filling anything out they go to login.php?login_failed

but it gives me

 

This webpage has a redirect loop

Error 310 (net::ERR_TOO_MANY_REDIRECTS): There were too many redirects.

 

<?php
session_start();
$email = $_SESSION['email'];
?>
<?php

$email = mysql_real_escape_string($_POST['email']);
$password = mysql_real_escape_string($_POST['password']);

if(!empty($email) && isset($email) &&!empty($password) && isset($password)){
$password = md5("$password");

require "includes/init/db_con.php"; 
$query = mysql_query("SELECT * FROM users WHERE email = '$email'");
$numrows = mysql_num_rows($query);

if($numrows != 0){

                $row = mysql_fetch_assoc($query);

	$dbemail = $row ['email'];
	$dbpassword = $row ['password'];

	if($dbemail === $email && $dbpassword === $password){
		$_SESSION['email'] = $dbemail;
		header("location: http://localhost/control/home.php");
	}else {
    header('Location: login.php?login_failed');
}
}else {
    header('Location: login.php?login_failed');
}
}else {
    header('Location: login.php?login_failed');
}
?>
<?php require "includes/overall/header.php";?>

<?php
if($_GET['login_failed']){
echo "Login Box will appear with messages";
}
?>


<?php require "includes/overall/footer.php";?>

Link to comment
https://forums.phpfreaks.com/topic/262712-login-error/
Share on other sites

well, your php's logic is kind of weird on one spot. i removed an unnecessary redirect. i also cleaned up your code a bit so it can be read a bit easier.

i commented out the redirect that doesn't make sense. if you don't understand it, feel free to ask what you don't understand.

<?php
session_start();
$email = $_SESSION['email'];

$email = mysql_real_escape_string($_POST['email']);
$password = mysql_real_escape_string($_POST['password']);

if(!empty($email) && isset($email) &&!empty($password) && isset($password))
{
    $password = md5("$password");

    require "includes/init/db_con.php"; 
    $query = mysql_query("SELECT * FROM users WHERE email = '$email'");
    $numrows = mysql_num_rows($query);

    if($numrows != 0)
    {
        $row = mysql_fetch_assoc($query);

        $dbemail = $row ['email'];
        $dbpassword = $row ['password'];

        if($dbemail === $email && $dbpassword === $password)
        {
            $_SESSION['email'] = $dbemail;
            header("location: http://localhost/control/home.php");
        }
        else 
        {
            // Login failed because email or password did not match
            header('Location: login.php?login_failed');
        }
    }
    else 
    {
        // Log in failed because the sessions email did not match a user record?
        #header('Location: login.php?login_failed');
    }
}

require "includes/overall/header.php";

if($_GET['login_failed']){
echo "Login Box will appear with messages";
}

require "includes/overall/footer.php";

Link to comment
https://forums.phpfreaks.com/topic/262712-login-error/#findComment-1346506
Share on other sites

try this,

 

<?php
session_start();
$email = $_SESSION['email'];

$email = mysql_real_escape_string($_POST['email']);
$password = mysql_real_escape_string($_POST['password']);

if(!empty($email) && isset($email) &&!empty($password) && isset($password))
{
    $password = md5($password);

    require "includes/init/db_con.php"; 
    $query = mysql_query("SELECT * FROM users WHERE email = '$email'");
    $numrows = mysql_num_rows($query);

    if($numrows != 0)
    {
        $row = mysql_fetch_assoc($query);

        $dbemail = $row ['email'];
        $dbpassword = $row ['password'];

        if($dbemail === $email && $dbpassword === $password)
        {
            $_SESSION['email'] = $dbemail;
            header("location: http://localhost/control/home.php");
        }
        else 
        {
            // Login failed because email or password did not match
            header('Location: login.php?login_failed');
        }
    }
    else 
    {
        // Log in failed because the sessions email did not match a user record?
        #header('Location: login.php?login_failed');
    }
}

require "includes/overall/header.php";

if($_GET['login_failed']){
echo "Login Box will appear with messages";
}

require "includes/overall/footer.php";


 

 

The problem was with md5 conversion, this should work

 

Password should be $password = md5($password); and not $password = md5("$password");

 

I couldnt see anything else wrong, if problem still exist change

 

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

 

to

 

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

Link to comment
https://forums.phpfreaks.com/topic/262712-login-error/#findComment-1346553
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.