尝试在PHP中获取不带文件名的完整URL [英] Trying to get a full URL without filename in PHP
问题描述
我以为这很简单,但是我似乎找不到具有所要查找内容的$_SERVER
数组变量.
I thought this would be simple but I can't seem to find a variable of $_SERVER
array that has what I'm looking for.
假设我的网址是http://example.com/subdirectory/index.php
,我想获取除文件名-http://example.com/subdirectory/
之外的所有内容.
Let's say my url is http://example.com/subdirectory/index.php
I want to get all but the filename - http://example.com/subdirectory/
.
我知道我可以很容易地通过一些字符串操作来做到这一点,但是我想知道我是否缺少_server
数组的变量.我已经尝试了所有的人,看看他们能给什么,但我能得到想要的东西.
I know I could quite easily do this with some string manipulation, but I want to know if there's a var of the _server
array that I'm just missing. I've tried all of them to see what they give and I can get anything BUT what I'm looking for.
推荐答案
不会有一个,因为它不是要跟踪的有用属性.如果当前脚本不是index.*
以外的其他内容,则不会返回当前脚本的正确URL.
There won't be one because it's not a useful attribute to track. If the current script is something other than index.*
then it will not return the correct URL for the current script.
但是,这可能会给您带来想要的东西:
However, this might get you what you want:
echo '//'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);
另外,除了安装Firefox的php.net在线手动搜索工具并使用搜索关键字进行设置(通过在URL中键入"php $_server
"之类的字词,您可以在$_SERVER
全局文档中找到文档)栏),您还应该知道var_dump()
和print_r()
可以让您输出任何变量的完整内容,包括数组和对象.这对于调试非常有用.
Also, aside from installing the php.net online manual search tool for Firefox and setting it up with a search keyword (which will let you pull up the documentation on the $_SERVER
global by typing something like "php $_server
" into your URL bar), you should also know that var_dump()
and print_r()
lets you output the full contents of any variable, including arrays and objects. This is very useful for debugging.
这篇关于尝试在PHP中获取不带文件名的完整URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!