PHP - 获取当前 URL

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

PHP - Getting Current URL

php

提问by Howdy_McGee

This seems like a common questions but none of them seem to be what i'm looking for. If there is a website:

这似乎是一个常见的问题,但似乎没有一个是我正在寻找的。如果有网站:

www.example.com/test.php

www.example.com/test.php

I want to pull the "test.php" and if it's something like:

我想拉“test.php”,如果它是这样的:

www.example.com/folder/folder1/test.php

www.example.com/folder/folder1/test.php

I want again to just pull "test.php" but everything seems to pull the exact path instead of just the page. Any suggestions?

我想再次拉“test.php”,但一切似乎都拉出确切的路径,而不仅仅是页面。有什么建议?

回答by Brian Graham

$query = $_SERVER['PHP_SELF'];
$path = pathinfo( $query );
$what_you_want = $path['basename'];

Voila.

瞧。

回答by Tadeck

This will get you the name of the file that was requested (based on the URL invoked, not real location of the file - just in case you are using mod_rewrite):

这将为您提供所请求文件的名称(基于调用的 URL,而不是文件的实际位置 - 以防万一您使用的是 mod_rewrite):

$filename = basename($_SERVER['REQUEST_URI']);

For both http://www.example.com/testing01.phpand http://www.example.com/x/y/z/testing01.phpwill give you:

对于两者http://www.example.com/testing01.phphttp://www.example.com/x/y/z/testing01.php都会给你:

testing01.php

回答by Matt Healy

$file = $_SERVER["SCRIPT_NAME"];
$break = explode('/', $file);
$pfile = $break[count($break) - 1];