6 Replies - 1966 Views - Last Post: 21 May 2010 - 11:49 AM Rate Topic: -----

#1 mukeshsoni.tech@gmail.com  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 19-May 10

PHP Header() Function

Posted 19 May 2010 - 04:01 AM

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 ????
Is This A Good Question/Topic? 0
  • +

Replies To: PHP Header() Function

#2 ShaneK  Icon User is offline

  • require_once("brain.php"); //Fatal error :/
  • member icon

Reputation: 240
  • View blog
  • Posts: 1,224
  • Joined: 10-May 09

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~

This post has been edited by ShaneK: 19 May 2010 - 05:42 AM

Was This Post Helpful? 0
  • +
  • -

#3 AdaHacker  Icon User is offline

  • Resident Curmudgeon

Reputation: 452
  • View blog
  • Posts: 811
  • Joined: 17-June 08

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.

This post has been edited by AdaHacker: 19 May 2010 - 05:44 AM

Was This Post Helpful? 0
  • +
  • -

#4 hadi_php  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 10
  • View blog
  • Posts: 382
  • Joined: 23-August 08

Re: PHP Header() Function

Posted 19 May 2010 - 08:06 PM

call header() before any html code which is outputted to user...

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.
Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2891
  • View blog
  • Posts: 10,025
  • Joined: 08-August 08

Re: PHP Header() Function

Posted 20 May 2010 - 06:15 AM

This:
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>'; 
}

Was This Post Helpful? 0
  • +
  • -

#6 jrm402  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 51
  • View blog
  • Posts: 333
  • Joined: 18-March 10

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.
Was This Post Helpful? 0
  • +
  • -

#7 smartflower2002  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 116
  • Joined: 20-May 10

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()
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1