Jump to content

header error when i include html


seany02

Recommended Posts

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.

?>

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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.
?>

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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>";

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.