Created
June 20, 2021 19:00
-
-
Save ano/dc7631b360f5411d774c1b29c87689c2 to your computer and use it in GitHub Desktop.
PHP Basic Authentication
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 | |
/* | |
* PHP-Basic-Auth | |
* Author: Ano Tisam | |
* Date: 11/10/2016 | |
* Description: A simple basic auth script. When user tries to access a page that requires | |
* this script it asks for a username and password. Passwords are stored in a $users array as below. | |
*/ | |
function pc_validate($user,$pass) { | |
/* replace with appropriate username and password checking, | |
such as checking a database */ | |
$users = array( | |
'user1' => 'password1', | |
'user2' => 'password2', | |
'user3' => 'password3' | |
); | |
if (isset($users[$user]) && ($users[$user] == $pass)) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
?> |
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 | |
//Include the Library | |
require_once("auth.php"); | |
// How to use BASIC AUTH | |
if (!pc_validate($_SERVER['PHP_AUTH_USER'],$_SERVER['PHP_AUTH_PW'])){ | |
$realm = 'Licensing API '.date('Y-m-d'); | |
header('WWW-Authenticate: Basic realm="'.$realm.'"'); | |
header('HTTP/1.0 401 Unauthorized'); | |
echo "You need to enter a valid username and password."; | |
exit; | |
} | |
//...Your API Code | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment