castanet / userid
模拟 Nginx 的 HTTP Userid 模块
Requires (Dev)
- phpunit/phpunit: 3.7.*
This package is not auto-updated.
Last update: 2024-09-28 12:58:47 UTC
README
此库模拟 Apache 的 mod_uid 或 Nginx 的 User ID 模块。关于 User ID 的想法,请访问以下链接。
目标用户
以下人群
- 使用 Apache HTTP 服务器,
- 通过 Apache 模块使用 PHP,并且
- 不使用 mod_uid Apache 模块
您使用的是 Apache 1.x.x?或者 2.0.x?请访问 mod_uid 页面。
您使用 Nginx?请访问 HttpUseridModule 页面。
安装
将以下内容添加到您的 composer.json 中
"require": {
"castanet/userid": "*"
}
然后执行
composer install
使用方法
PHP
require 'vendor/autoload.php';
$uid = new \Castanet_Userid;
$uid->enable()
->start();
您需要尽可能早地调用 \Castanet_Userid::start()
,因为它内部使用 setrawcookie()
函数,而如果在输出任何字符串后调用 setrawcookie()
,则会发生错误。
Apache 日志
将自定义日志格式添加到 httpd.conf
,如下所示
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" \"%{uid_got}n\" \"%{uid_set}n\"" combined_cookie
然后设置 combined_cookie
为当前日志格式,然后重启 Apache。
uid_got
和 uid_set
变量由此库设置。
设置
启用
Castanet_Userid
即使调用 start()
也不会做任何事情,除非您调用 enable()
(或 setEnabled(true)
)。
$uid = new Castanet_Userid;
$uid->start(); // do nothing
$uid->enable()->start(); // do something
名称(cookie 的键)
name
用作 cookie 的键。默认情况下,使用 uid
。
uid=fwAAAVEbtF1USQfEAwMEAg==
您可以通过 setConfig()
方法修改它
$uid->setConfig('name', 'castanet');
然后修改 cookie 的键
castanet=fwAAAVEbtF1USQfEAwMEAg==
请注意不要使用已用于其他目的的相同键。
服务
service
是一个任意整数,默认为服务器 PHP 处理的 IP 地址(实际上是通过 ip2long()
计算的)。它在日志中显示为前八个字符。例如,设置为 127.0.0.1
,此库记录的日志(作为 uid_got
和 uid_set
)以字符 0100007F
开头。
这些字符本身没有任何意义,但它们充当身份的角色。通过查看它们,您可以知道用户访问的第一个服务器。因此,您可能为通过一个反向代理进行负载均衡的多个服务器设置了相同的值。
您可以这样设置 service
$uid->setConfig('service', ip2long('127.0.0.1'));
Cookie 属性
Cookie 属性(domain
、path
和 expires
)可以通过 setConfig()
方法设置
$uid->setConfig('domain', 'www.example.net')
->setConfig('path', '/sandbox');
一次性设置
使用 setConfigs()
方法,您可以一次性设置上述属性
$uid->setConfigs(array(
'name' => 'castanet',
'service' => ip2long('127.0.0.1'),
'domain' => 'www.example.net',
'path' => '/sandbox'
));
合并的 uid
现在 Castanet Userid 除了 uid_set
和 uid_got
外,还记录了一个 uid
。这是存在的 uid_set
的值,或者如果不存在,则为 uid_set
和 uid_got
的和,它没有键名(uid=
)。
PHP 4
对于 PHP 4,请使用 Castanet_Userid4。