如何使用 PHP 获取脚本的当前 URL?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1871770/
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 04:07:46  来源:igfitidea点击:

How do I get the current URL of my script using PHP?

phpurl

提问by Steven

How do I get the current URL of my script using PHP?

如何使用 PHP 获取脚本的当前 URL?

回答by Andrew Hare

Please see PHP Get Current URL:

请参阅PHP 获取当前 URL

Sometimes it is not as straightforward as one may think to get the current url to use it inside your application. Here is a snippet that I use to fetch the current URL and use it in a script. The current url (whether http or https) is now a local variable that you can do with as you please.

有时,获取当前 url 以在应用程序中使用它并不像人们想象的那么简单。这是我用来获取当前 URL 并在脚本中使用它的片段。当前 url(无论是 http 还是 https)现在是一个本地变量,您可以随意使用。

$url = "http".(!empty($_SERVER['HTTPS'])?"s":"").
"://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];

Note: You should groom this value before using in anything sensitive, like a sql query.

注意:您应该在使用任何敏感内容(例如 sql 查询)之前修饰此值。

回答by adrianTNT

For www.example.com/files/script.php

对于 www.example.com/files/script.php

$_SERVER['SCRIPT_NAME']= /files/script.php

$_SERVER['SCRIPT_NAME']= /files/script.php

and if you want the current directory url:

如果您想要当前目录 url:

dirname($_SERVER['SCRIPT_NAME'])= /files

dirname($_SERVER['SCRIPT_NAME'])= /文件

回答by Hitesh Chavda

If you want to know the script name on your server, then..

如果您想知道服务器上的脚本名称,那么..

$file = $_SERVER["SCRIPT_NAME"];
echo $file;

$_SERVER["SCRIPT_NAME"] will give you the current executed script path, which related on your server not on the browser's url.

$_SERVER["SCRIPT_NAME"] 将为您提供当前执行的脚本路径,该路径与您的服务器相关,而不与浏览器的 url 相关。

回答by Brimstedt

Whe you need info like this its often convenient to look at the output of phpinfo() and then read the docs on those items that seems good.

当你需要这样的信息时,查看 phpinfo() 的输出通常很方便,然后阅读那些看起来不错的项目的文档。

Keep in mind that some of them might be correct on your server, but slightly different on others.. Depending on os, http server etc.

请记住,其中一些在您的服务器上可能是正确的,但在其他服务器上略有不同。取决于操作系统、http 服务器等。

回答by ravindrakhokharia

suppose you were accessing

How do I get the current URL of my script using PHP?

then echo $_SERVER['SERVER_NAME']; // This will return u the hostname that is:-

stackoverflow.com/

and echo $_SERVER['REQUEST_URI'];// It will return only the file name from current url will return below

"/questions/1871770/how-do-i-get-the-current-url-of-my-script-using-php"

and not the hostname.

假设您正在访问

如何使用 PHP 获取我的脚本的当前 URL?

然后 回显 $_SERVER['SERVER_NAME']; // 这将返回主机名,即:-

stackoverflow.com/

回显 $_SERVER['REQUEST_URI']; // 它将只返回当前 url 中的文件名将在

“/questions/1871770/how-do-i-get-the-current-url-of-my-script-using-php”下方返回,

而不是主机名。

回答by Vaibhav Jain

Hi this is the solution of your problem

您好,这是您问题的解决方案

//fetch page url by this

//通过这个获取页面url

$url=$_SERVER['REQUEST_URI'];
echo "$url<br />";

//it will print

//它会打印

//fetch host by this

//通过这个获取主机

$host=$_SERVER['HTTP_HOST'];
echo "$host<br />";

//you can fetch full url by this

//你可以通过这个获取完整的url

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