您的位置:首页 > 编程语言 > PHP开发

php 获取当前页面url_PHP获取URL –如何获取当前页面的完整URL

2020-08-19 23:33 1376 查看

php 获取当前页面url

In this PHP-focused article, we will explore how to get the URL of the current page in the PHP programming language.

在这篇以PHP为重点的文章中,我们将探索如何以PHP编程语言获取当前页面的URL。

You may want to get the current page URL for the following reasons:

出于以下原因,您可能需要获取当前页面的URL:

  • Building internal links

    建立内部链接
  • Using filters with GET requests, for example, currentURL.com?myFilterParameter=Food

    对GET请求使用过滤器,例如currentURL.com?myFilterParameter=Food

PHP actually stores lots of useful information as users navigate your web application. One of these is, of course, the current URL.

当用户浏览您的Web应用程序时,PHP实际上存储了许多有用的信息。 其中之一就是当前URL。

PHP stores these pieces of useful information in its array of super-global variables.

PHP将这些有用的信息存储在其超全局变量数组中。

什么是Superglobals? (What are Superglobals?)

Superglobals are already defined variables by the PHP engine which can be used in any kind of scope. They are readily available at any one time.

超全局变量是PHP引擎已定义的变量,可以在任何范围内使用。 它们随时可以随时使用。

There are many of these superglobals, but the one we are interested in is the $_SERVER superglobal.

这些超全局变量很多,但我们感兴趣的是$ _SERVER超级全局变量。

$ _SERVER超级全局 (The $_SERVER Superglobal)

The $_SERVER superglobal variable has many properties that are accessible with an associative style index.  

$ _SERVER超全局变量具有许多可通过关联样式索引访问的属性。

Some of the values we can access include:

我们可以访问的一些值包括:

  • HTTP_USER_AGENT

    HTTP_USER_AGENT
  • HTTP_HOST

    HTTP_HOST
  • HTTP_ACCEPT_ENCODING

    HTTP_ACCEPT_ENCODING
  • HTTP_ACCEPT

    HTTP_ACCEPT

You can see more of these indicies in the PHP documentation here.

您可以在此处PHP文档中看到更多这些标记

那么,我们如何获得完整的URL? (So, how do we get the full URL?)

With the above points on superglobals and the $_SERVER superglobal in mind, we can go ahead and get the current page's URL.

考虑到以上有关超级全局变量和$ _SERVER超级全局变量的知识,我们可以继续获取当前页面的URL。

In the following screenshot, I've rendered a PHP application in a local environment in a page named "home."

在下面的屏幕截图中,我已经在本地环境中名为“ home”的页面中渲染了一个PHP应用程序。

The URL is http://localhost/home.

URL是http:// localhost / home

In the code base of this page, I'm going to use the $_SERVER variable.

在此页面的代码库中,我将使用$ _SERVER变量。

With this variable, we will have to use 2 separate indices to get each part of the current page's URL. The first part will be the host, localhost, and the second part will be the page name, home.

使用此变量,我们将必须使用2个单独的索引来获取当前页面URL的每个部分。 第一部分将是主机localhost,第二部分将是页面名称home。

The first index we will use is HTTP_HOST - The current web address host, for example localhost, or example.com

我们将使用的第一个索引是HTTP_HOST-当前的网址主机,例如localhost或example.com

The second is REQUEST_URI which will give us the part of the URL after the host, so this is anything after localhost or example.com

第二个是REQUEST_URI ,它将为我们提供主机后面的URL部分,因此这是localhost或example.com之后的任何内容

Let's see this in action:

让我们来看看实际情况:

$currentPageUrl = 'http://' . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];

echo "Current page URL " . $currentPageUrl;

输出量 (Output)

And that is it - pretty straightforward!

就是这样-非常简单!

摘要 (Summary)

The $_SERVER superglobal variable stores a lot of vital information for modern day use-cases. As we've discovered in this instance, getting the current page's URL is made simple with the ability to access this specific variable.

$ _SERVER超全局变量存储了现代用例的许多重要信息。 正如我们在本例中发现的那样,通过访问此特定变量的功能,获取当前页面的URL非常简单。

It's worth checking out the documentation to see what other indices are available, though, as it's good to have in mind how helpful this variable can be.

不过,值得查阅文档以查看还有哪些其他索引可用,因为牢记此变量的有用性是一件好事。

I hope you enjoyed this article! If you did, feel free to check out my blog, https://www.codewall.co.uk/

希望您喜欢这篇文章! 如果您愿意,请随时查看我的博客https://www.codewall.co.uk/

翻译自: https://www.freecodecamp.org/news/php-get-url-how-to-get-the-full-url-of-the-current-page/

php 获取当前页面url

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: