Problem with header("Location:.. function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bissatch@yahoo.co.uk

    Problem with header("Location:.. function

    Hi,

    I am trying to use the following function:

    header("Locatio n: http://www.mysite.co.u k/home/update/index.php");

    ....but getting the following error:

    Warning: Cannot modify header information - headers already sent by
    (output started at
    /data/httpd/VirtualHosts/www/htdocs/home/update/index.php:12) in
    /data/httpd/VirtualHosts/www/htdocs/home/update/index.php on line 124


    Is there any reason why I am getting this error? One concern of mine
    was that the header function was not at the top of the page (line 1),
    does this matter? I need this command to run a series of statements and
    depending on specific conditions, redirect the user to another page.

    Cheers

    Burnsy

  • Alvaro G Vicario

    #2
    Re: Problem with header("Lo cation:.. function

    *** bissatch@yahoo. co.uk wrote/escribió (7 Jul 2005 03:15:21 -0700):[color=blue]
    > Warning: Cannot modify header information - headers already sent by
    > (output started at
    > /data/httpd/VirtualHosts/www/htdocs/home/update/index.php:12) in
    > /data/httpd/VirtualHosts/www/htdocs/home/update/index.php on line 124
    >
    >
    > Is there any reason why I am getting this error?[/color]

    Nope. You've been chosen randomly for suffering divine wrath :)

    May I ask what's in line #12 of
    /data/httpd/VirtualHosts/www/htdocs/home/update/index.php file?
    [color=blue]
    > One concern of mine was that the header function was not at the top of the page
    > (line 1), does this matter?[/color]

    It can be last time of the script if you want to. The only rule you must
    follow is that headers come first and content comes later. You cannot mix
    them.



    --
    -- Álvaro G. Vicario - Burgos, Spain
    -- http://bits.demogracia.com - Mi sitio sobre programación web
    -- Don't e-mail me your questions, post them to the group
    --

    Comment

    • kjordan@insurancejournal.com

      #3
      Re: Problem with header("Lo cation:.. function

      in other words, you're probably printing something to the screen
      before your header command. just put your header command way at the
      top.

      Comment

      • Philip  Olson

        #4
        Re: Problem with header("Lo cation:.. function

        This is the most commonly asked question in PHP and people keep asking
        it. Maybe it's time to add a FAQ to the PHP Manual, I thought there was
        already one.

        The line number in which you call this does not matter, it can be on
        line 1 or on line a million and still work. What does matter is whether
        or not output (like html, text...) is sent the browser first. You don't
        want that. Your error indicates that output began at around line 12 so
        fix that. Or if you're lazy and want to write improper code, enable
        output buffering to take care of this. Enabling it allows you to send
        headers() throughout your code without a problem.

        Comment

        • Michael Winter

          #5
          Re: Problem with header("Lo cation:.. function

          On 07/07/2005 17:35, Philip Olson wrote:
          [color=blue]
          > [Call to header function not 'working'] is the most commonly asked
          > question in PHP and people keep asking it. Maybe it's time to add a
          > FAQ to the PHP Manual, I thought there was already one.[/color]

          There is an FAQ in the manual, but this shouldn't need to be added -
          it's clearly addressed in the description of the header function:

          "Remember that header() must be called before any actual output is
          sent, either by normal HTML tags, blank lines in a file, or from
          PHP."

          [snip]

          Mike

          --
          Michael Winter
          Prefix subject with [News] before replying by e-mail.

          Comment

          • Marcin Dobrucki

            #6
            Re: Problem with header("Lo cation:.. function

            Philip Olson wrote:
            [color=blue]
            > The line number in which you call this does not matter, it can be on
            > line 1 or on line a million and still work. What does matter is whether
            > or not output (like html, text...) is sent the browser first. You don't
            > want that. Your error indicates that output began at around line 12 so
            > fix that. Or if you're lazy and want to write improper code, enable
            > output buffering to take care of this. Enabling it allows you to send
            > headers() throughout your code without a problem.[/color]

            Alternative solution is to use HTML_Page (HTML_Page2) or similar
            mechanism, where you construct an objet to represent a page, and then
            print your "body" stuff to it. That way, if you need to redirect mid
            way through the script, you are quite sure nothing has been sent. At
            the end of the script, just call the "display" function for your object.

            /m

            Comment

            • Philip  Olson

              #7
              Re: Problem with header("Lo cation:.. function

              Yes but it's asked all the time and newbies don't understand what that
              means. Anyway, I'll add this FAQ this week.

              Comment

              Working...