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
Share on other sites

Yeah,  I've tried it in IE8 and it tries to load the page for about 4 minutes, then i just cancelled it.  I'm wondering if the $_POST['submit'] is still set when the processform.php redirects to login.php.  However, I would think it would be cleared out.

Link to comment
Share on other sites

I believe youre right.  However, I'm not sure why it runs the code in the isset($_POST['submit']) if statement in login.php after the redirect in processform.php.  Shouldn't the $_POST array be empty at that point and the form displayed?

Link to comment
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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.