php $_SERVER["SCRIPT_URI"] 不起作用?选择?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/717874/
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
$_SERVER["SCRIPT_URI"] not working? alternative?
提问by Ahmad Fouad
This is odd, but for some reason the $_SERVER["SCRIPT_URI"]will not return the domain name when I am in child/sub-pages but will only work on the main page. Not sure if its due to the script (WordPress) or host, but please can you suggest any reliable solution to retrieve the domain name with PHP?
这很奇怪,但由于某种原因,$_SERVER["SCRIPT_URI"]当我在子页面/子页面时不会返回域名,而只会在主页上工作。不确定是由于脚本(WordPress)还是主机的原因,但请您提出任何可靠的解决方案来使用 PHP 检索域名?
回答by vartec
If you need domain name, use:
如果您需要域名,请使用:
$_SERVER['HTTP_HOST']
回答by Mike B
When in doubt
有疑问时
var_dump($_SERVER);
回答by Tom Wright
Depending on what you want, I'd use one of the following:
根据您的需要,我将使用以下方法之一:
- $_SERVER['PHP_SELF'] for the script file location
- $_SERVER['SERVER_NAME'] for the host name
- $_SERVER['PHP_SELF'] 为脚本文件位置
- $_SERVER['SERVER_NAME'] 作为主机名
EDIT: Maybe PHP_SELF isn't the best. See comments.
编辑:也许 PHP_SELF 不是最好的。看评论。
回答by Jeremy Stanley
This might be due to URL rewriting, you can try $_SERVER['REQUEST_URI'] instead if you want the path that was called in the url.
这可能是由于 URL 重写,如果您想要在 url 中调用的路径,您可以尝试 $_SERVER['REQUEST_URI'] 代替。
回答by Snsxn
Also, SCRIPT_URI does not exist, the right syntax is REQUEST_URI:
此外,SCRIPT_URI 不存在,正确的语法是 REQUEST_URI:
echo $_SERVER["REQUEST_URI"];It returns the whole path without the host and domain.
echo $_SERVER["REQUEST_URI"];它返回没有主机和域的整个路径。
回答by Mike Craig
I was using $_SERVER[SCRIPT_URI] on my website http://www.a2zidx.comand a number of subdirectories like http://howto.a2zidx.com
我在我的网站http://www.a2zidx.com和许多子目录(如 http://howto.a2zidx.com)上使用 $_SERVER[SCRIPT_URI]
I have had no problem with this for years and today got an error on a number of sites where $_SERVER[SCRIPT_URI] was not assigned. After contacting my isp they claim they have not made any changes but $_SERVER[SCRIPT_URI] no longer works. getenv('SCRIPT_URI') does not fail but returns a null string. Why this should happen suddenly after so many years I do not know. I ended up calling a function to go through various options to extract the filename which is what I wanted. Hope this covers everything. I had trouble including the function but checked "SCRIPT_NAME" "PHP_SELF" "SCRIPT_FILENAME "SCRIPT_URI"
多年来我一直没有遇到这个问题,今天在许多未分配 $_SERVER[SCRIPT_URI] 的站点上出现错误。联系我的 isp 后,他们声称他们没有进行任何更改,但 $_SERVER[SCRIPT_URI] 不再有效。getenv('SCRIPT_URI') 不会失败,但会返回一个空字符串。为什么这么多年后会突然发生我不知道。我最终调用了一个函数来通过各种选项来提取我想要的文件名。希望这涵盖了一切。我在包含函数时遇到了问题,但检查了“SCRIPT_NAME”“PHP_SELF”“SCRIPT_FILENAME”SCRIPT_URI”
Hope his helps.
希望他的帮助。
If anyone knows why SCRIPT_URI would suddenly stop working I would love to know. The server is currently running Apache 2.4.
如果有人知道为什么 SCRIPT_URI 会突然停止工作,我很想知道。服务器当前运行的是 Apache 2.4。
回答by A. Kiyoshi
Late late reply. I use this piece of script to store information in my db. Hope this might help someone. How to get your full url and the ip address of the client access from.
迟迟回复。我使用这段脚本将信息存储在我的数据库中。希望这可以帮助某人。如何获取完整的 url 和客户端访问的 ip 地址。
if (!empty($_SERVER['HTTP_CLIENT_IP'])){
$ip=$_SERVER['HTTP_CLIENT_IP'];
//Is it a proxy address
}elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}else{
$ip=$_SERVER['REMOTE_ADDR'];
}
$url = $_SERVER['SERVER_NAME'].$_SERVER['SCRIPT_NAME'];
echo $ip;
echo "<br>";
echo $url;
回答by Pancho
On Apache, SCRIPT_URI only exists when mod_rewrite is enabled.
在 Apache 上,SCRIPT_URI 仅在启用 mod_rewrite 时存在。
Apache requires:
阿帕奇需要:
rewrite_module (note: use platform specific load syntax)
rewrite_module(注意:使用平台特定的加载语法)
and
和
RewriteEngine On
重写引擎开启
statements in the Apache configuration file at the appropriate locations.
Apache 配置文件中适当位置的语句。
Then restart Apache, and it should work fine.
然后重新启动Apache,它应该可以正常工作。
回答by sepehr
var_dump($_SERVER)and see what suites your needs.
var_dump($_SERVER)看看哪些适合您的需求。
P.S. Making use of unsanitized $_SERVER["PHP_SELF"]could be a security risk.
PS 使用未经消毒的$_SERVER["PHP_SELF"]可能存在安全风险。
回答by randymized
If your browser formats JSONdocuments nicely and no output has taken place, inserting the following will result in something more readable than var_dump:
如果您的浏览器JSON很好地格式化文档并且没有发生任何输出,则插入以下内容将导致比以下内容更具可读性var_dump:
header('Content-Type: application/json');die(json_encode($_SERVER));
phpinfo()will also provide a list of all $_SERVERvalues and so much more!
phpinfo()还将提供所有$_SERVER值的列表等等!

