<?php

function authenticate($username, $password) {
   $ret = false;
   $ct = "";
   $fp = fopen("/home/music/pass.txt", "r");
   if($fp != null) {
      $ct = trim(fgets($fp, 1024));
      fclose($fp);
   }
   $ct0 = $password;
   if(strcmp($username, "mcoan") == 0
      && strcmp($ct0, $ct) == 0) {
      $ret = true;
   }
   return $ret;
}

if(!isset($_SERVER['PHP_AUTH_USER'])) {
   header('WWW-Authenticate: Basic realm="Music Realm"');
   header('HTTP/1.0 401 Unauthorized');
   echo 'Unathorized...';
   exit;
}
else {
    if(!authenticate($_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"])) {
       header('WWW-Authenticate: Basic realm="Music Realm"');
       header('HTTP/1.0 401 Unauthorized');
       echo 'Unathorized...';
       exit;
    }
}

?>
