如何使用 PHP 获取当前页面的完整 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25968071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 18:04:54  来源:igfitidea点击:

How to get the full URL of the current page using PHP

phpwordpress

提问by Manish Jesani

What is the "less code needed" way to get parameters from an URL query string which is formatted like the following?

从格式如下的 URL 查询字符串获取参数的“所需代码更少”的方法是什么?

My current url

我现在的网址

www.mysite.com/category/subcategory/#myqueryhash

I put this code

我把这个代码

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

It returns only www.mysite.com/category/subcategory/

它只返回 www.mysite.com/category/subcategory/

Output should be :

输出应该是:

www.mysite.com/category/subcategory/#myqueryhash

回答by Kausha Mehta

You can use this for HTTP request

您可以将其用于 HTTP 请求

<?php $current_url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; ?>

You can use this for HTTPS request

您可以将其用于 HTTPS 请求

<?php $current_url="https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; ?>

You can use this for HTTP/HTTPS request

您可以将其用于 HTTP/HTTPS 请求

<?php $current_url="//".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; ?>