Php - How to get page name using basename, Parse url and get the page name

How to get page name using basename


How to get the page name from a url?

To parse the url of the page you present in the sub-directory, Earlier I use to use the parse_url function to get current page. Today I experience a different method to extract the page name using another different function using basename. If the page is located in sub-folder, then this variable will return the page name along with sub-folder name also (e.g love/subfolder/index.php)

If you want to get only the filename (e.g index.php) then you can use the below code.

<?php

$currentpage=$_SERVER['SCRIPT_NAME'];
echo "scriptname : \$_SERVER['SCRIPT_NAME']";
echo "Current page : ".$currentpage.""; // subfolder/index.php
$currentpage=basename($_SERVER['SCRIPT_NAME']);
echo "Current page : ".$currentpage;        // index.php

echo "php self : \$_SERVER['PHP_SELF']";

$currentpage=$_SERVER['PHP_SELF'];	       // subfolder/index.php
echo "Current page : ".$currentpage."";
$currentpage=basename($_SERVER['PHP_SELF']);   // index.php
echo "Current page : ".$currentpage;

?>

The topic on Php - How to get page name using basename is posted by - Math

Hope you have enjoyed, Php - How to get page name using basenameThanks for your time

Tech Bluff