Function header()

Status
Not open for further replies.

mrnigma

New Member
Messages
4
Reaction score
0
Points
0
Hello!.
I'm trying to use the header function like this:

header("location:test1.php");
header("location:test2.php?var=".$var);

but when i upload the .php file on my website it doesn't work properly, making the seguent error:

Warning: Cannot modify header information - headers already sent by...

I've searched for this error and i found that it's an error which appears when the variable "output_buffer" in the php.ini file on the server is set on "Off" instead of "On".

It's possibile that this is the problem on the x10hosting server?
 

garrettroyce

Community Support
Community Support
Messages
5,609
Reaction score
252
Points
63
This error is usually caused by your script and not the server. If you send anything to the standard output, the headers will be sent and you cannot send them again.

For example:
Code:
<?php
echo 'hello world'; // headers sent here
?>
Code:
 <?php //notice the space before the tag
header(....);
?>

Now, the output buffer can be used like this:
Code:
<?php
ob_start(); // start output buffering
echo 'I can say whatever I want and you can't send the headers!';
header('Location: wherever_you_want.php');
header('Location: why_not_do_this_again.php');
ob_flush();
?>

When buffering the output, you can overwrite existing headers.
 

mrnigma

New Member
Messages
4
Reaction score
0
Points
0
This error is usually caused by your script and not the server. If you send anything to the standard output, the headers will be sent and you cannot send them again.

For example:
Code:
<?php
echo 'hello world'; // headers sent here
?>
Code:
 <?php //notice the space before the tag
header(....);
?>
Now, the output buffer can be used like this:
Code:
<?php
ob_start(); // start output buffering
echo 'I can say whatever I want and you can't send the headers!';
header('Location: wherever_you_want.php');
header('Location: why_not_do_this_again.php');
ob_flush();
?>
When buffering the output, you can overwrite existing headers.
I've not output before, the code is

PHP:
if($var1=="answer")
{
header('Location: wherever_you_want.php');
}
else
{
header('Location: wherever_you_want.php?var='.$var');
}
However i'll try to use your code and i'll write if it'll work properly.
However i'll thank you for the help :).
 

garrettroyce

Community Support
Community Support
Messages
5,609
Reaction score
252
Points
63
I forgot to mention, if your code has an error, warning, or notice, this counts as header output sent, because PHP sends an error message to the output and therefore has to post headers.
 

mrnigma

New Member
Messages
4
Reaction score
0
Points
0
I forgot to mention, if your code has an error, warning, or notice, this counts as header output sent, because PHP sends an error message to the output and therefore has to post headers.
I trained the php warning but nothig appears, only the already sent error posted previously.
But the fact is the on my pc the webpage works perfectly but when i publish it on x10hosting server i go to the www site and it doesn't work :mad:
 

mrnigma

New Member
Messages
4
Reaction score
0
Points
0
I tried using ob_flush as you said but it doesn't work ^^".
I put here the code of the pages where i use the header and maybe someone can understand why it doesn't work ^^".

Page1(form) "first":

PHP:
<form method="post" action="firstEff.php">
<input type="text" size="20" maxlength="20" name="first" style="color: #00CC00;  background-color:#000000">
<br />
<br />
<input type="submit" value="Go on">
</form></center>
<?php
if($_GET['var']=="ok")
{
    echo "<br><br><center><a1>Retry!</a1></center>";
}
?>
Page2(effectuation) "firstEff":

PHP:
<?php
if($first == "secret")
{
ob_start();
header("location:new_page.php");
ob_flush();
}
else
{
ob_start();
$var="ok";
header("location:first.php?var=".$var);
ob_flush();
}
?>
 
Status
Not open for further replies.
Top