Manixman
Manixman

Reputation: 307

set php variable to find current page

$pages = array('Text1.php', 'Text2.php', 'Text3.php');

// $currentpage depends the page you're on

$currentId = array_search($currentpage, $pages);

I have php script which is accessed through an include(Text4.php) and the include(Text4.php) is stored in all the three php scripts above in the array. Now what I want to do is set up the £currentpage so that it should equal the page the user is currently on and match a value in $pages which should be the filename of the current page.

My question is that in the code above, how should $currentId variable be written to perform the functionality it is suppose to acheive?

Upvotes: 0

Views: 522

Answers (3)

FatPurple
FatPurple

Reputation: 1

This is a piece of code I use for my browser game. The wording has been changed slightly but the code itself works excellent. Any php code can be put inside the 'if' statements, where I used 'print.'

    ///---Various Navigation & Functions Depending on Current Page---///

$findpage = strrpos($_SERVER['PHP_SELF'],'/');  
$currentpage = substr($_SERVER['PHP_SELF'],$findpage);
if ($currentpage == '/home.php'){
 print "This message is displayed when the client is on the 'home' page.";
 print "If you so chose, you can put the entire 'home' page code in here.";
 print "While keeping this the only code being executed.";}
if ($currentpage == '/features.php'){
 print "This message is displayed when the client is on the 'features' page.";
 print "If you so chose, you can put the entire 'features' page code in here.";
 print "While keeping this the only code being executed.";}
if ($currentpage == '/menu.php'){
 print "This message is displayed when the client is on the 'menu' page.";
 print "If you so chose, you can put the entire 'menu' page code in here.";
 print "While keeping this the only code being executed.";}
if ($currentpage == '/store.php'){
 print "This message is displayed when the client is on the 'store' page.";
 print "If you so choose, you can put the entire 'store' page code in here.";
 print "While keeping this the only code being executed.";}

Upvotes: 0

David Harris
David Harris

Reputation: 2707

File name only:

$currentPage = basename(__FILE__);

__

Directory name + filename:

$currentPage = __FILE__;

__

Directory only:

$currentPage = __DIR__;

Upvotes: 1

jcsbrotha
jcsbrotha

Reputation: 99

//if you want a path

$currentpage = $_SERVER['PHP_SELF'];

//if you only want the actual file name (literally "Test.php")

$posofslash = strrpos($_SERVER['PHP_SELF'],'/');
$currentpage = substr($_SERVER['PHP_SELF'],$posofslash);

Upvotes: 0

Related Questions