SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: Header help.

  1. #1
    SitePoint Evangelist Raining_Curses's Avatar
    Join Date
    May 2005
    Posts
    560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Header help.

    I keep getting this error on my page:

    Warning: Cannot modify header information - headers already sent by (output started at /home/staron/public_html/thetrial/test/index.php:21) in /home/staron/public_html/thetrial/test/index.php on line 27

    Why is this, im trying to send someone to an error page when they get an error, using
    PHP Code:
     Header ("location: page.php"); 
    Is there a more simple way i can redirect someone from to a different page.

    This is the code around line 27, its part of a form, this is the code for when it get submitted:

    PHP Code:
    <?

    if (isset($_POST['submit'])) { // if form has been submitted

        /* check they filled in what they were supposed to and authenticate */
        
    if(!$_POST['uname'] | !$_POST['passwd']) {
            
    header ("location: error.php?error_message=You did not fill a requiered field");
        }
    Any ideas?

    Thanks

    Raining
    www.thetrial.staronesw.com
    Check out my online text based rpg (In development!!)

  2. #2
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You cannot have any HTML above the code if you're going to use the header() function. As soon as the HTTP body has been started, it's too late to send any headers.

    If you cannot move the test to the beginning of the file, you'll have to buffer the output with ob_start to prevent any content from being output.

    Put this as the very first line in the file:
    PHP Code:
    <?php ob_start(); ?>
    Birnam wood is come to Dunsinane

  3. #3
    SitePoint Evangelist Raining_Curses's Avatar
    Join Date
    May 2005
    Posts
    560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nice one, fixed. I thought i was something to do with that because i was reading up about it, but i didnt realise you had to put <?php ob_start(); ?> right on the first line

    Thanks
    www.thetrial.staronesw.com
    Check out my online text based rpg (In development!!)

  4. #4
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, as the HTTP response is composed of two parts: head and body. These are separated by a blank line (CR+LF). The head must precede the body.

    Anything you output via PHP, either directly (with echo, print, etc.) or indirectly by jumping into HTML mode, goes to the body. As soon as a single character is sent to the body, you can no longer send any more headers.

    One solution is to start the file with '<?php' and make all the tests etc., and then call header() before outputting the form. The other is to buffer the output until you know whether or not you need to send any headers. For the aforementioned reasons, ob_start() must be called before any characters are output, or it will be too late for header() anyway.

    (There are other uses of output buffering, like transforming the result before outputting it, but this is one way to use it.)
    Birnam wood is come to Dunsinane

  5. #5
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    header ("location: error.php?error_message=You did not fill a requiered field");There are plenty of browsers that won't work in! Should be: header ("Location:.. followed by an absolute http path including the scheme.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •