Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@mikaelkundert
Created September 9, 2009 11:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikaelkundert/183644 to your computer and use it in GitHub Desktop.
Save mikaelkundert/183644 to your computer and use it in GitHub Desktop.
<?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