你如何让当前用户登录到 apache 领域?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1741838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you get the current user logged in to an apache realm?
提问by Marcus Stade
I'm writing a simple in-house frontend to Subversion. We've got an Apache setup serving up the SVN repositories thanks to WebDAV. Also, authentication is done through an Apache realm and Open Directory. For what it's worth, this is basically a pretty vanilla Mac OS X Server 10.6 setup.
我正在为 Subversion 编写一个简单的内部前端。多亏了 WebDAV,我们有了一个 Apache 设置来提供 SVN 存储库。此外,身份验证是通过 Apache 领域和开放目录完成的。就其价值而言,这基本上是一个非常普通的 Mac OS X Server 10.6 设置。
Now, some of the tasks our front end is responsible of kicking off need to know the username of the user calling the action. For instance, creating a repository needs it so that we get proper logs of who actually created it. If I don't supply this information, SVN just uses the process that created it which in our case is the user running httpd.
现在,我们的前端负责启动的一些任务需要知道调用该操作的用户的用户名。例如,创建存储库需要它,以便我们获得实际创建它的人的正确日志。如果我不提供这些信息,SVN 只会使用创建它的进程,在我们的例子中是运行 httpd 的用户。
I'm doing this in php, but I don't necessarily have to implement this using php. If I can get the information in a shell script that's fine too. What matters is that I somehow get the information. The solution I first developed, which I assumed worked since it correctly reported my user name, is simply calling:
我在 php 中执行此操作,但我不一定必须使用 php 来实现它。如果我可以在 shell 脚本中获取信息,那也很好。重要的是我以某种方式获得了信息。我最初开发的解决方案,我认为它有效,因为它正确报告了我的用户名,只是调用:
get_current_user();
However, this seems to alwaysreturn my user name, even though some other user is kicking off the action while being logged in to the realm. Is there a way to get the correct user?
但是,这似乎总是返回我的用户名,即使其他一些用户在登录领域时开始了该操作。有没有办法获得正确的用户?
EDIT:I may have been a bit too quick to jump the gun here. It looks as though the former variable doesn't get set after a certain amount of time. However, the user is still logged in as there is no request to login. Thus, the below suggested solution is not really applicable.
编辑:我可能有点太快了,无法在这里开枪。看起来好像前一个变量在一段时间后没有设置。但是,用户仍处于登录状态,因为没有登录请求。因此,以下建议的解决方案并不真正适用。
Now I feel like an idjit. This simple snippet worked (although it was difficult to find):
现在我觉得自己像个idjit。这个简单的片段有效(虽然很难找到):
$username = $_SERVER["PHP_AUTH_USER"];
However, this did not:
然而,这并没有:
$username = apache_getenv("REMOTE_USER");
Anyone got an idea as to why the latter doesn't work? The only docs I could find suggest that this only works on Apache 2. However, I am in fact running Apache 2 so that couldn't be it.
任何人都知道为什么后者不起作用?我能找到的唯一文档表明这仅适用于 Apache 2。但是,我实际上运行的是 Apache 2,所以不可能。
回答by Marcus Stade
So, after having googled for a long while I've realized that the method for getting the user name have changed with pretty much every passing PHP version. The current solution which seems to be the one is to get the data from the following variable:
所以,在谷歌搜索了很长时间之后,我意识到获取用户名的方法几乎随着每个 PHP 版本而改变。当前的解决方案似乎是从以下变量中获取数据:
$_SERVER['REMOTE_USER'];
However, this variable may or may not be set. The reason why I probably did see the information correctly was that the information was sent in the headers when I logged in to the realm and then visited the PHP script which was outside of said realm. The information did arrive, but wasn't reliable.
然而,这个变量可能会也可能不会被设置。我可能确实正确地看到了信息的原因是,当我登录到该领域然后访问该领域之外的 PHP 脚本时,该信息是在标题中发送的。信息确实到达了,但并不可靠。
The Apache docsactually tell you this in an "Oh BTW"-fashion. I'm not sure if the authentication is carried over across multiple realms on the same host, but in any case, that solved the problem.
在Apache的文档实际上告诉你这一个“哦,顺便说一句” -时尚。我不确定身份验证是否在同一主机上的多个领域中进行,但无论如何,这解决了问题。

