Page MenuHomePhabricator

basicauth.php
No OneTemporary

basicauth.php

<?php
/**
* This example shows how to do Basic authentication.
* *
* @copyright Copyright (C) 2009-2015 fruux GmbH (https://fruux.com/).
* @author Evert Pot (http://evertpot.com/)
* @license http://sabre.io/license/ Modified BSD License
*/
$userList = [
"user1" => "password",
"user2" => "password",
];
use Sabre\HTTP\Sapi;
use Sabre\HTTP\Response;
use Sabre\HTTP\Auth;
// Find the autoloader
$paths = [
__DIR__ . '/../vendor/autoload.php',
__DIR__ . '/../../../autoload.php',
__DIR__ . '/vendor/autoload.php',
];
foreach ($paths as $path) {
if (file_exists($path)) {
include $path;
break;
}
}
$request = Sapi::getRequest();
$response = new Response();
$basicAuth = new Auth\Basic("Locked down area", $request, $response);
if (!$userPass = $basicAuth->getCredentials()) {
// No username or password given
$basicAuth->requireLogin();
} elseif (!isset($userList[$userPass[0]]) || $userList[$userPass[0]] !== $userPass[1]) {
// Username or password are incorrect
$basicAuth->requireLogin();
} else {
// Success !
$response->setBody('You are logged in!');
}
// Sending the response
Sapi::sendResponse($response);

File Metadata

Mime Type
text/x-php
Expires
Tue, Dec 24, 10:43 AM (20 h, 35 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
915352
Default Alt Text
basicauth.php (1 KB)

Event Timeline