Page 1 of 1

Getting a page name from the server (PHP Newbie)

Posted: Fri Feb 24, 2006 11:15 am
by bordman
Hi,
I have the following code:

Code: Select all

<? $pagename = $_SERVER['PHP_SELF'] ;
	if ($pagename == "index.php") {
		$myimage = "image1.jpg" ;
		} else {
		$myimage = "image2.jpg" ;
		}
 ?>
pagename returns /foldername/index.php

How can I split this up so I only get index.php

THANKS!!!

Posted: Fri Feb 24, 2006 11:43 am
by s.dot
There's probably a predefined server variable

but you could try this

Code: Select all

<?php

function getPageName($phpself){
   $pieces = explode("/",$phpself);
   $num = count($pieces) - 1;
   return $pieces[$num];
}

echo getPageName($_SERVER['PHP_SELF']);

?>
using this on /test/index.php returns

Code: Select all

index.php

Posted: Fri Feb 24, 2006 11:45 am
by feyd
basename() works too. :lol:

Posted: Fri Feb 24, 2006 12:00 pm
by bordman
basename() works perfect!!!!!!!!!!!!!!!!!!!!!!!!!!!!! I was digging into a php reference site, that saves me some time!! THANKS!!!!!!!!!!!!