seany02 Posted April 14, 2009 Share Posted April 14, 2009 I know header errors are a common problem, and i have read the header error posts at the top of the forum. However ive not been able to resolve my problem. Below is the code for a log in form which works splendidly, but i cant put any html tags in until after the php which gives me zero control over the form element. When i try and put html tags inbetween start_session and include("... i get a header error message but the form still performs the log in if the username and password are correct. I want to know how to take control of the form so i can move it around. <?php session_start(); // Starts the session. // Includes the db and form info. include("conf.inc.php"); if ($_SESSION['logged'] == 1) { // User is already logged in. header("Location: main.php"); // Goes to main page. exit(); // Stops the rest of the script. } else { if (!isset($_POST['submit'])) { // The form has not been submitted. echo "<form action=\"login.php\" method=\"POST\">"; echo "<table>"; echo "<tr>"; echo "<td colspan=\"2\">Login:</td>"; echo "</tr>"; echo "<tr>"; echo "<td width=\"50%\">Username:</td><td width=\"50%\"><input name=\"username\" size=\"18\" type=\"text\" />"; echo "</tr>"; echo "<tr>"; echo "<td width=\"50%\">Password:</td><td width=\"50%\"><input name=\"password\" size=\"18\" type=\"text\" />"; echo "</tr>"; echo "<tr>"; echo "<td colspan=\"2\"><input type=\"submit\" name=\"submit\" value=\"Submit\"</td>"; echo "</tr>"; echo "</table>"; echo "</form>"; } else { $username = form($_POST['username']); $password = md5($_POST['password']); // Encrypts the password. $q = mysql_query("SELECT * FROM `users` WHERE username = '$username' AND password = '$password'") or die (mysql_error()); // mySQL query $r = mysql_num_rows($q); // Checks to see if anything is in the db if ($r == 1) { // There is something in the db. The username/password match up. $_SESSION['logged'] = 1; // Sets the session. header("Location: main.php"); // Goes to main page. exit(); // Stops the rest of the script. } else { // Invalid username/password. exit("Incorrect username/password!"); // Stops the script with an error message. } } } mysql_close($db_connect); // Closes the connection. ?> Quote Link to comment Share on other sites More sharing options...
taquitosensei Posted April 14, 2009 Share Posted April 14, 2009 session_start has to come before ANY output. Spaces / HTML anything and if you're including a page and you've already started your session you don't have to start it again. Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted April 14, 2009 Share Posted April 14, 2009 you can remove both of these lines as they aren't required... exit(); // Stops the rest of the script. I can't see anything else wrong with the script. Quote Link to comment Share on other sites More sharing options...
seany02 Posted April 14, 2009 Author Share Posted April 14, 2009 i know session_start() has to be at the top i tried closing the php tag after the include statement, insert <center> tag then restart <?php but that doesnt work and caused the following error Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\login.php:5) in C:\xampp\htdocs\login.php Quote Link to comment Share on other sites More sharing options...
runnerjp Posted April 14, 2009 Share Posted April 14, 2009 im presuming this is login.php? try that..sometime spaces cause the error!...also is thius included on any page or are you looking at this directly? <?php session_start(); // Starts the session. // Includes the db and form info. include("conf.inc.php");?> <center>try me</center> <? if ($_SESSION['logged'] == 1) { // User is already logged in. header("Location: main.php"); // Goes to main page. exit(); // Stops the rest of the script. } else { if (!isset($_POST['submit'])) { // The form has not been submitted. echo "<form action=\"login.php\" method=\"POST\">"; echo "<table>"; echo "<tr>"; echo "<td colspan=\"2\">Login:</td>"; echo "</tr>"; echo "<tr>"; echo "<td width=\"50%\">Username:</td><td width=\"50%\"><input name=\"username\" size=\"18\" type=\"text\" />"; echo "</tr>"; echo "<tr>"; echo "<td width=\"50%\">Password:</td><td width=\"50%\"><input name=\"password\" size=\"18\" type=\"text\" />"; echo "</tr>"; echo "<tr>"; echo "<td colspan=\"2\"><input type=\"submit\" name=\"submit\" value=\"Submit\"</td>"; echo "</tr>"; echo "</table>"; echo "</form>"; } else { $username = form($_POST['username']); $password = md5($_POST['password']); // Encrypts the password. $q = mysql_query("SELECT * FROM `users` WHERE username = '$username' AND password = '$password'") or die (mysql_error()); // mySQL query $r = mysql_num_rows($q); // Checks to see if anything is in the db if ($r == 1) { // There is something in the db. The username/password match up. $_SESSION['logged'] = 1; // Sets the session. header("Location: main.php"); // Goes to main page. exit(); // Stops the rest of the script. } else { // Invalid username/password. exit("Incorrect username/password!"); // Stops the script with an error message. } } } mysql_close($db_connect); // Closes the connection. ?> Quote Link to comment Share on other sites More sharing options...
seany02 Posted April 14, 2009 Author Share Posted April 14, 2009 that didnt work either same error message appeared Quote Link to comment Share on other sites More sharing options...
Mchl Posted April 14, 2009 Share Posted April 14, 2009 Ahem... This: header("Location: main.php"); // Goes to main page. has to be before any HTML is output. Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted April 14, 2009 Share Posted April 14, 2009 just get rid of... <center>try me</center> and it should work Quote Link to comment Share on other sites More sharing options...
seany02 Posted April 14, 2009 Author Share Posted April 14, 2009 so what you sayin mchl is that the form has to remain exactly in the top right hand corner of the page or it wont work??? since the php ends after the second header("location: main.php"); line Quote Link to comment Share on other sites More sharing options...
Mchl Posted April 14, 2009 Share Posted April 14, 2009 What I say is that all headers have to be send before any HTML. If you think your code through it is perfectly possible to have any layout you like with headers set in proper place (hint: create HTML first, display later) Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted April 14, 2009 Share Posted April 14, 2009 so what you sayin mchl is that the form has to remain exactly in the top right hand corner of the page or it wont work??? since the php ends after the second header("location: main.php"); line there is no reason you are to be limited to placing HTML in that spot .. with some better planning (and knowhow), you'd have better structured things to avoid such a scenario .. just place your <center> tags in with the form .. if (!isset($_POST['submit'])) { // The form has not been submitted. echo "<center>"; //place all your HTML here .. no problems. echo "<form action=\"login.php\" method=\"POST\">"; echo "<table>"; echo "<tr>"; echo "<td colspan=\"2\">Login:</td>"; Quote Link to comment Share on other sites More sharing options...
seany02 Posted April 14, 2009 Author Share Posted April 14, 2009 thanks mr marcus! great help, didnt know i could put that tag there. cheers for the links aswell mchl. not sure how to mark this thread as complete, if your reading this its solved thanks. Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted April 14, 2009 Share Posted April 14, 2009 bottom left corner is the solved button 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.