尝试在 PHP 中获取没有文件名的完整 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3602323/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:22:36  来源:igfitidea点击:

Trying to get a full URL without filename in PHP

php

提问by MrVimes

I thought this would be simple but I can't seem to find a variable of $_SERVERarray that has what I'm looking for.

我认为这很简单,但我似乎无法找到$_SERVER具有我正在寻找的数组变量。

Let's say my url is http://example.com/subdirectory/index.phpI want to get all but the filename - http://example.com/subdirectory/.

假设我的 url 是http://example.com/subdirectory/index.php我想要获取除文件名之外的所有内容 - http://example.com/subdirectory/

I know I could quite easily do this with some string manipulation, but I want to know if there's a var of the _serverarray 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.

我知道我可以很容易地通过一些字符串操作来做到这一点,但我想知道是否有_server我刚刚缺少的数组变量。我已经尝试了所有这些,看看他们给了什么,我可以得到任何东西,但我正在寻找什么。

回答by Lèse majesté

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.

不会有一个,因为它不是一个有用的跟踪属性。如果当前脚本不是index.*那么它不会返回当前脚本的正确 URL。

However, this might get you what you want:

但是,这可能会让你得到你想要的:

echo '//'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);

Edit:
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 $_SERVERglobal 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.

编辑:
此外,除了为 Firefox 安装 php.net 在线手动搜索工具并使用搜索关键字对其进行设置(这将使您可以$_SERVER通过php $_server在 URL 栏中键入类似“ ”的内容来提取全局文档),您还应该知道这一点,var_dump()print_r()让您输出任何变量的完整内容,包括数组和对象。这对于调试非常有用。

回答by Hyman Fuchs

In your case, string manipulation is the best solution.

在您的情况下,字符串操作是最好的解决方案。

For Example:

例如:

substr($_SERVER['REQUEST_URI'], 0, strrpos($_SERVER['REQUEST_URI'], '/') + 1);

回答by Carlos Ouro

There are no defined variables for what you want.

没有你想要的定义变量。

You can use the code below to get the full base url:

您可以使用以下代码获取完整的基本网址:

// Base folder url
$myBase =  ( (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on')) ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);

but hopefully most times you won't require the full thing just use:

但希望大多数时候你不需要完整的东西,只需使用:

// Base folder server root path
$myBase = dirname($_SERVER['PHP_SELF']);

Just be careful if you're using some kind of routes system (mod_rewrite).

如果您使用某种路由系统(mod_rewrite),请小心。

回答by Your Common Sense

I can't seem to find a variable of $_SERVER array that has what I'm looking for.

我似乎找不到具有我正在寻找的 $_SERVER 数组的变量。

because it's fictional string, existing only in the browser's address bar.
It's parts being sent to server separated.

因为它是虚构的字符串,只存在于浏览器的地址栏中。
它是分开发送到服务器的部分。

I want to know if there's a var of the _server array

我想知道是否有 _server 数组的 var

That's very easy to know. just print_r($_SERVER);really simple.

这很容易知道。只是print_r($_SERVER);很简单的。

I know I could quite easily do this with some string manipulation

我知道我可以很容易地通过一些字符串操作来做到这一点

yeah. the code you have to write is less than this question text.

是的。您必须编写的代码少于此问题文本。

but with some manual magic it can be reduced to just one function.

但是通过一些手动魔术,它可以减少到只有一个功能。

回答by Jonathan Kuhn

you can use dirname()to get the path of a url.

您可以使用dirname()获取 url 的路径。