获取 PHP 中的完整 URL [英] Get the full URL in PHP
问题描述
我使用此代码获取完整网址:
I use this code to get the full URL:
$actual_link = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
问题是我在我的 .htaccess
中使用了一些掩码,所以我们在 URL 中看到的并不总是文件的真实路径.
The problem is that I use some masks in my .htaccess
, so what we see in the URL is not always the real path of the file.
我需要的是获取 URL,URL 中写的内容,仅此而已——完整的 URL.
What I need is to get the URL, what is written in the URL, nothing more and nothing less—the full URL.
我需要了解它在 Web 浏览器的导航栏中的显示方式,而不是服务器上文件的真实路径.
I need to get how it appears in the Navigation Bar in the web browser, and not the real path of the file on the server.
推荐答案
看看$_SERVER['REQUEST_URI']
,即
$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
(注意双引号字符串语法是完全正确)
(Note that the double quoted string syntax is perfectly correct)
如果你想同时支持 HTTP 和 HTTPS,你可以使用
If you want to support both HTTP and HTTPS, you can use
$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
编者注:使用此代码具有安全隐患.客户端可以将 HTTP_HOST 和 REQUEST_URI 设置为它想要的任意值.
Editor's note: using this code has security implications. The client can set HTTP_HOST and REQUEST_URI to any arbitrary value it wants.
这篇关于获取 PHP 中的完整 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!