在PHP中获取完整的URL [英] Get the full URL in PHP
问题描述
我使用此代码来获取完整的URL:
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屋!