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

Get page name in php

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

Get page name in php

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.

Get page name in php

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

Get page name in php

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

Get page name in php

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

Output

Get page name in php



Get page name in php
For Videos Join Our Youtube Channel: Join Now


Feedback

  • Send your Feedback to feedba[email protected]

Help Others, Please Share

Get page name in php
Get page name in php
Get page name in php






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:


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 = "http://"; // Append the host(domain name, ip) to the URL. $url. = $_SERVER['HTTP_HOST'];.
$curPageName = substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);.

What is $_ GET page?

PHP $_GET is a PHP super global variable which is used to collect form data after submitting an HTML form with method="get". $_GET can also collect data sent in the URL. Assume we have an HTML page that contains a hyperlink with parameters:

How do I get a full URL?

Just right-click in Chrome's address bar select “Always show full URLs” to make Chrome show full URLs. Chrome will now always show the full URL of every web address you open. To disable this feature, right-click in the address bar again and uncheck it.

How can I get the last part of a URL in PHP?

')) $uri = substr($uri, 0, strpos($uri, '? ')); $url = trim($uri, '/');