设置php变量来查找当前页面

时间:2013-01-03 19:37:45

标签: php

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

// $currentpage depends the page you're on

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

我有通过include(Text4.php)访问的php脚本,include(Text4.php)存储在数组上面的所有三个php脚本中。  现在我要做的是设置£currentpage,使其等于用户当前所在的页面,并匹配$pages中应该是当前页面文件名的值。

我的问题是,在上面的代码中,应该如何编写$ currentId变量来执行它想要实现的功能?

3 个答案:

答案 0 :(得分:1)

仅限文件名:

$currentPage = basename(__FILE__);

__

目录名+文件名:

$currentPage = __FILE__;

__

仅限目录:

$currentPage = __DIR__;

答案 1 :(得分:0)

//如果你想要一个路径

$currentpage = $_SERVER['PHP_SELF'];

//如果你只想要实际的文件名(字面意思是“Test.php”)

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

答案 2 :(得分:0)

这是我用于浏览器游戏的一段代码。措辞略有改变,但代码本身效果很好。任何PHP代码都可以放在' if'陈述,我用过' 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.";}