Cannot modify header information and SetCookie Function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jackponte
Forum Newbie
Posts: 11
Joined: Thu Feb 04, 2010 5:04 pm

Cannot modify header information and SetCookie Function

Post by jackponte »

Hi,
I'm trying to implement a nice code I found to be able to login from a page and redirect to another after authentication.

The PHP code works by itself but when I add it to the HTML page it give the error:

Warning: Cannot modify header information - headers already sent by (output started at C:\Documents and Settings\jack.ponte\Desktop\Website\index2.php:5) in C:\Documents and Settings\jack.ponte\Desktop\Website\index2.php on line 105

Warning: Cannot modify header information - headers already sent by (output started at C:\Documents and Settings\jack.ponte\Desktop\Website\index2.php:5) in C:\Documents and Settings\jack.ponte\Desktop\Website\index2.php on line 106

Warning: Cannot modify header information - headers already sent by (output started at C:\Documents and Settings\jack.ponte\Desktop\Website\index2.php:5) in C:\Documents and Settings\jack.ponte\Desktop\Website\index2.php on line 110

The lines point to the setcookie function but I've tried a few thing and it does not help.

- No spaces in the page
- ob_start()

I tried just the PHP part in new page and it works.

How would I go about fixing the code or getting around the erro? Any help would be appreciated.

Thanks.

Code: Select all

 
<html>
<head>
<title>Rogers ESC RMA Portal</title>
<link rel=stylesheet type="text/css" href="style.css">
<style type="text/css">
<!--
.style3 {
    font-family: Arial, Helvetica, sans-serif;
    font-weight: bold;
}
.style4 {font-family: Arial, Helvetica, sans-serif; font-weight: bold; font-size: small; }
-->
    </style>
</head>
 
<body bottommargin="0" leftmargin="0" rightmargin="0" topmargin="0" background="img/background.jpg">
 
<table height="123" width="100%" cellpadding="0" cellspacing="0" border="0">
<tr valign="top">
<!----- insert logo below ------------------------------------------>
<td><img src="img/backer.jpg" width="574" height="123"></td>
<!------------------------------------------------------------------>
<td width="75%" background="img/topbg.jpg"><p><img src="img/logo_Rogers.jpg" width="300" height="100" align="right"></p>
  </td>
</tr>
</table>
 
<table height="40" width="100%" cellpadding="0" cellspacing="0" border="0">
<tr valign="top">
<td width="75" background="img/topbar.jpg">&nbsp;</td>
</tr>
</table>
 
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr valign="top">
 
<td width="201"><p><BR>
<!----- menu, insert links below ------------------------------------------>
&nbsp;&nbsp;&raquo;&nbsp;<a href="http://www.rogers-esc.com/index.html">Home</a><BR>
<img src="img/menubar.jpg" width="201" height="11" border="0" alt=""><BR>
&nbsp;&nbsp;&raquo;&nbsp;<a href="form.html">Process RMA </a><BR>
<img src="img/menubar.jpg" width="201" height="11" border="0" alt=""><BR>
&nbsp;&nbsp;&raquo;&nbsp;<a href="http://www.rogers-esc.com">Submit</a><BR>
<img src="img/menubar.jpg" width="201" height="11" border="0" alt=""><BR>
&nbsp;</p>
<p align="center"><img src="img/login.jpg" width="102" height="102" align="middle"></p>
<?php
ob_start();
// Connects to your Database
mysql_connect("localhost", "root", "escrma") or die(mysql_error());
mysql_select_db("rma_portal") or die(mysql_error());
//Checks if there is a login cookie
 
if(isset($_COOKIE['ID_my_site']))
//if there is, it logs you in and directes you to the members page
{
$username = $_COOKIE['ID_my_site'];
$pass = $_COOKIE['Key_my_site'];
$check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error());
while($info = mysql_fetch_array( $check ))
{
if ($pass != $info['Password'])
{
}
else
{
ob_start();
header("Location: form.html");
ob_end_flush();
}
}
}
//if the login form is submitted
if (isset($_POST['submit']))
{
// makes sure they filled it in
if(!$_POST['username'] | !$_POST['pass']) {
die('You did not fill in a required field.');
}
// checks it against the database - Email
if (!get_magic_quotes_gpc()) {
$_POST['username'] = addslashes($_POST['username']);
}
$check = mysql_query("SELECT * FROM users WHERE username = '".$_POST['username']."'")or die(mysql_error());
//Gives error if user doesnt exist
$check2 = mysql_num_rows($check);
if ($check2 == 0) {
die('That user does not exist in our database. <a href=add.php>Click Here to Register</a>');
}
while($info = mysql_fetch_array( $check ))
{
$_POST['pass'] = stripslashes($_POST['pass']);
$info['Password'] = stripslashes($info['Password']);
$_POST['pass'] = md5($_POST['pass']);
 
//gives error if the password is wrong
if ($_POST['pass'] != $info['Password']) {
die('Incorrect password, please try again.');
}
else
{
// if login is ok then we add a cookie
$_POST['username'] = stripslashes($_POST['username']);
$hour = time() + 3600;
setcookie(ID_my_site, $_POST['username'], $hour);
setcookie(Key_my_site, $_POST['pass'], $hour);
 
//then redirect them to the members area
ob_start();
header("Location: form.html");
ob_end_flush();
 
}
}
}
else
{
// if they are not logged in
?>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
<table border="0">
<tr><td>Username:</td><td>
<input type="text" name="username" maxlength="15">
</td></tr>
<tr><td>Password:</td><td>
<input type="password" name="pass" maxlength="15">
</td></tr>
<tr><td colspan="2" align="right"><center>
<input type="submit" name="submit" value="Login">
</center>
</td></tr>
</table>
</form>
<?php
}
?>
</td>
<td width="100%">
<table width="100%" cellpadding="10" cellspacing="0" border="0">
<tr valign="top">
<td>
<!----- content, insert text and content below ------------------------------------------>
<h2>Welcome to Rogers Wireless RMA Portal </h2>
<p align="left">Thank you for visiting the Rogers RMA Portal - your one stop solution for all of your device return needs.</p>
<p align="left">If you are a corporate customer and you have a defective device that is still under warranty, you can quickly submit the RMA (<em>Returned Merchandise Authorization</em>) information through the portal and go back to your day. Rogers will process your request and your replacement device will be on it's way to you in no time. </p>
<p align="left">It's quick and easy to use. Follow these steps to submit your RMA.</p>
<p align="left">&nbsp;   &nbsp; 1. Before you start, make sure that you have all the information that you'll need. This includes: </p>
<p align="left">&nbsp;   &nbsp; &nbsp; &nbsp;   &nbsp; a) your company and contact information, the make and model of the device,</p>
<p align="left">&nbsp;   &nbsp; &nbsp; &nbsp;   &nbsp; b) the IMEI # for the device </p>
<p align="left">&nbsp;   &nbsp; &nbsp; &nbsp;   &nbsp; c) a brief description of the issue with the device</p>
<p align="left">&nbsp;   &nbsp; &nbsp; &nbsp;   &nbsp; d) a description of any steps taken to try to resolve the issue  </p>
<p align="left">&nbsp;   &nbsp; &nbsp; &nbsp;   &nbsp; e) Shipping and contact information </p>
<p align="left">&nbsp;   &nbsp; 2. Login to the site with the Userid and password provided by Rogers ESC.</p>
<p align="left">&nbsp;   &nbsp; 3. Click on the Process RMA link from the navigation links on the left of the page.</p>
<p align="left">&nbsp;   &nbsp; 4. Complete the form being sure to provide complete and detailed information  </p>
<p align="left">&nbsp;   &nbsp; 5. Click on the submit link to send the information and finish the process</p>
<p align="left">You will receive an email from Rogers once the request has been processed. If there is any reason that prevents us from processing the RMA, a Rogers agent will contact you to discuss the issue.  </p>
<p align="left">&nbsp;  &nbsp; &nbsp;</p>
<!--------------------------------------------------------------------------------------->
<BR><BR>
<BR><BR><BR><BR><BR>
<BR><BR><BR><BR><BR><BR><BR>
<p align="center">&nbsp;</p>
</td></tr>
</table></td>
</tr>
</table>
</body>
</html>
 
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Cannot modify header information and SetCookie Function

Post by social_experiment »

By adding the html, you sent output to the browser, you sent output to the browser, which setcookie() doesn't like because it needs to be sent to the browser before any other output.

Code: Select all

<?php  ob_start();
 header("Location: form.html");
 ob_end_flush(); ?>
If you want ob_start() to work you will have to place it right at the start of your page (just after the <?php tag) and right at the end of the page (just before the final ?>)

You could create a function that accepts the values you want to set in the cookie as arguments.

Code: Select all

<?php function setMyCookie($value, $value1, $value2) {
 setcookie($value, $value1, $value2);
}?>
and then you call it inside the script you pasted.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply