Jump to content

Header()


ericburnard

Recommended Posts

Hey i have read the header stick looked it up on numerous websites but i am still confussed about why i get this error

 

Warning: Cannot modify header information - headers already sent by (output started at /home/www/ericburnard.freehostia.com/index2.php:1) in /home/www/ericburnard.freehostia.com/header.html on line 2

Warning: Cannot modify header information - headers already sent by (output started at /home/www/ericburnard.freehostia.com/index2.php:1) in /home/www/ericburnard.freehostia.com/header.html on line 3

Warning: Cannot modify header information - headers already sent by (output started at /home/www/ericburnard.freehostia.com/index2.php:1) in /home/www/ericburnard.freehostia.com/header.html on line 4

 

This is the code i am using.

 

<?php
// Date in the past
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Cache-Control: no-cache");
header("Pragma: no-cache");
?>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link href="style.css" rel="stylesheet" type="text/css" />
<title>.: Eric's Blog :.</title>

<?

$p = $_GET['p']; 

if ($p == 'home') 
{ 
echo "This is the home file"; 
} else if ($p == 'login') 
{ 
header('Location:www.login.com');
} else if ($p == 'photos') 
{ 
echo "This is the photos file";
} else if ($p == 'wall') 
{ 
echo "This is the wall file";
} else if ($p == 'links') 
{ 
echo "This is the links file"; 
} else if ($p == 'contact') 
{ 
echo "This is the contact file"; 
} else 
{ 
echo "No page with that ID exists."; 
} 
?>



</head>

<body>


<div style="position: Absolute; left: 140px; top: 70px; height: 10px; width: 10px; padding: 1em; z-index: 1">
<form name="input" action="login.php"
method="get">
Username: 
<input type="text" name="username" class="text"width="20"><BR>
Password:
<input type="password" name="password" class="text" width="20"><br><br>
<input type="submit" value="Submit" class="submit">
</form>
</div>
<div id="container">
<div id="header">
	<ul>
		<li><a href="?p=home">Home</a></li>
		<li><a href="?p=login">Login</a></li>
		<li><a href="?p=photos">Photos</a></li>
		<li><a href="?p=wall">Wall</a></li>
		<li><a href="?p=links">Links</a></li>
		<li><a href="?p=contact">Contact</a></li>
	</ul>
</div>
<br><br>
 <div id="footer"></div>

 

Any help would be great

Eric x

Link to comment
Share on other sites

If you read the sticky, you didn't 'get' it at all...

 

Your script sends out actual headers, then you check $_GET['p'], and if it equal login, you call header(). You can't do that, because you've already sent the header. PHP does not like to start sending a part of a page, then stop and start sending something different. Your browser wouldn't be amused either...

 

PhREEEk

Link to comment
Share on other sites

There are actually two problems.

 

The first one is what is causing the error messages (output started in line 1) and is what is preventing the three headers in lines 2,3, and 4 from working.

 

The second problem is the use of a header('Location:...') redirect after the file outputs html content.

 

For the first problem, here is the short answer - either your file contains some white space (one or more spaces, tabs, or newlines) before the opening <?php tag or the file is saved in UTF-8 format and the BOM (byte order marker) characters at the start of the file are being output to the browser. Either white space or the BOM characters are "content" that is output to the browser and are preventing headers from being sent. Putting ob_start() in your script will not correct this problem. You could turn on output buffering in php.ini or a .htaccess file to work around this problem, but fixing your file is the correct way to solve this problem.

 

For the second problem, do what the sticky post and teng84 suggests and put any php logic that determines if a redirect will occur on the page first, before the file outputs any content to the browser.

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.