Jump to content

[SOLVED] Header Cannot Be Sent Error


fanfavorite

Recommended Posts

I am writing a login script.  The main pages in question are:

The Login Script - login/index.php

The Verification Script (if a user tries to go directly to a password protected page) - login/membersverify.php

The Login Form - login/membersloginform.php

The Members Home Page - index.php

 

Now if I go directly to login/index.php and login, it works correctly.  However if I try to go directly to index.php.  It all works properly until it tries to set the cookie and redirect to the correct page.  The error I get is:

 

Warning: Cannot modify header information - headers already sent by (output started at /home/USER/public_html/cms/login/membersverify.php:1) in /home/USER/public_html/cms/login/index.php on line 18

 

Warning: Cannot modify header information - headers already sent by (output started at /home/USER/public_html/cms/login/membersverify.php:1) in /home/USER/public_html/cms/login/index.php on line 19

 

Line 18 - setcookie

Line 19 - header

 

Now I know that headers must be sent before html, but I don't see why it works going directly to login/index.html and not when you try to go directly to index.html.  My code is below.  Any help would be appreciated.  Thanks!

 

-JC

 

login/index.php

<?
set_include_path('/home/USER/public_html/cms/login');

if ($_GET['logout'])
{
setcookie("login");
}
if ($_POST[loginaccount])
{

include ('membersconfig.php');
$q = mysql_query("select * from Login WHERE Username = '$_POST[username]';");
while($f=mysql_fetch_array($q))
{
$c = "$f[username]";
if ($_POST[password] == $f[Password] AND $_POST[password] != "")
{
	setcookie("login",$c,false,"/",false);
	header("Location: ../index.php");
}
else
{
$error = "Invalid username and/or password.  Please try again.";
include ('membersloginform.php');
}
die;
   }
$error = "Invalid username and/or password.  Please try again.";
include ('membersloginform.php');
}
else
{
include ('membersloginform.php');
}
?>

 

login/membersverify.php

<?
set_include_path('/home/USER/public_html/cms/login');
include('membersconfig.php');
if ($_COOKIE[login] != "")
{
$q = mysql_query("select * from Login WHERE Username = '$_COOKIE[login]';");
while($f=mysql_fetch_array($q))
{
if ($f[username] != $_COOKIE[login])
{
include('index.php');
die;
}
}
} else {
include('index.php');
die;
}
?>

 

login/membersloginform.php

<form name="form1" method="post" action="">
<p class="butheadings">Login:</p>
<p class="center"><input name="username" type="text" size="25" /></p>
<p class="butheadings">Password:</p>
<p class="center"><input name="password" type="password" size="25" /></p>
<p class="center"><input type="submit" alt="Submit" value="Login" name="loginaccount" id="loginaccount" class="formbutton" /><input type="reset" alt="Clear" value="Clear" class="formbutton" /></p>
<p class="mini">Forgot your password?<br><a href="http://www.mydomain.com/cms/login/membersforgotpassword.php">Click Here</a></p>
</form>

 

index.php

?
   include ('login/membersverify.php');
   include ('login/membersconfig.php');
   $User = $_COOKIE[login];
   ob_start();
?>
<html>
...
<?
ob_end_flush();
?>

Link to comment
https://forums.phpfreaks.com/topic/49083-solved-header-cannot-be-sent-error/
Share on other sites

Where would I put that in this script?  I have tried a few combinations, but not sure exactly where they should go in this case because I am not sure where the html is getting set.  When the login form submits, it submits to blank page, in which I don't see where it would call any html before the setcookie. 

Awesome man, this has been driving me crazy.  I actually didn't need to add anything else, just move the ob_start:

 

<?

  include ('login/membersverify.php');

  include ('login/membersconfig.php');

  $User = $_COOKIE[login];

  ob_start();

?>

 

TO

 

<?

  ob_start();

  include ('login/membersverify.php');

  include ('login/membersconfig.php');

  $User = $_COOKIE[login];

?>

 

Thanks again!

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.