Getting a page name from the server (PHP Newbie)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bordman
Forum Newbie
Posts: 16
Joined: Thu Jan 26, 2006 8:20 am
Location: RI

Getting a page name from the server (PHP Newbie)

Post 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!!!
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

basename() works too. :lol:
bordman
Forum Newbie
Posts: 16
Joined: Thu Jan 26, 2006 8:20 am
Location: RI

Post by bordman »

basename() works perfect!!!!!!!!!!!!!!!!!!!!!!!!!!!!! I was digging into a php reference site, that saves me some time!! THANKS!!!!!!!!!!!!
Post Reply