Jump to content

The page isn't redirecting properly


samona

Recommended Posts

I keep getting a 'The page isn't redirecting properly error on Firefox.  Anyone have an idea?  I think it has something to do with the header() function, but I can't seem to pinpoint it.  Code for the two files are below.

 

login.php

<?php

require_once('./lib/myform.class.php');
require_once('./functions.php');

$page = 'Login Page';
$myStyles = './css/mystyles.css';	


if (isset($_POST['submit'])) {

	$error_ar = array();
	$values_ar = array();


	$username = sanatize($_POST['username']);
	$password = sanatize($_POST['password']);

	if (empty($username)) {

		$error_ar['username'] = 'You must enter your username';
		//echo $arr_error['username'];			
	}

	else {

		$values_ar['username'] = $_POST['username'];
	}

	if (empty($password)) {

		$error_ar['password'] = 'You must enter a password';			
	}

}

if (count($error_ar) == 0) {

	session_start();

	$_SESSION['username'] = $username;
	$_SESSION['password'] = md5($password);


	header('Location: processform.php');
	exit();
}


?>


<html>
<head>		
  <title><?php print $page ?></title>
  <link href="<?php print $myStyles ?>" rel="stylesheet" type="text/css">
  
</head>

<body>
<div id="container">
	<div id="form">

	<?php 


		$f = new myForm($error_ar);	


		$f->beginForm("login.php");	

		$f->beginFieldset(array('class'=>'form'));
		$f->addLegend($page);	
		$f->beginList();	

		$f->beginListItem();
		$f->addLabel('username', 'Username');
		$f->addInput('text', 'username', $values_ar['username'], array('class'=>'text', 'id'=>'username'));
		$f->endListItem();

		$f->beginListItem();
		$f->addLabel('password', 'Password');
		$f->addPassword();
		$f->endListItem();

		$f->endList();	
		$f->endFieldset();

		$f->beginFieldset(array('class'=>'form'));
		$f->addLegend('Submit');
		$f->beginList();
		$f->beginListItem();
		$f->submitButton('Login', array('class'=>'submit'));
		$f->endListItem();
		$f->endList();
		$f->endFieldset();

		echo $f->printForm();

	?>
	</div>
</div>
</body>
</html>

 

processform.php


<?php

session_start();

require_once('./lib/mysqldb.class.php');



if (!isset($_SESSION['username'])) {
	header('Location: login.php');
	exit();
}

$db = new MySQLDB();	

$username =  $_SESSION['username'];
$password =  $_SESSION['password'];

if ($db->authenticateUser($username, $password)) {

	echo "SUCCESS!!!";
}

else {



	$_SESSION = array();		
	session_destroy();		

	header('Location: login.php');


}






?>

Link to comment
https://forums.phpfreaks.com/topic/235631-the-page-isnt-redirecting-properly/
Share on other sites

Okay so in login.php, you verify that $error_ar isn't empty, every time reglardless of whether they submitted the form. Assuming it's not you then redirect them to processform.php. In processform.php you then attempt to authenticate them, based on the username and password you stored in the session in login.php.

 

If they aren't authenticated you then redirect them back to login.php, which doesn't enter the validation because $_POST['submit'] is not set - which also means that $error_ar is not defined. So when you enter the $error_ar count check afterwards, it defaults to 0 and you redirect them back to processform.php.. Which still has the session values and then results in a redirect back to login.php, and so on.

 

This logic needs correcting; perhaps by moving the error check in login.php to within the submit button condition's code? Also I'm guessing that either your authenticate method doesn't work, or you're providing invalid login details..

 

Edit

 

Why are you splitting the logic and forcing the redirect like this anyway? Why not just perform it all within the same request, and then redirect once successful?

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.