Jump to content

Login Infinate Redirect Loop


darkflaw

Recommended Posts

I am searching my login for bugs and I have found two. The first is when I login with the remember me box checked it creates an infinite redirect loop between the homepage and the login page. The second bug is if you login with a banned account but check off remember me it creates another infinite redirect loop like the problem above. I can't figure out how to solve this problem and I am looking for a little push in the right direction.

 

Here are the files that are essential to the login and homepage. I have left out the html and things not relevant to the problem.

 

functions.php

<?php
//Check Login
function loggedin()
{
	if (isset($_SESSION['username']) || isset($_COOKIE['username']))
	{
		$loggedin = TRUE;
		return $loggedin;
	}
}
?>

 

home.php

<?php
error_reporting(E_ALL & ~E_NOTICE);
include 'login/functions.php';

if (!loggedin())
{
header("Location: login/index.php");
exit();
}

$user = $_SESSION['username'];
?>

 

login.php

<?php
error_reporting(E_ALL & ~E_NOTICE);
include 'functions.php';
if (loggedin())
{
	header("Location: /index.php");
	exit();
}

if ($_POST['login'])
{
$username = mysql_real_escape_string($_POST['username']);
$password = $_POST['password'];
$rememberme = $_POST['rememberme'];

if (!empty($username) && !empty($password))
{
		$login = mysql_query("SELECT * FROM users WHERE username='$username' LIMIT 1");

		while($row = mysql_fetch_array($login))
		{
		$db_password = $row['password'];
		}
		if (md5($password)==$db_password)
			$loginok = TRUE;
		else
			$loginok = FALSE;

		if ($loginok)
		{
			if ($rememberme=="on")
				setcookie("username", $username, time()+2678400);
			else if ($rememberme=="")
				$_SESSION['username']=$username;

			if (!banned())	
			{
				header("Location: /index.php");
				exit();	
			}
			else
			{
				$username = "";
				$password = "";
				$error = TRUE;
				$message = "Account is banned.";
				$image = "delete.png";
					session_start();
					session_destroy();
					setcookie("username", "", time()-2678400);
			}
		}
		else
		{
			$error = TRUE;
			$message = "Verify your details.";
			$image = "exclamation.png";
		}
}
else
{
	$error = TRUE;
	$message = "Please fill all fields.";
	$image = "error.png";
}
}
else
{
$username = "";
$password = "";
}	
?>

Link to comment
https://forums.phpfreaks.com/topic/175448-login-infinate-redirect-loop/
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.