frijole Posted May 28, 2008 Share Posted May 28, 2008 I am working on a log in script and I am trying to redirect the user back to the front page once they have logged in. This is my log in Script, followed by the attached functions: I would really appreciate some guidance if anyone has seen this before. <?php require_once 'dbConnect.php'; require_once 'functions.php'; $error = ''; if ($_POST['logIn'] == 1) { //if the form has been submitted $userName = $_POST['user']; $pass = $_POST['pass']; $query = "SELECT * FROM users WHERE user_name='$userName' AND user_pass='$pass'"; $result = mysql_query($query) or die("connection error."); if (empty($userName) || empty($pass)) { //are any of the fields empty $error = "All fields must be filled out.";} elseif (mysql_num_rows($result) != 1) { //if the username and password combo are not valid $error = "Invalid username, password combo.";} else { //log in was successful if (keepLogged ==1) { $year = 525600 + time(); setcookie(thinksnack, 1, $year); } $_SESSION['userName'] = $userName; $_SESSION['loggedIn'] = 1; header('Location: http://www.thinksnack.com/'); } } showLogInForm ($error); ?> <?PHP function showRegisterForm ($error) { echo $error; ?> <fieldset> <legend>Register</legend> <table> <form name="input" action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post"> <tr> <td>Username: </td> <td><input type="text" name="user"></td> </tr> <tr> <td>Password:</td> <td><input type="password" name="pass"></td> </tr> <tr> <td>Password:</td> <td><input type="password" name="rePass"></td> </tr> <tr> <input type="hidden" name="register" value="1"> <td><input type="submit" value="Submit"></td> </tr> </form> </table> </fieldset> <?PHP }?> <?PHP function showLogInForm ($error) { echo $error; ?> <table> <form name="input" action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post"> <tr> <td>username </td><td>password</td><td>stay logged in<input type="checkbox" name="keepLogged" value="1"></td> </tr><tr> <td><input type="text" name="user"></td> <td><input type="password" name="pass"></td> <input type="hidden" name="logIn" value="1"> <td><input type="submit" value="Submit"></td> </tr> </form> </table> <?php } ?> Link to comment https://forums.phpfreaks.com/topic/107570-solved-header-problem-quotcannot-modify-header-informationquot/ Share on other sites More sharing options...
Prismatic Posted May 28, 2008 Share Posted May 28, 2008 it's because you're setting a cookie, which sends header information out to the browser. You can get around this by buffering the output, <?php # Buffer the output ob_start(); require_once 'dbConnect.php'; require_once 'functions.php'; $error = ''; if ($_POST['logIn'] == 1) { $userName = $_POST['user']; $pass = $_POST['pass']; $query = "SELECT * FROM users WHERE user_name='$userName' AND user_pass='$pass'"; $result = mysql_query($query) or die("connection error."); if (empty($userName) || empty($pass)) { $error = "All fields must be filled out."; } elseif (mysql_num_rows($result) != 1) { $error = "Invalid username, password combo."; } else { if (keepLogged ==1) { $year = 525600 + time(); setcookie(thinksnack, 1, $year); } $_SESSION['userName'] = $userName; $_SESSION['loggedIn'] = 1; header('Location: http://www.thinksnack.com/'); } } showLogInForm ($error); # Dump the buffer ob_end_flush(); ?> Link to comment https://forums.phpfreaks.com/topic/107570-solved-header-problem-quotcannot-modify-header-informationquot/#findComment-551376 Share on other sites More sharing options...
frijole Posted May 28, 2008 Author Share Posted May 28, 2008 that was it, thanks a lot. Link to comment https://forums.phpfreaks.com/topic/107570-solved-header-problem-quotcannot-modify-header-informationquot/#findComment-551380 Share on other sites More sharing options...
darkfreaks Posted May 28, 2008 Share Posted May 28, 2008 can you press hte topic solved button thanks Link to comment https://forums.phpfreaks.com/topic/107570-solved-header-problem-quotcannot-modify-header-informationquot/#findComment-551384 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.