Jump to content

Cannot modify header information and SetCookie Function


jponte

Recommended Posts

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.

 

Any help would be appreciated.

 

<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"> </td>
</tr>
</table>

<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr valign="top">

<td width="201"><p><BR>
<!----- menu, insert links below ------------------------------------------>
  » <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>
  » <a href="form.html">Process RMA </a><BR>
<img src="img/menubar.jpg" width="201" height="11" border="0" alt=""><BR>
  » <a href="http://www.rogers-esc.com">Submit</a><BR>
<img src="img/menubar.jpg" width="201" height="11" border="0" alt=""><BR>
 </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">      1. Before you start, make sure that you have all the information that you'll need. This includes: </p>
<p align="left">              a) your company and contact information, the make and model of the device,</p>
<p align="left">              b) the IMEI # for the device </p>
<p align="left">              c) a brief description of the issue with the device</p>
<p align="left">              d) a description of any steps taken to try to resolve the issue  </p>
<p align="left">              e) Shipping and contact information </p>
<p align="left">      2. Login to the site with the Userid and password provided by Rogers ESC.</p>
<p align="left">      3. Click on the Process RMA link from the navigation links on the left of the page.</p>
<p align="left">      4. Complete the form being sure to provide complete and detailed information  </p>
<p align="left">      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">      </p>
<!--------------------------------------------------------------------------------------->
<BR><BR>
<BR><BR><BR><BR><BR>
<BR><BR><BR><BR><BR><BR><BR>
<p align="center"> </p>
</td></tr>
</table></td>
</tr>
</table>
</body>
</html>

Web servers send a response back to the browser.  The response is plain-text and broken into two sections.

 

The first section is the header section.  It contains all sorts of meta information, where meta information means general info about the response.

 

The other section is the content.  The content is the actual web page.

 

The rules for how this information is organized is very strict.

1) The headers must come first, one header per line

2) Two empty lines must be inserted

3) The content is inserted

 

Any errors in regards to Cannot modify header information - headers already sent by are occurring because you are sending #3 before you have finished sending #1.

 

This occurs when you echo text from PHP and then call functions like header() or try to set cookies in the browser.

 

You code looks like this:

<html>
<head>
<title>Rogers ESC RMA Portal</title>
<link rel=stylesheet type="text/css" href="style.css">
...SNIP...
 </p>
<p align="center"><img src="img/login.jpg" width="102" height="102" align="middle"></p>
<?php
ob_start();

 

Everything before the <?php is sent to the browser exactly as if you had echoed it with PHP..

 

This might fix it:

<?php ob_start(); ?>
<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"> </td>
</tr>
</table>

<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr valign="top">

<td width="201"><p><BR>
<!----- menu, insert links below ------------------------------------------>
  » <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>
  » <a href="form.html">Process RMA </a><BR>
<img src="img/menubar.jpg" width="201" height="11" border="0" alt=""><BR>
  » <a href="http://www.rogers-esc.com">Submit</a><BR>
<img src="img/menubar.jpg" width="201" height="11" border="0" alt=""><BR>
 </p>
<p align="center"><img src="img/login.jpg" width="102" height="102" align="middle"></p>
<?php
// 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">      1. Before you start, make sure that you have all the information that you'll need. This includes: </p>
<p align="left">              a) your company and contact information, the make and model of the device,</p>
<p align="left">              b) the IMEI # for the device </p>
<p align="left">              c) a brief description of the issue with the device</p>
<p align="left">              d) a description of any steps taken to try to resolve the issue  </p>
<p align="left">              e) Shipping and contact information </p>
<p align="left">      2. Login to the site with the Userid and password provided by Rogers ESC.</p>
<p align="left">      3. Click on the Process RMA link from the navigation links on the left of the page.</p>
<p align="left">      4. Complete the form being sure to provide complete and detailed information  </p>
<p align="left">      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">      </p>
<!--------------------------------------------------------------------------------------->
<BR><BR>
<BR><BR><BR><BR><BR>
<BR><BR><BR><BR><BR><BR><BR>
<p align="center"> </p>
</td></tr>
</table></td>
</tr>
</table>
</body>
</html>
<?php echo ob_get_clean(); ?>

 

I noticed you had some other ob_starts() in your code.  Typically you need only one if you handle things correctly.

  • 3 weeks later...

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.