Friday, March 30, 2007

PHP: HTTP authentication

Using HTTP authentication. Many free PHP hosting service locks custom .htaccess because they usually make money by redirecting 404 or 500 server error to their AD page. This usually leads to lock of password protected directory in free hosting account. Following steps are how I made PHP files password protected without having .htaccess file. It's little outdated.

<?php if( !isset($_SERVER['PHP_AUTH_USER']) ) { header('WWW-Authenticate: Basic realm="Private"'); header('HTTP/1.0 401 Unauthorized'); echo 'Login/Password required'; exit; } $uname = $_SERVER['PHP_AUTH_USER']; $upass = $_SERVER['PHP_AUTH_PW']; if( ($uname != 'user') || ($upass != 'password') ) { echo 'Wrong Login/Password'; exit; } // do something ?>
Using digest HTTP authentication. I've never done using this method before. So I advise you to read this.

No comments: