Save below as password.inc and just include it at the top of any page you want to password protect. The $PHP_AUTH_USER is nice because it acts like a session variable. ie. once set you can use it anytime without asking or setting it again.
<?php
function authenticate(){
Header("WWW-authenticate: basic realm=\"www.yourpagehere.com\"");
Header("HTTP/1.0 401 Unauthorized");
echo "
You need your name and password for this.
";
exit;
}
if(!$PHP_AUTH_USER){
authenticate();
}
else{
$query = mysql_query("select name from table_name where name='$PHP_AUTH_USER' and password='$PHP_AUTH_PW'");
if(!mysql_num_rows($query)){
authenticate();
}
}
?>