Thread: headers

    #1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Location
    utr
    Posts
    9
    Rep Power
    0
    Everytime i try to use headers on win2k/apache/php4 i keep getting errors. I dont have a clue why i get them.. but anyway, i use this code(from webmonkey):

    <?php

    // File Name: auth02.php

    // Check to see if $PHP_AUTH_USER already contains info

    if (!isset($PHP_AUTH_USER)) {

    // If empty, send header causing dialog box to appear

    header('WWW-Authenticate: Basic realm="My Private Stuff"');
    header('HTTP/1.0 401 Unauthorized');
    echo 'Authorization Required.';
    exit;

    } else if (isset($PHP_AUTH_USER)) {

    if (($PHP_AUTH_USER != "admin") &#0124; &#0124; ($PHP_AUTH_PW != "abc123")) {

    header('WWW-Authenticate: Basic realm="My Private Stuff"');
    header('HTTP/1.0 401 Unauthorized');
    echo 'Authorization Required.';
    exit;

    } else {
    echo "
    <P>You're authorized!</p>
    ";
    }
    }

    ?>

    and when the output i get in my browser is:

    Warning: Cannot add header information - headers already sent by (output started at grogram filesapache groupapachehtdocsschildmain_admin.php4:2) in grogram filesapache groupapachehtdocsschildmain_admin.php4 on line 5

    Warning: Cannot add header information - headers already sent by (output started at grogram filesapache groupapachehtdocsschildmain_admin.php4:2) in grogram filesapache groupapachehtdocsschildmain_admin.php4 on line 6
    Authorization Required.


    does any1 else get these problems? and maybe some1 knows a solution?

  2. #2
  3. No Profile Picture
    Apprentice Deity
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 1999
    Location
    Niagara Falls (On the wrong side of the gorge)
    Posts
    3,237
    Rep Power
    18
    Look at the file/line referenced. Your starting to send output at that point. It's either whitespace or text outside of the php tags (<? ?> ) or a print/echo statement.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Location
    utr
    Posts
    9
    Rep Power
    0
    hm, could u maybe be more specific? i mean, im still a newbie.. so i still dont get what was wrong in that script.. :|

  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2000
    Location
    Alkmaar, The Netherlands
    Posts
    123
    Rep Power
    14
    I can come up with 2 problems:

    1. If this is not all your code check if you already are sending some text BEFORE the headers, because that's not "allowed".

    2. There needs to be a newline after the headers. I'm not sure the header() function already does that for you. so try adding a n (or 2) in the last header string.

    ------------------
    Good luck,
    Bas

    ------------------
    E-mail me at: b.vandermeijden@pecoma.nl
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Location
    utr
    Posts
    9
    Rep Power
    0
    <?php
    if (!isset($PHP_AUTH_USER)) {
    header('WWW-Authenticate: Basic realm="My Private Stuff"') ."n";
    header('HTTP/1.0 401 Unauthorized') . "n";
    echo 'Authorization Required.';
    exit;
    } else if (isset($PHP_AUTH_USER)) {
    if (($PHP_AUTH_USER != "bla") && ($PHP_AUTH_PW != "plop")) {
    header('WWW-Authenticate: Basic realm="Login"');
    header('HTTP/1.0 401 Unauthorized');
    echo 'Authorization Required.';
    exit;
    } else {
    ?>

    <html>
    <head>
    <title>MAIN_ADMIN</title>
    </head>

    <body bgcolor="Olive" text="Black" link="Maroon" vlink="Maroon" alink="Maroon">

    <a href="news_admin.php4">Nieuws toevoegen</a><br>
    <a href="pic_admin.php4">Plaatjes toevoegen</a><br>

    </body>
    </html>
    <?
    }
    }
    ?>

    thats the exact code i use atm.. without the 'n' i get the error i mentioned in the first msg. But when the 'n' are added i get a internal server error. the error log says this:

    [Wed Oct 11 07:47:38 2000] [error] [client 127.0.0.1] malformed header from script. Bad header=HTTP/1.0 401 Unauthorized: g:/php/php.exe

    i had this internal server error before, but in another script, which also used the header() function. But now is the question: is the error in the code or is the server malfunctioning?

  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Location
    utr
    Posts
    9
    Rep Power
    0
    oh i just saw that i forgot to add the 'n' to the other headers.. well i did now, but i still got the same error
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2000
    Location
    Alkmaar, The Netherlands
    Posts
    123
    Rep Power
    14
    Hmmm, maybe my answer wasn't clear?

    Try removing the 'n' from the headers, but print a 'nn' after you called the last header.

    [This message has been edited by MeijdenB (edited October 11, 2000).]
  14. #8
  15. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Location
    utr
    Posts
    9
    Rep Power
    0
    <?php
    if (!isset($PHP_AUTH_USER)) {
    header("WWW-Authenticate: Basic realm='login");
    header("HTTP/1.0 401 Unauthorized");
    print "nn";
    echo 'Authorization Required.';
    exit;

    u mean like this?

    if so.. it still got a internal server error.. :/
  16. #9
  17. No Profile Picture
    unemployed dev junkie :(
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Posts
    32
    Rep Power
    14
    First, you don't need to print any n after the header statements .. not even the last one .. PHP takes care of it itself ..
    Second, change:
    header("HTTP/1.0 401 Unauthorized");
    to
    header("Status: 401 Unauthorized");

    Also, remember that the header() function expects a string in format:
    "header_type: header_value"


    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>Originally posted by rh_f:
    <?php
    if (!isset($PHP_AUTH_USER)) {
    header("WWW-Authenticate: Basic realm='login");
    header("HTTP/1.0 401 Unauthorized");
    print "nn";
    echo 'Authorization Required.';
    exit;

    u mean like this?

    if so.. it still got a internal server error.. :/
    [/quote]

Similar Threads

  1. Replies: 2
    Last Post: December 12th, 2003, 05:25 PM
  2. Access crosstab query column headers
    By shafan in forum Database Management
    Replies: 2
    Last Post: March 1st, 2003, 11:23 PM
  3. headers not working across platforms and browsers
    By dave_mwi in forum PHP Development
    Replies: 5
    Last Post: August 19th, 2002, 08:49 PM
  4. headers for file download of multiple types
    By dave_mwi in forum PHP Development
    Replies: 12
    Last Post: August 14th, 2002, 01:01 PM
  5. session headers problem?
    By tchick in forum PHP Development
    Replies: 3
    Last Post: March 29th, 2001, 03:10 PM

IMN logo majestic logo threadwatch logo seochat tools logo