samona Posted May 5, 2011 Share Posted May 5, 2011 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'); } ?> Quote Link to comment Share on other sites More sharing options...
samona Posted May 6, 2011 Author Share Posted May 6, 2011 Anyone? Quote Link to comment Share on other sites More sharing options...
fugix Posted May 6, 2011 Share Posted May 6, 2011 well your header syntax is correct...so am i correct in saying that it is trying to redirect but it just isnt working? have you tried it on another browser? Quote Link to comment Share on other sites More sharing options...
samona Posted May 6, 2011 Author Share Posted May 6, 2011 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. Quote Link to comment Share on other sites More sharing options...
Adam Posted May 6, 2011 Share Posted May 6, 2011 That firefox error suggests you have an infinite loop in your redirect. Quote Link to comment Share on other sites More sharing options...
samona Posted May 6, 2011 Author Share Posted May 6, 2011 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? Quote Link to comment Share on other sites More sharing options...
Adam Posted May 6, 2011 Share Posted May 6, 2011 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? Quote Link to comment Share on other sites More sharing options...
samona Posted May 6, 2011 Author Share Posted May 6, 2011 Thanks! You were right. That was the issue. I'll take your advice and put it all in the same request. thanks again! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.