如何获取当前PHP页面名称[duplicate]

时间:2023-02-05 23:39:31

Possible Duplicate:
Get the current script file name

可能重复:获取当前脚本文件名

I've a file called demo.php where I don't have any GET variables in the URL, so if I want to hide a button if am on this page I can't use something like this:

我有一个文件叫做demo。php中,URL中没有变量,所以如果我想隐藏一个按钮如果我在这个页面上,我不能使用这样的东西:

if($_GET['name'] == 'value') {
  //Hide
} else {
  //show
}

So I want something like

所以我想要一些

$filename = //get file name
if($filename == 'file_name.php') {
  //Hide
} else {
  //show
}

I don't want to declare unnecessary GET variables just for doing this...

我不想仅仅为了这个而声明不必要的GET变量…

3 个解决方案

#1


244  

You can use basename() and $_SERVER['PHP_SELF'] to get current page file name

您可以使用basename()和$_SERVER['PHP_SELF']获取当前页面文件名

echo basename($_SERVER['PHP_SELF']); /* Returns The Current PHP File Name */

#2


23  

$_SERVER["PHP_SELF"]; will give you the current filename and its path, but basename(__FILE__) should give you the filename that it is called from.

$ _SERVER(“PHP_SELF”);将为您提供当前文件名及其路径,但是basename(__FILE__)应该为您提供它所调用的文件名。

So

所以

if(basename(__FILE__) == 'file_name.php') {
  //Hide
} else {
  //show
}

should do it.

应该这样做。

#3


9  

In your case you can use __FILE__ variable !
It should help.
It is one of predefined.
Read more about predefined constants in PHP http://php.net/manual/en/language.constants.predefined.php

您可以使用__FILE__变量!它应该帮助。它是预定义的。在PHP http://php.net/manual/en/language.constants.predefinedphp中阅读更多关于预定义常量的信息

#1


244  

You can use basename() and $_SERVER['PHP_SELF'] to get current page file name

您可以使用basename()和$_SERVER['PHP_SELF']获取当前页面文件名

echo basename($_SERVER['PHP_SELF']); /* Returns The Current PHP File Name */

#2


23  

$_SERVER["PHP_SELF"]; will give you the current filename and its path, but basename(__FILE__) should give you the filename that it is called from.

$ _SERVER(“PHP_SELF”);将为您提供当前文件名及其路径,但是basename(__FILE__)应该为您提供它所调用的文件名。

So

所以

if(basename(__FILE__) == 'file_name.php') {
  //Hide
} else {
  //show
}

should do it.

应该这样做。

#3


9  

In your case you can use __FILE__ variable !
It should help.
It is one of predefined.
Read more about predefined constants in PHP http://php.net/manual/en/language.constants.predefined.php

您可以使用__FILE__变量!它应该帮助。它是预定义的。在PHP http://php.net/manual/en/language.constants.predefinedphp中阅读更多关于预定义常量的信息