Forum Moderators: coopster

Message Too Old, No Replies

Warning: Cannot modify header information - headers already sent by

Receiving an error when trying to set a cookie (I believe)

         

compusolvusa

5:36 pm on Feb 3, 2005 (gmt 0)



I'm a newbie at all of this, but am loving it! Well, until I started getting the below error message and I can't figure out how to solve it! Any help would be greatly appreciated! Remember when you send ideas and suggestions, that I'm still pretty new to this, so you may want to be detailed so I can understand! Thanks!

Here's the error messages I'm receiving:
Warning: Cannot modify header information - headers already sent by (output started at /home/compuso/public_html/midwest/index_new.php:39) in /home/compuso/public_html/midwest/logon/logon.inc on line 83

Warning: Cannot modify header information - headers already sent by (output started at /home/compuso/public_html/midwest/index_new.php:39) in /home/compuso/public_html/midwest/logon/logon.inc on line 86

The errors are received after performing the following:
1. In the test logon screen (http://www.midwesthorseman.com/index_new.php?menu=logon) I fill in the user (webmaster) and password (test) and then click on the "login" button.

The following programs are listed in the error:

Logon.inc (Included module in the main program (index_new.php), used to logon, validate logon, and set timed cookie)
<?
if (isset($_POST['submit']))
{
ob_start();

require_once ('/home/compuso/public_html/midwest/includes/mysql_connect.php');

function escape_data ($data)
{
global $dbc;
if (ini_get('magic_quotes_gpc'))
{
$data = stripslashes($data);
}
return mysql_real_escape_string($data, $dbc);
}

$message = NULL;
if (empty($_POST['logon_user']))
{
$u = FALSE;
$message .= '<p>You forgot to enter your username!</p>';
}
else
{
$u = escape_data($_POST['logon_user']);
}

if (empty($_POST['logon_password']))
{
$p = FALSE;
$message .= '<p>You forgot to enter your password!</p>';
}
else
{
$p = escape_data($_POST['logon_password']);
}

if ($u && $p)
{
// If everything's OK.
$query = "SELECT logon_status, logon_name_first FROM logon_table WHERE logon_user='$u' AND logon_password='$p'";
$result = @mysql_query ($query);
$row = mysql_fetch_array ($result, MYSQL_NUM);

// If logon_user and logon_password are found in database then process
if ($row)
{
// Set logon variables
$logon_user = $row[logon_user];
$PHP_AUTH_USER = $logon_user;
$logon_password = $row[logon_password];
$logon_date = date("Ymd");
$logon_time = date("h:i:s");
$logon_ip_address = $REMOTE_ADDR;
$logon_host = $_SERVER['HTTP_HOST'];
$logon_program_name = 'logon.inc';

// Check to see if a cookie exists - delete it if it does
if (isset($_COOKIE['midwesthorseman_logon_user']))
{
setcookie ('midwesthorseman_logon_user', '', time()-300, '/', '', 0);

$logon_message = 'logoff';
// Add logoff information to the logon_db
$result_logoff = mysql_query("INSERT INTO logon_db (logon_user, logon_password, logon_date, logon_time, logon_ip_address, logon_host, logon_program_name, logon_message) VALUES ('$logon_user', '$logon_password', '$logon_date', '$logon_time', '$logon_ip_address', '$logon_host', '$logon_program_name', '$logon_message')");
if(!$result_logoff) die ('Database error ($result_logoff): ' .mysql_error());
}

// Set logon_status to active on member_table
$result_update = mysql_query("UPDATE member_table SET member_logon_status = 'y' where logon_user = '$logon_user'");
// if(!$result_update) die ('Database error ($result_update): ' .mysql_error());

// Add logon information to the logon_db
$logon_message = 'logon';
$result_add = mysql_query("INSERT INTO logon_db (logon_user, logon_password, logon_date, logon_time, logon_ip_address, logon_host, logon_program_name, logon_message) VALUES ('$logon_user', '$logon_password', '$logon_date', '$logon_time', '$logon_ip_address', '$logon_host', '$logon_program_name', '$logon_message')");
if(!$result_add) die ('Database error ($result_add): ' .mysql_error());

// Set the cookie and redirect
$expires_10minute = time()+600;
$expires_1hour = time()+3600;
$expires_8hour = time()+28800;
setcookie ('midwesthorseman_logon_user', $logon_user, $expires_1hour, '/', '', 0);

// Set header to direct where to go next
header ("Location: ["...] . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . "?menu=logon");
// exit();

}
// logon_user and logon_password was not found in database
else
{
$message = '<p>The username and password entered do not match those on file.</p>';
}

mysql_close();
}
else
{
$message .= '<p>Please try again.</p>';
}
ob_end_flush();
}
?>

<TABLE>
<TR WIDTH="100%">

<!--- Column One --->
<TD WIDTH="25%">
<P>
<?
if (isset($_COOKIE[midwesthorseman_logon_user]))
{
?>
logout
<?
}
else
{
?>
login
<?
}
?>
</P>
<P>
Forgot your password?
</P>
</TD>

<!--- Column Two --->
<TD WIDTH="40%" VALIGN="center" BGCOLOR="#FFFF99">
<?

if (isset($message))
{
?>
<font color="red">$message</font>
<?
}
?>
<!---
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
--->
<form action="<?=$_SERVER['PHP_SELF']?>?menu=logon" method="post">
<fieldset>
<legend>
Enter your information in the form below:
</legend>
<p>
<b>User Name:</b>
<input type="text" name="logon_user" size="10" maxlength="20" value="<?php if (isset($_POST['logon_user'])) echo $_POST['logon_user'];?>" />
</p>
<p>
<b>Password:</b>
<input type="password" name="logon_password" size="20" maxlength="20" />
</p>
<div align="center">
<input type="submit" name="submit" value="Login" />
</div>
</fieldset>
</form>
<!-- End of Form -->
</TD>

<!--- Column Three --->
<TD WIDTH="35%">
advertisement
</TD>
</TR>
</TABLE>

Index_new.php (Main program - To save space I've cut out much of the code and just left the areas that could be affected by the error)
<?
// Check logon information
// Include logon check program
include "logon/loggedin.php";
// $logon_user = $PHP_AUTH_USER;

// Check for popup cookie
if(!isset($_COOKIE['brad_paisley_contest']))
{
// No cookie found Set passed variable and set cookie for 8 hours
$first_time_here = 'y';
$expires_10minute = time()+600;
$expires_1hour = time()+3600;
$expires_8hour = time()+28800;
$expires_24hour = time()+86400;
setcookie('brad_paisley_contest','midwesthorseman.com_brad_paisley_contest',$expires_8hour,'','',0);
}
else
{
$first_time_here = 'n';
}

// Set Header title
$header = 'midwesthorseman_com_logo.gif';

// Frame Background Color
$bgcolor = '#99CCFF';
?>

<HTML>
<HEAD>
<TITLE>
MidWestHorseman.com - Your local HORSE CALENDAR web site!
</TITLE>

<!-- Meta Data -->
<meta name="keywords" content="horse,cowboy,equestrian,horseman,animal,equine,local events,rochester,minnesota,singles,rochester dating,dating rochester,single,date,friend,online friendship,chat,adult">

<meta name="description" content="Your favorite equestrian web site, serving the midwest and SE Minnesota areas">

<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1">

<!-- Style Definitions -->
<style type="text/css">
.menu_bar:link { color: #336633; font-family: verdana; font-size: 14px; font-weight: bold; text-decoration: none }
.menu_bar:visited { color: #336633; font-family: verdana; font-size: 14px; font-weight: bold; text-decoration: none }
.menu_bar:hover { color: #FF6600; font-family: verdana; font-size: 15px; font-weight: bold; text-decoration: none }

.calendar_TD {border-style: solid; border-width: thin; margin: 0px; padding: 0px; border-color: #'.$calendar_border_color.'; font-weight: normal; background-color: #'.$calendar_bg_color.'}

<?
// include "calendar/calendar_style.inc";
?>
</style>

<!-- Style Definitions for FAQ -->
<link rel="stylesheet" type="text/css" href="faq/style.css">

<!-- Javascript Functions -->
<SCRIPT TYPE="text/javascript">
function openwindow(url,screen,w,h)
{
window.open(url,screen,"toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width="+w+", height="+h);
}

function popupwindow(url,screen,w,h)
{
window.open(url,screen,"toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, width="+w+", height="+h);
}

</SCRIPT>

<!-- Javascript for Menu Buttons -->
<script src="includes/menu/menuscript.js" language="javascript" type="text/javascript">
</script>
<link rel="stylesheet" type="text/css" href="includes/menu/menustyle.css" media="screen, print" />

</HEAD>

<!-- ---------------------------------------------------------- -->
<!-- Popup if this is the first time here and cookie still set -->
<!-- ---------------------------------------------------------- -->
<?
if ($first_time_here == 'y')
{
$first_time_here = 'n';
?>
<BODY onLoad="popupwindow('http://www.midwesthorseman.com/popups/20050116/20050116.html', 'myWindow',
'300', '525')">
<?
}
else
{
?>
<BODY>
<?
}
?>

.
.
. (cut)
.
.

<!--------------->
<!-- Body Area -->
<!--------------->
<TD WIDTH="85% ALIGN="center" VALIGN="top">
<TABLE WIDTH="100%" HEIGHT="95%" BORDERCOLOR="#76A8DB" BORDER="2">
<TR>
<?
// Check logon information
if ($menu == "logon")
{
// Program tracking sent to the logon_db
// $logon_program_name = $PHP_SELF;
$logon_program_name = 'index.php?menu=logon';
include "logon/logon_program_tracking.php";

// Include logon check program
include "logon/logon.inc";
// $menu = 'home';
}

.
.
. (cut)
.
.
<!-- ------ -->
<!-- Footer -->
<!-- ------ -->
<TABLE WIDTH="100%" HEIGHT="5%" BGCOLOR="<?=$bgcolor?>">

<tr>
<td height="5">
<img src="spacer.gif" width="1" height="5">
</td>
</tr>

<TR>
<TD ALIGN="center">
<FONT SIZE="2" COLOR="black">
Copyright ©2004-2005 MidWestHorseman.com - All Rights Reserved
</FONT>
</TD>
</TR>
</TABLE>
</BODY>
</HTML>

HeadBut

6:02 pm on Feb 3, 2005 (gmt 0)

10+ Year Member



I'm new at this, so consider yourself warned!. I didn't read all your code, but....

If you have a header error, you must either change the configuration of your server (even dynamically/ but more difficult) or set the cookie before you have any output (best/easiest).

Be sure you have your code at the top of the page. Even a few returns at the top of the page can start the sending of your header, so make sure "<?php" is at the very top. And set your cookie as soon as you can and before any other output. If this is a problem there is a directive you can set to hold the sending of the header.... just can remember it now, ..... lookin!

Good luck!

BjarneDM

5:22 am on Feb 4, 2005 (gmt 0)

10+ Year Member



Your problem is, that headers has to be the very first thing generated/sent by your php script. Just as soon as the first line of html has been generated and sent it's too late to send headers.

You've got two options:
1) fix your script to send the cookies at the correct time
2) buffer your output.

You can find all of the pertinent information by starting on this page:
[php.net...]

And: the online php manual is your very best friend.