Created
September 9, 2009 11:20
-
-
Save mikaelkundert/183644 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Requires certain username and password to continue. | |
* | |
* How to use? | |
* Simply add require_login("username", "password", "realm") in your PHP code and it will | |
* require the user to login with given username and password. | |
* | |
* @param string The required username | |
* @param string The required password | |
* @param string The "Basic realm", which is shown to user while asking for username/password | |
* @return boolean Returns TRUE if username and password matches. Otherwise it will exit. | |
*/ | |
function require_login($username, $password, $realm = ""){ | |
// Set up default variables | |
$authed = FALSE; | |
// Checks if we are authenticating | |
if( isset($_SERVER['PHP_AUTH_USER']) ){ | |
if( $_SERVER['PHP_AUTH_USER'] == $username && $_SERVER['PHP_AUTH_PW'] == $password ){ | |
$authed = TRUE; | |
} | |
} | |
// Are we authenticated | |
if( ! $authed ){ | |
header('WWW-Authenticate: Basic realm="'.$realm.'"'); | |
header('HTTP/1.0 401 Unauthorized'); | |
exit; | |
} | |
return TRUE; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment