Basic HTTP Authentication using .htaccess and .htpasswd

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
desmondlk
Forum Commoner
Posts: 27
Joined: Tue Sep 24, 2002 10:27 pm
Location: Malaysia

Basic HTTP Authentication using .htaccess and .htpasswd

Post by desmondlk »

Dear all,

I am currently using PHP version 4.0.4 with Apache server.

My OS is XP Professional.

Code: Select all

<?php
<?php 
  if(!isset($PHP_AUTH_USER)) { 
    Header("WWW-Authenticate: Basic realm="My Realm""); 
    Header("HTTP/1.0 401 Unauthorized"); 
    echo "Text to send if user hits Cancel button\n"; 
    exit; 
  } else { 
    echo "Hello $PHP_AUTH_USER.<P>"; 
    echo "You entered $PHP_AUTH_PW as your password.<P>"; 
  } 
?> 

?>
I create a file named authen.php with the code above.
And I try to execute the file, it display internal server error.

Do I need to create .htaccess and .htpasswd files?
How both should be enclosed with?
Or other configuration in the server?

please help. Thanks in advance.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

This link should shed some light on your problem:

http://www.javascriptkit.com/howto/htaccess.shtml
Post Reply