通过 PHP 验证 Apache 目录
大家好,
我设置了一个目录,其中包含一些 Apache Auth 内容。这是目录: http://gojalapeno.com/intranet/content/ado-jeune
使用 ado-jeune :tezesyrab 登录。
下面是为其提供支持的 .htaccess 代码:
AuthName "Connexion au dossier:"
AuthType Basic
AuthUserFile "D:\Dropbox\htaccess\secret\.htpasswd"
Require valid-user
现在该目录的登录表单是由浏览器生成的,但我希望为此使用我自己的 HTML 表单。我该怎么做呢?我尝试过在 PHP 中使用 cURL:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://gojalapeno.com/intranet/content/ado-jeune');
curl_setopt($ch, CURLOPT_USERPWD, 'ado-jeune:tezesyrab');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
curl_close($ch);
这验证得很好,但问题是它不会重定向到页面(尽管有 CURLOPT_FOLLOWLOCATION),它只是将其“包含”在当前文件中。因此,我所有的相关链接都被破坏了。
因此,我正在寻找一种方法,使用我自己的 HTML 表单对 Apache Basic Auth 目录进行身份验证,然后重定向到该目录。
谢谢!
Good day everyone,
I have a directory set up with some Apache Auth stuff. Here is the directory:
http://gojalapeno.com/intranet/content/ado-jeune
Use ado-jeune:tezesyrab to login.
Here is the code for the .htaccess that powers it:
AuthName "Connexion au dossier:"
AuthType Basic
AuthUserFile "D:\Dropbox\htaccess\secret\.htpasswd"
Require valid-user
Now the login form for that directory is generated by the browser, but I wish to use my own HTML form for this. How can I do it? I've tried using cURL with PHP:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://gojalapeno.com/intranet/content/ado-jeune');
curl_setopt($ch, CURLOPT_USERPWD, 'ado-jeune:tezesyrab');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
curl_close($ch);
That authenticates fine, but the problem with that is that it doesn't redirect to the page (despite the CURLOPT_FOLLOWLOCATION), it merely "includes" it in the current file. As such, all my relative links are broken.
So what I'm looking for is a way to use my own HTML form to authenticate to an Apache Basic Auth directory, and to redirect to that directory after.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不幸的是,由于 AuthBasic 的工作方式,您所描述的内容将很难实现。来自 Apache 文档:
在用户输入用户凭据后,浏览器会自动为每个后续请求发送用户凭据。
但在您的情况下,浏览器本身并没有连接到受保护的目录,而是 cURL 。一种解决方案是让 cURL 处理对该受保护目录的所有请求,本质上充当代理。
要实现此目的,您必须编写一个 PHP 脚本来请求用户的凭据,并在请求受保护目录中的页面时将这些凭据一起发送。所以像这样:
希望这有帮助!
Unfortunately, what you're describing is going to be difficult to implement due to the way that AuthBasic works. From Apache's documentation:
Browsers handle sending the user's credentials automatically for each subsequent request after the user enters them in.
But in your case, the browser itself is not connecting to the protected directory, but rather cURL is. One solution would be to have cURL process all requests to that protected directory, in essence acting as a proxy.
To accomplish this, you would have to write a PHP script that requests the user's credentials and sends them along as it requests a page in the protected directory. So something like this:
Hope this helps!
也许您可以定义自己的错误文档,其中包含
.htaccess
中的表单:请参阅 http://httpd.apache.org/docs/2.0/custom-error.html
Maybe you can define your own error document that includes the form in
.htaccess
:See http://httpd.apache.org/docs/2.0/custom-error.html