php – Cannot modify header information – headers already sent by (php header already sent error)

This error comes when you print any thing before php hreader command

Example Error Code:
<?php
print “text”;
header(‘Location: http://www.example.com/&#8217;);
?>

Solution :

<?php
function FunJavaScriptRedirection($url)
{?>
<script type=”text/javascript”>
<!–
window.location = “<?=$url?>”
//–>
</script>
<?}
print “test”;
FunJavaScriptRedirection(“http://www.example.com/&#8221;);

?>

Glad to Help you !

Glad to Help you !

97 Comments »

  1. gentowa said

    Thanks! That’s what I needed for my redirection script now :)

  2. Tharindu Dasun said

    WoW dude, Thanks a Lot; This Information is Really Helpfull to me..

  3. to'wan said

    thanks mate… i spent whole my time figuring this out.. u rock!!!

  4. Ramesh said

    Hi Great,

    Thank you so much.

  5. francis said

    nice..thanks dude..that was very helpful..

  6. Wasim said

    I have used your example. But this page is not re-directed to desired page. Why is this problem occured? Can you help me pls?

    • A.C.E said

      change your code to this:

      window.location = “”;

  7. Zzzy said

    IT WAS KILLING ME!!!!

    Thanks man!!

  8. rupesh said

    I just say want to say Thanks.
    I was facing this for the error, and urs idea work.

  9. rupesh said

    i just want to say many many thanks to u for this advice.

  10. karuppasamy said

    Very nice.

  11. niz said

    Hi, i’ve used your solution, but the redirect page was’nt display..
    pls help me…

  12. niz said

    ok…sorry..i forget something…
    thanks for your coding…
    really help…

  13. Vishal said

    this problem have a lot of pages in my site.

  14. Vaasu said

    Thanks for solution. but i need solve the php problem. im not ready to go js. please give solution (about header) in php.
    thanks………

  15. Mohamad A said

    You rock dude. Thanks

  16. am3n said

    even a blank line space could generate the error :D

  17. Amjith PS said

    I just say want to say Thanks.
    I was facing this for the error, and urs idea work.

  18. myboo said

    thnks a lot… its a simple solution, but is very usefull …
    thanks …

  19. marxguru said

    thanks for ur idea yaar

  20. Kamil said

    It´s working! I have to say just thank you men!With headers i had big problem.
    I´m form chcech republic and on ours web pages there aren´t good anual howto do that.

  21. IntetkenCer said

    There are 5 houses in five different colors
    In each house lives a different nationality.
    These 5 owners drink a certain beverage, smoke a certain brand of cigar and keep a certain pet.
    No owners have the same pet, smoke the same brand of cigar, or drink the same beverage.

    The CLUES:

    The Brit lives in the Red house.
    The Swede keeps dogs as pets.
    The Dane Drinks tea.
    The Green House is on the left of the White House.
    The Green House’s owner drinks coffee.
    The person who smokes Pall Mall rears birds.
    The owner of the yellow house smokes Dunhill.
    The man in the center house drinks milk.
    The Norwegian lives in the first house.
    The man who smokes Blends lives next to the one who keeps cats
    The man who keeps horses lives next to the man who smokes Dunhill.
    The man who smokes Blue Master drinks beer.
    The German smokes Prince.
    The Norwegian lives next to the Blue House.
    The man who smokes Blends has a neighbor who drinks water.
    The QUESTION:

    Who owns the fish?

  22. Akhil said

    Thanx a lot. My project survives b’coz of u.

  23. jhili said

    thank u so much for responding.It was killing me

  24. syeda said

    Thanks u very very mauch ….. i was trying to figure it out for an hour ……

  25. jb.jigar said

    Thanks …

  26. Jaswant Kumar Tak said

    Its the same like you can use

    echo “document.location.href=’new_url.php’

  27. nordage said

    Thanks! Very interesting solution.

  28. Adsense Click value said

    Thx article useful

  29. MyDigitalZone » Blog Archive » Warning: Cannot modify header information - headers already sent said

    [...] Then, you need to check to see if you are using header() before any output is sent. For example, the php.net gives some examples. Here’s another example. [...]

  30. maheshchari said

    You have done simply better.
    but i have one issue,if user disables javascript what you do?

  31. Roe Lee said

    You are clever !
    But i agree with maheshchari has said, how can we use it when user disabled javascript ?

  32. Srihari said

    thanks a lot buddy

  33. Michael Montgomery said

    Hi Guy’s,
    I know this example originally posted makes sense, but did not work for me. My only solution was to use the meta Refresh…. Major hack but solved my issue.

  34. User1 said

    From the above example , i got solution of my requirement. But Can plz tell me how to use following header information
    Content-type:
    Content-Length:
    “Content-Disposition:
    Accept-Ranges:

  35. milind said

    Thaks, It is very use full
    This error is occurs when we write echo “” befire header(“dddddd”);

  36. Hey thanks a lot!

  37. noman said

    Above code is enogh to page redirect. Some code is not working on firefox mozilla.

    So, other option is internet explore. Every time it should be work and it is working.

    A programmer should not be copy the code from the internet.

  38. Brian said

    This NOT a solution to the problem.
    It is a workaround which only avoids the problem if the user has js activated.

  39. Manish said

    Sir When we Used Session in a Page and used the Session in another Page then Recived a Error that Header already Sent.

  40. sathish said

    i am getting the error,header already sent

  41. Denise Carr said

    Thank you VERY much, really helpful!

  42. nalla said

    Thanks a lot.. that was really coolest solution..

  43. sushil karki said

    just use ob_start() in the begining of the page. no need of javascript.

  44. miff said

    Use ob_start() at a very top of your script, and after header(…) use ob_end_flush().
    Works great, JS is good but when user disable js…

  45. Ruslan said

    I got:

    Parse error: syntax error, unexpected T_STRING on line:

    FunJavaScriptRedirection(“http://www.example.com/”);

  46. Ruslan said

    Sorry…
    Parse error: syntax error, unexpected ‘:’ on line:
    FunJavaScriptRedirection(“http://www.example.com/”);

  47. Dinesh said

    Yes Sir, you have helped me to solve out my problem.

  48. Ayesha said

    Thanks..
    best solution

  49. Sravan said

    Thanks a lot for the info .It helped me a lot

  50. muhsin said

    i am getting the error:Warning: Cannot modify header information – headers already sent by (output started at /home/goldento/public_html/welcome/logindispatch.php:13) in /home/goldento/public_html/welcome/logindispatch.php on line 50
    please help me

    • ashokansu said

      hello muhsin
      this problem is not come in localhost….but sometime give the error in server(not all server)

      this is called whitespace problem

      you use not space

      or

      use javascript
      window.location

  51. [...] One way to fix this problem is by using Javascript or some other language to call the redirect. [...]

  52. Peter said

    Stupit,

    what about if the visitor dont support javascipt.

    becasue of designers like YOU.. i HAVE TO turn on javascipts

  53. Thanks! It works!

  54. harish said

    Hi,

    iam facing following error,can any one help me please..

    Warning: move_uploaded_file(tempresume/test.doc) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home/mgbss/public_html/site1/resume2.php on line 44

    Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move ‘/tmp/phpUaHPQ0′ to ‘tempresume/test.doc’ in /home/mgbss/public_html/site1/resume2.php on line 44

  55. Govind said

    dear all,
    I’m working php Xampp tools 6.0 php into mysql image store and receive the image. Store the image data base successfully but get the database into php file download the file. replay one error msg.

    Warning: Cannot modify header information – headers already sent by (output started at J:\xampp\htdocs\xampp\SMS\download.php:14) in J:\xampp\htdocs\xampp\SMS\download.php on line 60

    How to run the program dears replay

  56. jigsaw said

    This web page is mostly a stroll-by for all the data you needed about this and didn’t know who to ask. Glimpse here, and also you’ll undoubtedly uncover it.

  57. amitmondal said

    Nice tutorial….thanks for this post…..
    Please visit this link for learn more script.

    http://amitmondal.wordpress.com/

  58. digitalpbk said

    There are many solutions to this problem, please see

    http://http://digitalpbk.com/php/warning-cannot-modify-header-information-headers-already-sent

  59. paulo said

    Great!!! Thanks a lot!!!

  60. sabal said

  61. sabal said

    need to solve this problem.link is not working

  62. amila said

    Great pal… it works perfectly… Thanx a lot.. and lot more…

  63. phpdev said

    Thanks! This saved me lots of trouble & time.

  64. vvv said

    function js_redirect($url){echo”

    document.location.href=’”.$url.”‘;

    “;}

  65. Naleen said

    :)

    Ya, it really helps me. Thanks a lot for your post.

  66. Solution is very simple. dont change anything on your script,

    header(“LOCATION:http://www.google.com“);

    this will cause error.

    Process 1 : Just Paste

    error_reporting(0);

    at the top of your page it will works fine.

    if not work for you use
    Process 2. :
    paste
    ob_start();
    at the top of your page.

    and paste
    ob_end_flush();

    at the bottom of your page

    [ NB: process two is very usefull if you host your site into siteground ]

  67. Xopmoyen said

    pictures blank scrolls,

  68. Mike said

    I had been fighting with that for a few days. Thank You

  69. Maaz Ahmed said

    Thanks. It was really effected……….

  70. Manoj Nemade said

    Thanks……

  71. shakir said

    thanks.. its working. outstanding method

  72. gowsik said

    Warning: Cannot modify header information – headers already sent by (output started at /home/klna4/public_html/logininter.php:8) in /home/klna4/public_html/logininter.php on line 36

    How to solve this warning for this code

    <?php
    $username1='';
    $companyname='';
    if(isset($_POST['companyname']))
    $companyname=$_POST['companyname'];
    if(isset($_POST['username']))
    $username1=$_POST['username'];
    if(isset($_POST['password']))
    $password1=$_POST['password'];
    @$conn=mysql_connect("localhost","klna4_login1","loginform1");
    @$db=mysql_select_db("klna4_login")or die(mysql_error());
    $result=mysql_query("select * from login") or die(mysql_error());

    while($row=mysql_fetch_array($result))
    {
    if($companyname==$row['company'])
    {
    if($username1==$row['username'])
    {
    if($password1==$row['password'])
    {
    if($row['find']=='0')
    {
    mysql_query("update login set find='1' where username='$username1'and company='$companyname'");
    header("Location:bodypage.php?name=$username1,name2=$companyname");
    }
    else
    {
    header("Location:login.php");
    }
    }
    if($password1$row['password'])
    {
    header(“Location:login.php”);
    }
    }
    }
    }
    ?>

  73. hi!,I like your writing very a lot! proportion we be in contact extra about your article on AOL? I require an expert on this area to resolve my problem. Maybe that’s you! Looking ahead to look you.

  74. emil boc & elena udrea said

    ,

  75. Chandima said

    Great!!! Once I removed my all printf and print_r site is working smoothly

  76. Muhammad Sajid said

    Use ob_start() and ob_flush() to get rid of this problem. Actually this is a warning message and comes if the output buffer already contains any value it may be some garbage, so at the most top of the file use ob_start() and at the end of the file use ob_flush(). Yap no matter if the file contains session_start() at top.
    For more detail please read http://www.php.net/manual/en/function.flush.php

  77. [...] You should see this time saving url: Redtube [...]

  78. Kritika said

    Thanx a lot…You are great.

  79. source said

    This is often a fantastic website, would you be interested in working on an interview concerning just how you produced it? If so e-mail myself!

  80. Manish said

    Really Really helpful to me. A Lots of thnx Indeed.

  81. Continue said

    When someone writes an article he/she maintains the
    plan of a user in his/her mind that how
    a user can understand it. Thus that’s why this piece of writing is great. Thanks!

  82. brandybra said

    The National Oceanic and Atmospheric Administration of the US government has
    announced only last September, the global average for the period January to August 2010 is 580F (14.
    Although everyone thought that their city or world would cease
    to exist, that was not the actuality. Global warming is caused by burning
    hydrocarbons and depleting forests, not by the sun.

  83. siswo said

    Yes i have script php here :
    <?
    header("location:index.php?page=download");
    …………….
    problem :
    Warning: Cannot modify header information – headers already sent by (output started at /home/siprofit/public_html/index.php:15) in /home/siprofit/public_html/download.php on line 7
    please advice, thanks

  84. Michaael said

    I would not recommend this for security admin area as the user could disable javascript.

  85. Stella said

    I all the time emailed this website post page to all
    my friends, because if like to read it after that my contacts will too.

  86. dinesh said

    I wrote this code insite the word press dashboard page panel but I got the error like

    Cannot modify header information – headers already sent by (output started at /home/content/44/10911544/html/onwork/dreamart/wp-content/themes/elegant-photography/header.php:15) in /home/content/44/10911544/html/onwork/dreamart/wp-content/plugins/insert-php/insert_php.php(48) : eval()’d code on line 17

    my code is
    …………………………….

    [insert_php]

    //error_reporting(0);

    $name=$password=$code=”;

    if(isset($_POST['submit']))
    {

    $name=$_POST['vm_name'];
    $password=$_POST['vm_password'];
    $code=$_POST['vm_code'];

    if($name==”dinesh” && $password==”dinesh1″)
    {
    $_SESSION['vm_code']=$code;
    header(“Location: http://vmwebs.com/onwork/dreamart/vasumathywedding“);
    }
    }

    [/insert_php]

    need help please

  87. Undeniably take into consideration that which
    you stated. Your best justification have also been concerning the net the simplest thing to bear in mind.

    I show you, I certainly get irked while people consider concerns
    they can plainly don’t know about.

  88. Glen said

    Somebody necessarily lend a hand to make
    severely posts I would state. This is the 1st time I frequented your site and thus far?
    I amazed with the analysis you made to make this kind
    of put up incredible. Fantastic task!

  89. “get the facts”

    “[...]f Hello there! This is kind of off topic but I need some advice from an estab 0f[...]“

  90. “a cool way to improve”

    “[...]p Thank you for any other magnificent post. Where else could anyone get that zg[...]“

  91. raghu said

    retailmetoday

  92. Kamlesh Dave said

    Thanks your code is working and script is woriking gooddddd………………

  93. Wiley said

    Managers also develop budget estimates for their departments, and keep track of purchases and expenses to make sure they are in line with budget guidelines.
    We also ensure that your nursing papers have been
    formatted according to the writing style recommended by your instructor.

    A nursing education on the internet gives students flexibility and
    is much less highly-priced.

RSS feed for comments on this post · TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: