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

Link to comment
Share on other sites

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!

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.