使用 php 脚本保护 lighttpd 中的目录

发布于 2024-08-06 02:02:37 字数 68 浏览 1 评论 0原文

我想保护目录并针对 mysql 数据库对用户进行身份验证。我正在使用 lighttpd,但无法找到这样做的方法。是否可以?

I would like to protect a directory and authenticate users against a mysql database. I am using lighttpd and haven't been able to find a way of doing so. Is it possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

思念绕指尖 2024-08-13 02:02:37

您可以使用mod_auth,这里是相关的文档页面,

因为它有无法直接访问数据库,我建议使用“htdigest”方法,并从数据库用户重新生成文件。

“htdigest”格式只是:“user:realm:md5(password)”,如页面中所述。

从 php 脚本生成这样的文件应该非常简单。
伪代码:

foreach ($users as $user) {
    // $user['md5pass'] = md5($user['password']);
    $line = sprintf("%s:%s:%s\n", $user['username'], 'protected', $user['md5pass']);
    file_put_contents('htdigest-file', $line, FILE_APPEND);
}

此外,在同一页面上,这里是 mod_auth 的 lighttpd 配置示例:

auth.backend                   = "htdigest" 
auth.backend.htdigest.userfile = "lighttpd-htdigest.user" 

auth.require = ( "/download/" =>
                 (
                 # method must be either basic or digest
                   "method"  => "digest",
                   "realm"   => "download archiv",
                   "require" => "user=agent007|user=agent008" 
                 ),
                 "/server-info" =>
                 (
                 # limit access to server information
                   "method"  => "digest",
                   "realm"   => "download archiv",
                   "require" => "valid-user" 
                 )

)

You could use mod_auth, here is the relevant doc page

Since it has no direct access to a database, i would recommend using the 'htdigest' method, and regenerating the file from your database users.

the 'htdigest' format is just: "user:realm:md5(password)", as explained in the page.

Generating a file like this from a php script should be extremely simple.
pseudo-code:

foreach ($users as $user) {
    // $user['md5pass'] = md5($user['password']);
    $line = sprintf("%s:%s:%s\n", $user['username'], 'protected', $user['md5pass']);
    file_put_contents('htdigest-file', $line, FILE_APPEND);
}

Also, from the same page, here is a sample lighttpd configuration for mod_auth:

auth.backend                   = "htdigest" 
auth.backend.htdigest.userfile = "lighttpd-htdigest.user" 

auth.require = ( "/download/" =>
                 (
                 # method must be either basic or digest
                   "method"  => "digest",
                   "realm"   => "download archiv",
                   "require" => "user=agent007|user=agent008" 
                 ),
                 "/server-info" =>
                 (
                 # limit access to server information
                   "method"  => "digest",
                   "realm"   => "download archiv",
                   "require" => "valid-user" 
                 )

)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文