Advertisement
Advertisement


How to get current PHP page name


Question

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:

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

2020/02/02
1
112
2/2/2020 1:34:49 PM

Accepted Answer

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

echo basename($_SERVER['PHP_SELF']); /* Returns The Current PHP File Name */
2012/10/23
308
10/23/2012 2:37:25 PM

$_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.

So

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

should do it.

2017/08/12

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

2012/10/23