2013-01-03 5 views
0
$pages = array('Text1.php', 'Text2.php', 'Text3.php'); 

// $currentpage depends the page you're on 

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

내가 include(Text4.php)를 통해 액세스하고 include(Text4.php) 배열에서 위의 모든 세 PHP 스크립트에 저장되어있는 PHP 스크립트가 있습니다. 이제는 사용자가 현재있는 페이지와 같아야하고 $pages의 값과 일치하도록 £currentpage을 설정합니다.이 값은 현재 페이지의 파일 이름이어야합니다.설정 PHP 변수는 현재 페이지를 찾을 수

제 질문은 위 코드에서 $ currentId 변수를 작성하여 작성한 기능을 수행해야합니다.

+0

마지막 문장 :'$ currentpage' 변수, 그렇습니까? – greg0ire

+0

http://stackoverflow.com/questions/7997016/if-this-page-show-this-else-show-this – andrewk

답변

1

파일 이름하려는 경우 :

$currentPage = basename(__FILE__); 

__

디렉토리 이름 + 파일 이름 :

$currentPage = __DIR__; 
+0

'Text4.php' 페이지는 배열의 다른 PHP 페이지와 같은 폴더에있다. 그래서 파일 이름 만 사용하거나 디렉토리와 파일 이름을 사용하는 것이 좋습니다. – Manixman

+0

시행 착오, dewd! –

+0

마지막 질문은 변수를 정의한 것처럼 간단하게 설정되는 변수입니까? 임씨는 사용자의 현재 페이지를 결정할 수있는 간단한 코드 행을 깜짝 놀라게했습니다. 나는 그것이 더 복잡 할 것이다라고 생각했다 – Manixman

0

// 당신은 경로를 원하는 경우

$currentpage = $_SERVER['PHP_SELF']; 

// 당신은 단지 실제 파일 이름 (문자 그대로 "Test.php") 만

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

PHP_SELF를 사용하지 마십시오. 악용 될 수 있습니다. –

+0

gotcha, 그다지 좋지 않다. – jcsbrotha

0

이 내 브라우저 게임에 사용하는 코드의 일부는 다음과 같습니다

$currentPage = __FILE__; 

에만 디렉토리

__. 표현은 약간 변경되었지만 코드 자체는 훌륭하게 작동합니다. 모든 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.";}