When i use this function in first line of the code no error comes but when this function is used in middle of the code an error comes,
error --Php Header already sent
what is the solution and why this error comes ????
PHP Header() Function
Page 1 of 16 Replies - 1718 Views - Last Post: 21 May 2010 - 11:49 AM
Replies To: PHP Header() Function
#2
Re: PHP Header() Function
Posted 19 May 2010 - 05:40 AM
You should read the documentation. (It's pretty much one of the first things you see when you load the page
)
You can send NO data to the browser (Don't output anything, basically.) before the header() function is used. If you do, you'll get that error.
Yours,
Shane~
You can send NO data to the browser (Don't output anything, basically.) before the header() function is used. If you do, you'll get that error.
Yours,
Shane~
This post has been edited by ShaneK: 19 May 2010 - 05:42 AM
#3
Re: PHP Header() Function
Posted 19 May 2010 - 05:43 AM
Read the documentation. All calls to header() must be made before any output is sent to the client. When you stop and think about it, this should be obvious - it's sending the HTTP headers. If you try to stick the headers in the middle of the page body, then by definition they aren't headers anymore. Hence it won't work.
Edit: Drat! Beat me to it.
Edit: Drat! Beat me to it.
This post has been edited by AdaHacker: 19 May 2010 - 05:44 AM
#4
Re: PHP Header() Function
Posted 19 May 2010 - 08:06 PM
call header() before any html code which is outputted to user...
Not correct
THis is correct
You can use html meta refresh instead of header() IF YOU REALLY NEED TO DISPLAY SOMETHING BEFORE REDIRECTION.
Not correct
echo '<div>Hello!</div>'; header('Location : index.php');
THis is correct
header('Location : index.php'); echo '<div>Hello!</div>';
You can use html meta refresh instead of header() IF YOU REALLY NEED TO DISPLAY SOMETHING BEFORE REDIRECTION.
#5
Re: PHP Header() Function
Posted 20 May 2010 - 06:15 AM
This:
is essentially this:
because it's going to redirect before getting to the echo statement. You could do something like:
header('Location : index.php'); echo '<div>Hello!</div>';
is essentially this:
header('Location : index.php');
because it's going to redirect before getting to the echo statement. You could do something like:
if($redirect)
{
header('Location : index.php');
} else
{
echo '<div>Hello!</div>';
}
#6
Re: PHP Header() Function
Posted 20 May 2010 - 12:47 PM
There is a workaround if you absolutely must do it the way you are currently trying. Research output buffers in PHP.
#7
Re: PHP Header() Function
Posted 21 May 2010 - 11:49 AM
easy way to understand this is,
DO NOT OUTPUT ANYTHING BEFORE header() function,
header function should be given at the top of output code,., say like
{lot of starting php code like connection to database etc etc}
{header function}
{actual output to user, which includes printf, echo, print_r depends.}
so in short do not send anything to browser before header(), and if u already sent it, do not use header()
DO NOT OUTPUT ANYTHING BEFORE header() function,
header function should be given at the top of output code,., say like
{lot of starting php code like connection to database etc etc}
{header function}
{actual output to user, which includes printf, echo, print_r depends.}
so in short do not send anything to browser before header(), and if u already sent it, do not use header()
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote







|