Get page name in php

Asked 9 years, 10 months ago

Viewed 354k times

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

Samuel Liew

73.6k106 gold badges156 silver badges238 bronze badges

asked Oct 23, 2012 at 14:36

0

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 */

answered Oct 23, 2012 at 14:37

Mr. AlienMr. Alien

149k33 gold badges288 silver badges272 bronze badges

5

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

answered Oct 23, 2012 at 14:42

AmykateAmykate

4855 silver badges8 bronze badges

0

Not the answer you're looking for? Browse other questions tagged php or ask your own question.

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

To get the current page URL, PHP provides a superglobal variable $_SERVER. The $_SERVER is a built-in variable of PHP, which is used to get the current page URL. It is a superglobal variable, means it is always available in all scope.

If we want the full URL of the page, then we'll need to check the protocol [or scheme name], whether it is https or http. See the example below:

Output

Note: The isset[] function is used here to check whether HTTPS is enabled or not. It checks whether a variable exists or not.

Or, we can also get the full URL of current page using another way given in the next example.

Output

To get only name of the current page opened at browser, see the below example:

Output

For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to feedba[email protected]

Help Others, Please Share


Topic: PHP / MySQLPrev|Next

Answer: Use the PHP $_SERVER Superglobal Variable

You can use the $_SERVER built-in variable to get the current page URL in PHP. The $_SERVER is a superglobal variable, which means it is always available in all scopes.

Also if you want full URL of the page you'll need to check the scheme name [or protocol], whether it is http or https. Let's take a look at an example to understand how it works:

Related FAQ

Here are some more FAQ related to this topic:

  • How to extract substring from a string in PHP
  • How to combine two strings in PHP
  • How to prepend a string in PHP

How to get website name in PHP?

$url = "//"; // Append the host[domain name, ip] to the URL. $url. = $_SERVER['HTTP_HOST'];.

Chủ Đề