我怎样才能在 PHP 中获取完整的 URL,而不仅仅是其中的一部分


How can I get the full URL in PHP, not just part of it?

这是我当前的URL结构http://wp.raddyx.in/consultant-dietitians/staff-single/#Fiona%20Brown

我正在使用echo $url=$_SERVER['REQUEST_URI'];

但是,它仅显示/consultant-dietitians/staff-single/,我想在其中获取完整的URL。

使用 $_SERVER["SERVER_NAME"] 获取主机名wp.raddyx.in

使用 $_SERVER["HTTPS"] 检查 http 与 https。

您可能还需要$_SERVER["SERVER_PORT"]以及一些可以出现在 URL 中的其他杂项内容(如 PHP_AUTH_USER

你不能获取URL的哈希部分(#Fiona%20Brown),因为哈希只是客户端的。它们不会发送到服务器。

相关手册页:http://php.net/manual/en/reserved.variables.server.php

使用 $_SERVER[HTTP_HOST]

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