How to get the current PHP page name

Possible duplicate:
Get current script name

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

 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 this ...

+76
php
Oct 23
source share
3 answers

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

 echo basename($_SERVER['PHP_SELF']); /* Returns The Current PHP File Name */ 
+226
Oct 23
source share
— -

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

So,

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

must do it.

+20
Oct 23
source share

In your case, you can use the __FILE__ variable!
This should help.
This is one of the predefined.
More information about predefined constants in PHP http://php.net/manual/en/language.constants.predefined.php

+8
Oct 23
source share



All Articles