Hello Everyone,

Thanks for taking your time to look at my post. I am having a problem with my PHP file which is giving me the following error whenever a header("Location: ") is used and the set_cookie function: Warning: Cannot modify header information and it always says the output was started on the first line, i'm really stuck becuase there is no whitespace either.

Here is my full file:

<?php
$user = $_COOKIE['userloggedin'];
$error = $_GET['error'];
$login = $_POST['login'];
if(isset($login)) {
	$username = strip_tags($_POST['username']);
	$password = strip_tags($_POST['password']);
	if($username == "" and $password == "") {
		header("Location: ?error=both");
	}elseif($username == "") {
		header("Location: ?error=username");
	}elseif($password == "") {
		header("Location: ?error=password");
	}else{
		include("includes/database.php");
		$select = mysql_query("SELECT * FROM users WHERE username = '$username' AND password = '$password'");
		$check = mysql_num_rows($select);
		if($check == "0") {
			header("Location: ?error=incorrect");
		}else{
			$fetch = mysql_fetch_array($select);
			$username_new = $fetch['username'];
			setcookie("userloggedin", $username_new);
			header("Location: account.php");
			exit;
		}
	}
}
if($user != "") {
	header("Location: account.php");
	exit;
}
$title = "Login";
require("templates/header.php");
?>
<DIV class="inner_content guest_home_page">
  <DIV>
  <br />
<br />
&nbsp;&nbsp;&nbsp;<H2>Login</H2></DIV>
<br />
<DIV class="clear"></DIV>
<DIV class="welcome-signup" style="width: 100%">
<DIV class="home_page_intro">
<?php
if(isset($error)) {
?>
<div class="login_alert">
<?php
if($error == "both") {
    echo "You did not type your username or password, please try again.";
}elseif($error == "username") {
    echo "You did not type your password, please try again.";
}elseif($error == "password") {
	echo "You did not type your password, please try again.";
}elseif($error == "incorrect") {
	echo "The details you entered are incorrect, please try again.";
}
?>
</div>
<? }else{ ?>
<P>Login to this site using the form below:</P>
<? } ?>
</DIV>
<DIV class="guest_login_bar" style="width: 100%">
<IMG alt=MEMBERS src="images/members_login_header.gif"> 
<DIV class=leading_line><A 
href="forgot.php">Click here</A> if you 
have forgotten your password. </DIV>
<FORM method="post" action="login.php">
<UL>
  <LI><LABEL for="username">Username</LABEL> <INPUT id="username" class="text" type="text" name="username"></LI>
  <LI><LABEL for="password">Password</LABEL> <INPUT id="password" class="text" type="password" name="password"> 
  <INPUT class="submit imgover" alt="Go" src="images/submit.gif" name="login" type="image" value="Go"></LI>
</UL>
<SPAN class=leading_line>Not a member? <A href="register.php">Click here</A> to join. </SPAN>
</FORM>
</DIV>
</DIV>
</DIV>
<?php require("templates/footer.php"); ?>

Thanks in advance!

Check all of the included files as well and if you're still stumped then you can always use obstart() to buffer any output as a quick fix (and then discard it at the end).

Thanks for your reply chrishea, unfortunatley I have checked all files and added ob_start(); to my page and ob_flush at the end and still no look, i still recieve the header error?

Hi makman99, thanks for replying, i'm using Dreamweaver CS5 and yes every header line is failing, it's driving me crazy!

Thanks.

What does the source come out to be?

There's got to be an empty space somewhere...

"Remember that header() must be called before any actual output is sent,
either by normal HTML tags, blank lines in a file, or from PHP. It is a
very common error to read code with include(), or require(), functions,
or another file access function, and have spaces or empty lines that are
output before header() is called. The same problem exists when using a
single PHP/HTML file."

Maybe try to show all characters (I think notepad++ does this best) and see if there is anything there you can't see.

Another option would be to replace the headers with "exit()" and load each possibility in the browser, and see if there are any characters printed. Open the source in notepad and view all characters.

Thanks again for your response. I tried both your possibilities and still have no solution, there is nothing being outputted with the exit;'s and no hidden characters are being shown, The full code to my file is in the post above and is exactly how it is, something else is that i have the same PHP code with a simpler HTML interface which does not give me the error which is strange and below is the full code of the file that works:

<?php
$user = $_COOKIE['userloggedin'];
$error = $_GET['error'];

if($user != "") {
	header("Location: account.php");
	exit;
}

$login = $_POST['login'];

if(isset($login)) {
	$username = strip_tags($_POST['username']);
	$password = strip_tags($_POST['password']);
	
	if($username == "" and $password == "") {
		header("Location: ?error=both");
	}elseif($username == "") {
		header("Location: ?error=username");
	}elseif($password == "") {
		header("Location: ?error=password");
	}else{
		include("includes/database.php");
		$select = mysql_query("SELECT * FROM users WHERE username = '$username' AND password = '$password'");
		$check = mysql_num_rows($select);
		
		if($check == "0") {
			header("Location: ?error=incorrect");
		}else{
			setcookie("userloggedin", $username);
			header("Location: account.php");
		}
	}
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Login</title>
<style type="text/css">
.login-statement {
	font-family: Arial, Helvetica, sans-serif;
	font-style: normal;
	font-weight: bold;
}
</style>
</head>

<body>
<form action="<?= $_SERVER['PHP_SELF']; ?>" method="post">
<table width="100%" border="0">
  <tr>
    <td height="54" align="center" valign="middle"><small class="login-statement"><?php if(isset($_GET['created'])) { echo "<font color='green'><strong>ACCOUNT CREATED,<br />YOU HAVE BEEN SENT AN EMAIL CONFIRMING YOUR DETAILS TO THE EMAIL ADDRESS PROVIDED...</strong></font><br /><br />"; } ?>USE THE FORM BELOW TO ACCESS YOUR ACCOUNT:</small></td>
  </tr>
  <tr>
    <td align="center"><table width="500" border="0">
      <?php
	  if(isset($error)) {
	  ?>
      <tr>
        <td colspan="2" align="center" valign="middle">
        <font color="#FF0000"><strong>
        <?php
        if($error == "both") {
			  echo "<small>YOU DID NOT ENTER YOUR USERNAME AND PASSWORD!</small>";
		}elseif($error == "username") {
			  echo "<small>YOU DID NOT ENTER YOUR USERNAME!</small>";
		}elseif($error == "password") {
			  echo "<small>YOU DID NOT ENTER YOUR PASSWORD!</small>";
		}elseif($error == "incorrect") {
			  echo "<small>THE DETAILS YOU ENTERED ARE INCORRECT!</small>";
		}
		?>
        </strong></font><br /></td>
        </tr>
		<? } ?>
      <tr>
        <td width="46%" align="right">Username:</td>
        <td width="54%"><label for="textfield">
          <input name="username" type="text" id="username" size="25" maxlength="25" />
        </label></td>
      </tr>
      <tr>
        <td align="right">Password:</td>
        <td><input name="password" type="password" id="password" size="25" /></td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td colspan="2" align="center"><input type="submit" name="login" id="login" value="Login" />
          <input type="reset" name="reset" id="button" value="Clear Form" /></td>
        </tr>
      <tr>
        <td colspan="2" align="center"><br />
          <a href="forgot.php">Forgot your username or password?</a><br />
          <a href="register.php">Don't have an account?</a>
        </td>
      </tr>
    </table>
    </td>
  </tr>
</table>
</form>
</body>
</html>
Member Avatar for diafol

Maybe you have a BOM in your files. Try opening the file in Notepad++ and encoding it as UTF-8 without BOM.

commented: Great answer, solved my problem! +0

OMG, your amazing i love you!

What is BOM and why is it making this happen to my files?

Thanks and will I have to keep saving without BOM or will it now stay like that so i can continue using Dreamweaver?

Again, thank you very very much!

http://en.wikipedia.org/wiki/Byte_order_mark

I think you should be able to convert it back and save it under the original encoding. Change to UTF-8 without BOM and save, and then change it back.

Member Avatar for diafol

Here's Dreamweaver's preferences dialog (my version is MX2004):

Ensure that your BOM is unchecked:

Good call by Ardav. One upvote.

Member Avatar for diafol

Thanks Chris! :)

I had this same exact problem, and I believe it's because I use MS Visual Studio 2010 to edit my PHP files. This was EXASPERATING and driving me CRAZY. And by the way, the problem does NOT happen when hosting php on IIS 7.5 on Windows 7. But it happened when I moved my code to my live LAMP server.

Anyway, saving in UTF-8 without the BOM did the trick!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.