header() issues

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
User avatar
moniarde
Forum Newbie
Posts: 20
Joined: Thu Feb 06, 2003 12:45 am
Location: Perth, Western Australia

header() issues

Post by moniarde »

I am trying to do a simple redirect, but am having some serious issues with it all. No matter what I do, everytime I try to use header(), I get this message.
(I've changed the file info)
Warning: Cannot modify header information - headers already sent by (output started at drive:/host/folder/filename.php:10) in drive:/host/folder/filename.php on line 14
I've checked this syntax over and over and there doesn't seem to be anything wrong with it. Here's the code.

Code: Select all

<html>
<head>
	<title>Untitled</title>
</head>

<body>

<?

$testvar = "this has worked.";

header("location: http://%sheader.php" .$_SERVER&#1111;'HTTP_HOST']);

exit;

?>

</body>
</html>
The variable is just to test that it will pass a variable through.

Does anyone have any ideas?
crazyjimsmith
Forum Commoner
Posts: 28
Joined: Sat Jan 11, 2003 1:46 pm
Location: Sydney
Contact:

Post by crazyjimsmith »

Im not an expert but I do recall reading some of the read these first posts at the top of the forum.

Your header info needs to be placed before any code.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

User avatar
moniarde
Forum Newbie
Posts: 20
Joined: Thu Feb 06, 2003 12:45 am
Location: Perth, Western Australia

reply

Post by moniarde »

OK, thanks for the reply, and I understand now that I need to be careful what i can use before I use a header tag.

The problem I have though, is that I tried to do the same thing WITHOUT trying to create the variable, and it still did it. I commented it out, I deleted it, I tried renaming the file, but it still didn't work. Any ideas?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

It's not creating the variable which is causing the problem, it is the HTML that you are outputting to the browser beforehand:

Code: Select all

html> 
<head> 
   <title>Untitled</title> 
</head> 

<body>
You cannot have any output before you try and use the header() function.

Mac
User avatar
moniarde
Forum Newbie
Posts: 20
Joined: Thu Feb 06, 2003 12:45 am
Location: Perth, Western Australia

Post by moniarde »

No luck with deleting anything before header(). Here's the code I have:

Code: Select all

<?

header("location: http://%sheader.php" .$_SERVER&#1111;'HTTP_HOST']);

?>
This is everything in the file.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Are you still getting the same error or is it not working in a different way?

Mac
User avatar
moniarde
Forum Newbie
Posts: 20
Joined: Thu Feb 06, 2003 12:45 am
Location: Perth, Western Australia

Post by moniarde »

Well, after I sent that last post, about 5 mins after I first deleted everything, it decided it was going to work, and since then I've been testing it with everything I can to make sure it's going to hold up. I'm currently using output buffering to make sure it holds up. And so far it is. Thanks all for you help.
shareaweb
Forum Newbie
Posts: 3
Joined: Sun Feb 16, 2003 9:33 am
Location: Philippines

header issue

Post by shareaweb »

you must use output bufferring in this case since you already sent html tags to the browser (and hence a header..) or you can switch to meta tag refresh

ob_start();

/* code here */

ob_end_flush();

All you worries go away this way ;)
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Re: header issue

Post by twigletmac »

shareaweb wrote:you must use output bufferring in this case since you already sent html tags to the browser (and hence a header..) or you can switch to meta tag refresh
or you can just make sure you don't output anything to the browser before you try and use the header() function, not every circumstance requires output buffering.

Mac
Post Reply