php coding website password protect

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Porkie999
    New Member
    • Sep 2007
    • 19

    php coding website password protect

    hi i have used this code in my website but have a problem with the header parts where is the problem the error is


    [code=php]
    <?php

    $myusername = "myusername ";
    $mypassword = "mypassword ";
    $areaname = "My Protected Area";

    if ($_SERVER ["PHP_AUTH_U SER"] == ""||$_SERVE R ["PHP_AUTH_P W"] == ""||
    $_SERVER["PHP_AUTH_U SER"]!= $myusername ||$_SERVER["PHP_AUTH_P W"]!=
    $mypassword) {
    header ("HTTP/1.0 401 Unauthorized");
    header ("WWW-Authenticate: Basic realm =\"$areaname\"" );


    echo"<h1>Author ization Required.</h1>";
    die();
    }

    ?>
    [/code]
    ERRORS
    Warning: Cannot modify header information - headers already sent by (output started at /home/afusionc/public_html/Auth.php:6) in /home/afusionc/public_html/Auth.php on line 15

    Warning: Cannot modify header information - headers already sent by (output started at /home/afusionc/public_html/Auth.php:6) in /home/afusionc/public_html/Auth.php on line 16

    please tell me where iam goin wrong
    regards
    george thanks for all help
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, George.

    You are getting this error because your script outputs something before it has a chance to send the header data.

    Is there any whitespace before your opening <?php tag, by any chance?

    Comment

    • ronverdonk
      Recognized Expert Specialist
      • Jul 2006
      • 4259

      #3
      I will show the text of an article by wildteen88, june 2006, who can explain this a lot better then I can.

      The reason why you are getting this message is because you have may have/be:
      • Whitespace before the opening php tag <?php
      • Outputting something to the browser before you use session_start, header, setcookie etc

      session_start, setcookie, header and a few other functions write header information to the web server/browser what to do. Such as when you use session_start it requests the browser to create a cookie which stores the PHPSESSID.

      If you have output before you use these functions then they are unable to send new header information as it has already been sent in the form text/html, and so you get the headers already sent error message.

      You can easily find the source of the problem by looking at the error message. As the answer is actully in there, but most people dont notice it as they may not understand what the error means. So lets take this error message as an example:

      Warning: Cannot modify header information - headers already sent by (output started at C:\server\www\t est.php:6) in C:\server\www\t est.php on line 8
      Now PHP has given us a clue here as it has told use where the output has started! Have look where it says output started at and it gives you the full path to the file and the line number. Now the line number that it stats is not usually where the output has started but where the output has ended, becuase the output could be above that line.
      So lets look at test.php:
      [code=php]
      1 <html>
      2 <head>
      3 <title>Header test</title>
      4 </head>
      5 <body>
      6 <?php
      7
      8 header("Locatio n: http://www.google.com" );
      9
      10 ?>
      11 </body>
      12 </html>[/code]As you can see line 6 is the opening PHP tag (< ?php) this isn't the output but look above that line you'll notice it has some HTML code. This html code is the cause of the error!

      So when you get this error message again look for the clue where it says output started at and goto the file and line number it says. Look above the line number and find where your output is located to.

      Hope that helps you understand why your are getting this error message. (Thanks to wildteen88)

      Ronald
      Last edited by pbmods; Sep 20 '07, 11:24 PM. Reason: Changed [CODE] to [CODE=php].

      Comment

      • Porkie999
        New Member
        • Sep 2007
        • 19

        #4
        hi guys i still dont understand what you mean.so here is the full code of exactly what it looks like on dreamweaver.ple ase edit the code so i can understand where iam going wrong.

        regards
        george
        thanks for all replies
        PHP CODE[code=php]
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
        <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <title>Please Login</title>

        <?php
        $myusername = "myusername ";
        $mypassword = "mypassword ";
        $areaname = "My Protected Area";

        if ($_SERVER ["PHP_AUTH_U SER"] == ""||$_SERVE R ["PHP_AUTH_P W"] == ""||
        $_SERVER["PHP_AUTH_U SER"]!= $myusername ||$_SERVER["PHP_AUTH_P W"]!=
        $mypassword) {
        header ("HTTP/1.0 401 Unauthorized");
        header ("WWW-Authenticate: Basic realm =\"$areaname\"" );


        echo"<h1>Author ization Required.</h1>";
        die();
        }

        ?>
        </head>

        <body>[/code]


        my main text.
        </body>
        </html>
        Last edited by pbmods; Sep 21 '07, 05:17 PM. Reason: Added CODE tags.

        Comment

        • bergy
          New Member
          • Mar 2007
          • 89

          #5
          The HTML before your PHP is what's causing those errors...

          This Code: [HTML]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
          <html xmlns="http://www.w3.org/1999/xhtml">
          <head>
          <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
          <title>Please Login</title>
          [/HTML]
          Should be under the PHP - so your code should look like this:[PHP]
          <?php
          $myusername = "myusername ";
          $mypassword = "mypassword ";
          $areaname = "My Protected Area";

          if ($_SERVER ["PHP_AUTH_U SER"] == ""||$_SERVE R ["PHP_AUTH_P W"] == ""||
          $_SERVER["PHP_AUTH_U SER"]!= $myusername ||$_SERVER["PHP_AUTH_P W"]!=
          $mypassword) {
          header ("HTTP/1.0 401 Unauthorized");
          header ("WWW-Authenticate: Basic realm =\"$areaname\"" );
          die();
          }
          ?>
          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
          <html xmlns="http://www.w3.org/1999/xhtml">
          <head>
          <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
          <title>Please Login</title>
          </head>

          <body>
          my main text.
          </body>
          </html>
          [/PHP]

          If you want to add your Authorization Required string, I suggest adding some PHP tags after <body>, checking a variable you set to see if someone is logged in or not, and based on that outputting "my main text" or "authorizat ion required".

          The reason you get this error is because absolutely NOTHING can be sent to the browser (even one space before your beginning <?php tag) before you use the header(); function.

          Comment

          • Porkie999
            New Member
            • Sep 2007
            • 19

            #6
            thanks alot for all replies greatly appreciated.

            Cheers
            George

            very happy :)

            Comment

            • Porkie999
              New Member
              • Sep 2007
              • 19

              #7
              ok sorry to multipost but how would i add more names to the login list? sorry i normally dont use php

              regards
              george

              Comment

              • bergy
                New Member
                • Mar 2007
                • 89

                #8
                Try this:

                [PHP]
                <?php
                $users = array("user1", "user2", "user3");
                $pwrds = array("pw1", "pw2", "pw3");
                $loginSuccessfu l = false;
                for($i = 0; $i < count($users); $i++){
                if($users[$i] == $_SERVER["PHP_AUTH_U SER"]){
                if($pwrds[$i] == $_SERVER["PHP_AUTH_P W"]){
                $loginSuccessfu l = true;
                }
                }
                }
                if($loginSucces sful){
                //Login Successful!!
                }else{
                //Error Message!!
                }
                ?>
                [/PHP]

                Comment

                • Porkie999
                  New Member
                  • Sep 2007
                  • 19

                  #9
                  hi erm that code i carnt get to work, i dont know why but it just dousnt show any boxes etc???

                  any help here?

                  or can i just modify code off the one that i did?

                  Comment

                  • bergy
                    New Member
                    • Mar 2007
                    • 89

                    #10
                    Yes, I'm sorry for not being more specific. You're going to want to add the snippet I posted to your above code, you still need the following to make it work. [php]
                    header ("HTTP/1.0 401 Unauthorized");
                    header ("WWW-Authenticate: Basic realm =\"$areaname\"" );
                    die();[/php]
                    You just need to add in my snippet to have multiple logins.

                    Comment

                    Working...