mikechip / php-httpauth

该软件包已被放弃,不再维护。未建议替代软件包。

PHP 7 的 HTTP 身份验证包装器

1.0.0 2019-10-26 21:01 UTC

This package is auto-updated.

Last update: 2024-06-27 07:52:35 UTC


README

HTTP 身份验证包装器

库提供简单的 HTTP 身份验证

已弃用

该库已经很久远了,可能已经过时。如果想要使用它,请创建分支。

安装

只需下载并在 src 中包含类,或者使用 Composer

composer require mikechip/php-httpauth

示例使用

    require_once('vendor/autoload.php');
    
    $auth = new Mike4ip\HttpAuth();
    $auth->addLogin('admin', 'test');
    $auth->addLogin('foo', 'bar');
    $auth->requireAuth();
    
    print('This is your hidden page');

定制

    require_once('vendor/autoload.php');

    /*
     * HTTP Auth with customization
     */
    $auth = new Mike4ip\HttpAuth();
    $auth->setRealm('Pass login and password');
    
    // Set unauthorized callback
    $auth->onUnauthorized(function() {
        print("<h1>403 Forbidden</h1>");
        die;
    })->setCheckFunction(function($user, $pwd) {
        // List of logins => passwords
        $users = [
        'admin' => 'test',
        'foo' => 'bar'
        ];
    
        // Returns true if login and password matches
        return (isset($users[$user]) && $users[$user] === $pwd);
    })->requireAuth();

    print('This is your hidden page');

反馈

使用 问题 联系我