Again "headers already sent by ".

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Q

    Again "headers already sent by ".

    Hello,

    I have composed the following simple php file:
    <html>
    <head><title> Title. </title></head>
    <body>
    <?php
    header("Locatio n: http://www.something.c om/tmp2.php");
    ?>
    </body>
    </html>

    And result of its execution is:
    Warning: Cannot modify header information - headers already sent by
    (output started at /home/something/public_html/tmp1.php:4) in
    /home/something/public_html/tmp1.php on line 5

    I thing, that in newsgroups I found a cause of my error:
    "The reason you are getting this warning is because PHP is trying to
    send a header (redirecting in this case) to browser, but it cannot,
    because the output has already started."

    But now I do not understand what is the use of header("Locatio n:
    ***");. It can be placed only in the beginning of files, and after
    browser open this file it (browser) is immediatly redirected to second
    file. Is it not better to open second file straight away (withou
    opening the first file)? Further, what I need to do if I want to
    diplay some file and than, let say in 20 second, user have to be
    redirected to next file? What I need to do if I want to perform
    redirection only if some variables have same defined values?

    Pleas help me if you know answers.
    Thank you.
  • Stijn Verholen

    #2
    Re: Again &quot;header s already sent by &quot;.

    Q wrote:[color=blue]
    > Hello,
    >
    > I have composed the following simple php file:
    > <html>
    > <head><title> Title. </title></head>
    > <body>
    > <?php
    > header("Locatio n: http://www.something.c om/tmp2.php");
    > ?>
    > </body>
    > </html>
    >
    > And result of its execution is:
    > Warning: Cannot modify header information - headers already sent by
    > (output started at /home/something/public_html/tmp1.php:4) in
    > /home/something/public_html/tmp1.php on line 5
    >
    > I thing, that in newsgroups I found a cause of my error:
    > "The reason you are getting this warning is because PHP is trying to
    > send a header (redirecting in this case) to browser, but it cannot,
    > because the output has already started."
    >
    > But now I do not understand what is the use of header("Locatio n:
    > ***");. It can be placed only in the beginning of files, and after
    > browser open this file it (browser) is immediatly redirected to second
    > file. Is it not better to open second file straight away (withou
    > opening the first file)? Further, what I need to do if I want to
    > diplay some file and than, let say in 20 second, user have to be
    > redirected to next file? What I need to do if I want to perform
    > redirection only if some variables have same defined values?
    >
    > Pleas help me if you know answers.
    > Thank you.[/color]

    You have already created an html page with the tags, that is the output.
    The headers, sent with header(), can only arrive BEFORE any other output
    in the file.

    What I think u are trying to do, is include html tags and php code from
    tmp2.php into tmp.php.
    If this is the case, try include(<your page>); in stead of header().

    HTH

    Stijn

    Comment

    • Erwin Moller

      #3
      Re: Again &quot;header s already sent by &quot;.

      Q wrote:
      [color=blue]
      > Hello,
      >
      > I have composed the following simple php file:
      > <html>
      > <head><title> Title. </title></head>
      > <body>
      > <?php
      > header("Locatio n: http://www.something.c om/tmp2.php");
      > ?>
      > </body>
      > </html>
      >
      > And result of its execution is:
      > Warning: Cannot modify header information - headers already sent by
      > (output started at /home/something/public_html/tmp1.php:4) in
      > /home/something/public_html/tmp1.php on line 5[/color]

      Which makes sense.
      You did produce output already.

      Do not confuse headers with meta-tags or something like that.
      You produced output because you printed: <html><head> etc etc..

      [color=blue]
      >
      > I thing, that in newsgroups I found a cause of my error:
      > "The reason you are getting this warning is because PHP is trying to
      > send a header (redirecting in this case) to browser, but it cannot,
      > because the output has already started."[/color]

      Sounds correct.
      [color=blue]
      >
      > But now I do not understand what is the use of header("Locatio n:
      > ***");.[/color]

      lemmie help you then. :-)
      [color=blue]
      > It can be placed only in the beginning of files, and after
      > browser open this file it (browser) is immediatly redirected to second
      > file.[/color]

      correct, but don't use the word 'file'.
      It is just a resource or better: URL or URI, not always a simple file.

      [color=blue]
      > Is it not better to open second file straight away (withou
      > opening the first file)?[/color]

      If that doesn't break anything, well... of course!
      Why send somewhere without reason?

      COnsider the following situation:

      page1.php contains a nice page with a form. The action of the form is
      page2.php

      page2.php contains logic to receive the form and insert some records in some
      database.

      Ok?

      The problem you face now is that a client fills in the form on page1.php and
      presses submit, the data in the form is send to page2.php.
      page2.php processes this information and inserts it into the database.

      ......

      And now?

      Do you want to let your visitor see an empty page, since page2.php finished
      its job and didn't produce any output/html.

      So here is a situation where you would like to use a header("Locatio n:
      page1.php");

      Then the clients browser receives a command to go to page1.php

      Does it make sense now?
      [color=blue]
      > Further, what I need to do if I want to
      > diplay some file and than, let say in 20 second, user have to be
      > redirected to next file? What I need to do if I want to perform
      > redirection only if some variables have same defined values?[/color]

      Use javascript or META-tags, not PHP.

      In javascript: use window.setTimeo ut("location='a notherpahe.html ';" , 500);

      google for the metatag, i never use it, so don't know the syntax.
      [color=blue]
      >
      > Pleas help me if you know answers.
      > Thank you.[/color]


      You are welcome.
      Regards,
      Erwin Moller

      Comment

      • Geoff Berrow

        #4
        Re: Again &quot;header s already sent by &quot;.

        I noticed that Message-ID:
        <5f275cd6.05020 50532.46c85b73@ posting.google. com> from Q contained the
        following:
        [color=blue]
        >But now I do not understand what is the use of header("Locatio n:
        >***");. It can be placed only in the beginning of files, and after
        >browser open this file it (browser) is immediatly redirected to second
        >file. Is it not better to open second file straight away (withou
        >opening the first file)?[/color]

        Let's say you have a series of pages where you require the user to log
        in. You store the log in status as a session variable and then check
        for its existence at the start of each page, before any HTML. If it
        doesn't exist you can redirect the user to the login page via the header
        function.
        [color=blue]
        >Further, what I need to do if I want to
        >diplay some file and than, let say in 20 second, user have to be
        >redirected to next file?[/color]
        Once the PHP process is over the server has no knowledge of what is
        going on at the client side. To redirect in 20 seconds requires a
        client side process, ie JavaScript or meta refresh
        [color=blue]
        >What I need to do if I want to perform
        >redirection only if some variables have same defined values?[/color]

        Put all the PHP code before any html. <?php must be absolutely the first
        thing on your page.

        --
        Geoff Berrow (put thecat out to email)
        It's only Usenet, no one dies.
        My opinions, not the committee's, mine.
        Simple RFDs http://www.ckdog.co.uk/rfdmaker/

        Comment

        • Geoff Berrow

          #5
          Re: Again &quot;header s already sent by &quot;.

          I noticed that Message-ID: <4204d022$0$289 75$e4fe514c@new s.xs4all.nl>
          from Erwin Moller contained the following:
          [color=blue]
          >google for the metatag, i never use it, so don't know the syntax.[/color]

          <meta http-equiv="refresh" content="20;URL =http://www.example.com ">
          --
          Geoff Berrow (put thecat out to email)
          It's only Usenet, no one dies.
          My opinions, not the committee's, mine.
          Simple RFDs http://www.ckdog.co.uk/rfdmaker/

          Comment

          Working...