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_AGENTHTTP_USER_AGENT
  • HTTP_HOSTHTTP_HOST
  • HTTP_ACCEPT_ENCODINGHTTP_ACCEPT_ENCODING
  • HTTP_ACCEPTHTTP_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

php 获取当前页面url_PHP获取URL –如何获取当前页面的完整URL相关推荐

  1. PHP/JS中获取当前页面的完整URL

    PHP/JS中获取当前页面的完整URL javascript实现: top.location.href 顶级窗口的地址this.location.href 当前窗口的地址 php实现 PHP实现 #测 ...

  2. iis服务器指定页面index,如何在Windows / IIS服务器上获取当前页面的完整URL?

    白衣非少年 对于Apache:'http'.(empty($_SERVER['HTTPS'])?'':'s').'://'.$_SERVER['SERVER_NAME'].$_SERVER['REQU ...

  3. PHP中获取当前页面的完整URL

    Javascript top.location.href 顶级窗口的地址 this.location.href 当前窗口的地址 PHP实现 #测试网址: http://localhost/blog/t ...

  4. PHP获取当前页面的完整URL

    javascript实现: top.location.href 顶级窗口的地址 this.location.href 当前窗口的地址 PHP实现 #测试网址: http://localhost/blo ...

  5. 怎样获取当前页面值php,想要得到当前页面的所有url参数信息怎么用PHP来实现?...

    本篇文章主要给大家介绍怎么使用php获取完整url.首先给新手小白们简单介绍下什么是url.百度百科上是这么解说的,统一资源定位符是对可以从互联网上得到的资源的位置和访问方法的一种简洁的表示,是互联网 ...

  6. 请教!如何实现:在微信中,分享出去的 URL 不是当前页面的 URL

    类似下面这个地址中的功能. 在微信中打开下面这个 URL  http://play.11h5.com/legend 进入之后,资源代下载网他会自动跳转到下面这个 URL http://play.11h ...

  7. Flutter由其他页面回到当前页面时监听并刷新当前页面的内容

    项目中经常遇到这样的场景,比如很多App都有的收藏.点赞列表等,当我们从收藏列表点击一个item项进入到详情页时,用户点击了取消收藏,那么当我们点击返回到收藏列表页时,App需要主动刷新收藏列表的数据 ...

  8. php 伪静态 获取当前页面路径_php伪静态如何获取地址栏url -

    PHP+MYSQL/开发 视频教程 解决方法 [img]http://www.caopeng.org/html/image/php.jpg[/img] 曹鹏 PHP+MYSQL 为网络开发提供流行动力 ...

  9. flask从服务器获取html页面,flask的ajax、获取服务器数据、放到前端页面、如果数据存在显示标签、如果不存在不显示标签...

    -------------------------------------第一部分----------------------------------------------------------- ...

最新文章

  1. Linux权限管理 - 特殊权限之文件特殊权限
  2. Smartform 动态打印选择屏幕上传的图片
  3. 在 Red HatAS4下添加网卡驱动!!
  4. 路由选项卡_M32全面培训第四讲——路由配置
  5. ES启动异常:max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
  6. 游戏会记录某个api的调用_专家坐诊丨老出BUG怎么办?游戏服务器常见问题的解决方法分享...
  7. 【python】列表的定义与操作
  8. Unity 2D角色复活点与复活等待时间设置
  9. 三步教你手动破解网易云音乐加密
  10. 洛谷P1067-多项式输出(模拟好题!)
  11. html5银行理财项目实践作业,金融理财产品实践报告.doc
  12. 九宫格一条线连起来_9个圆圈用4条线连起来-九个点用四条线连接-数学-霍甲心同学...
  13. 【2021年蓝桥杯Java-B组国赛题解】
  14. 2018年我跑去做淘宝了(续篇)
  15. 算法学习之:全排列问题的递归算法(Perm)
  16. 如何关闭电脑自动更新?方案三部曲带你走出自动更新的阴影
  17. socket连接 error 113 /111 的解决方法
  18. 批量转换图像格式Irfanview之CR2图片转Jpeg
  19. css3 text-overflow制作固定区域的博客列表
  20. 中国邮件群发服务商现状分析

热门文章

  1. Java—接口(工厂模式代理模式)
  2. 员工评价系统 winform
  3. 设置 chrome 无头浏览器
  4. requests-session类对象-0223
  5. Laravel Eloquent关联模型查询设置查询条件与指定字段
  6. zabbix监控系统时间的问题
  7. 理解SVN中trunk,branches,tags
  8. java 判断字符串是否为数字
  9. 13-5 15 xshell使用xftp pure-ftpd
  10. 所生成项目的处理器架构“MSIL”与引用“***”的处理器架构“x86”不匹配。这种不匹配可能会导致运行时失败。请考虑通过配置管理器......