Ashoar Posted April 13, 2009 Share Posted April 13, 2009 Ok, please do not link me to the sticky about this, i have already read it and tried what was said and nothing worked. This login form worked perfectly until i started running it on php 5.2.9. When i login i get this error message: Warning: Cannot modify header information - headers already sent by (output started at /home/asilentd/public_html/forum/login.php:5) in /home/asilentd/public_html/forum/login.php on line 33 As said, i tried what the sticky said about these header errors but nothing has worked, and it has only started doing this since i upgraded to php 5.2.9. I moved the session start right up the top of the page as that was causing another error, and it fixed after moving it there. Here is the code: <?php session_start(); ?> <div id="wrapper"> <?php include "header.php"; include 'config.php'; if(isset($_POST['login'])) { $username = trim(addslashes($_POST['username'])); $password = md5(trim($_POST['password'])); $query = mysql_query("SELECT * FROM Users WHERE Username = '$username' AND Password = '$password' LIMIT 1") or die(mysql_error()); $row = mysql_fetch_array($query); // now we check if they are activated if(mysql_num_rows($query) > 0) { if($row['Activated'] > 0) { $_SESSION['s_logged_n'] = 'true'; $_SESSION['s_username'] = $username; $_SESSION['s_name'] = $row['Name']; $insertuser="INSERT INTO online(username) values('$username')"; mysql_query($insertuser) or die("Could not login insert user"); header("Location: index.php"); } else { echo ' <html> <head> <title>Login</title> <link href="style.css" rel="stylesheet" type="text/css"> </head> <body> <div id="error"><p>Sorry, you must activate your account first. Please check your email for the email.</p> <p>Didn'."'".'t get your validation email? <a href="resend.php">Click here</a> to resend the validation email.</p></div> </body> </html> '; } } else { echo ' <html> <head> <title>Login</title> <link href="style.css" rel="stylesheet" type="text/css"> </head> <body> <div id="error"><p>There was an error processing your login, it appears that your username and/or password was incorrect. Please try again.</p> <p>Didn'."'".'t get your validation email? <a href="resend.php">Click here</a> to resend the validation email.</p> </div> </body> </html> '; } } else { ?> <html> <head> <title>Login</title> <link href="style.css" rel="stylesheet" type="text/css"> </head> <body> <div id="wrapper"> <table class='maintable'> <tr class='headline'><td width=100%>Login</td></tr> <form method="post" action="<?= $_SERVER['PHP_SELF'] ?>"> <tr class='mainrow'><td><p>Username:<br> <input name="username" type="text" class="textBox" id="username"> <tr class='mainrow'><td><p>Password:<br> <input name="password" type="password" class="textBox" id="password"> </p> <p> <input name="login" type="submit" class="textBox" id="login" value="Submit"> </p> </form> <p>Didn't get your validation email? <a href="resend.php">Click here</a> to resend the validation email.</p> <p>Need an account? <a href="register.php">Click here</a> to register, it's completely free! </p> </div> </table> </div> </body> </html> <? } mysql_close($l); ?> <?php include "footer.php"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/153832-solved-redirect/ Share on other sites More sharing options...
ToonMariner Posted April 13, 2009 Share Posted April 13, 2009 add ob_start(); to the beginning of your script and ob_end_flush(); at the end. Quote Link to comment https://forums.phpfreaks.com/topic/153832-solved-redirect/#findComment-808475 Share on other sites More sharing options...
ratcateme Posted April 13, 2009 Share Posted April 13, 2009 do you understand how headers work and how php handles them? headers are sent at the top of a HTTP response. so you cannot send headers after any output has been sent because they had to come first now if you read the error output is started on line 5 "<div id="wrapper">" looking at your code i don't quite see how this line of code works with the rest of the script you need. anyway to i would recommend moving the login checker part of the script to the top on the lines after session_start() that way you would have no problems Scott. Quote Link to comment https://forums.phpfreaks.com/topic/153832-solved-redirect/#findComment-808476 Share on other sites More sharing options...
Ashoar Posted April 13, 2009 Author Share Posted April 13, 2009 The wrapper is just adding everything of the page inside of a centered alignment with a certain width. Well i do rat, as this entire code worked perfect until i upgraded to a higher version of php which is why it has confused me since it did work fine before. Anyway i have it working now, it seems to work just by adding the session start to the header.php page that is included at the top of each page. Quote Link to comment https://forums.phpfreaks.com/topic/153832-solved-redirect/#findComment-808479 Share on other sites More sharing options...
Dathremar Posted April 13, 2009 Share Posted April 13, 2009 get rid of the <div id="wrapper"> on the top of the page. As i noticed u have another <div id="wrapper"> in the HTML little down in the script. I really don't understand the need of the 1st div. And if this page is wrapped in another one, then put that div in the wrapping page. Quote Link to comment https://forums.phpfreaks.com/topic/153832-solved-redirect/#findComment-808485 Share on other sites More sharing options...
ToonMariner Posted April 13, 2009 Share Posted April 13, 2009 not changed then it should fail in older versions of php. @ratcateme has alluded to the problems your are encountering but without refactoring your code the simplest solution is to use the ob_start() / ob_end_flush() solution I suggested above. alternatively you can go through all your code - ensure all your datachecks/validation redirects etc are done BEFORE you start the output - as soon as you start outputting markup (including whitespace) all headers must have been sent. Quote Link to comment https://forums.phpfreaks.com/topic/153832-solved-redirect/#findComment-808534 Share on other sites More sharing options...
Ashoar Posted April 13, 2009 Author Share Posted April 13, 2009 The main problem was as Rat said. When i had originally swapped them around they were not right with the session start info. I have gone through now and done that, also with this version of php it did require session start to be at the very top of any page. It is all fixed now. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/153832-solved-redirect/#findComment-808562 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.