2016-07-17 1 views
1

파일 트리가 가정 사용

/[PROJECT_ROOT]/include-caller-1.php 
/[PROJECT_ROOT]/subfolder/yet-another-include-caller-2.php 
/[PROJECT_ROOT]/subfolder/subfolder2/yet-another-include-caller-3.php 
/[PROJECT_ROOT]/subfolder/page-get-included.php 
/[PROJECT_ROOT]/subfolder/jump-to-this-page.php 

세 포함 '페이지-GET-'- caller.php 포함 ' included.php '적절한'포함 '문을. /include-caller-1.php

include("/subfolder/page-get-included.php"); 

/subfolder/yet-another-include-caller-2.php

include("page-get-included.php"); 

/하위 폴더 subfolder2 /은 아직 다른-포함/-caller-3.php /subfolder/page-get-included.php, 그것은 리디렉션을 만들 것입니다

include("../page-get-included.php"); 

에 '점프 -에 -이 - page.php'와 같은 뭔가 :

header("Location: " . realpath(dirname(__FILE__) . "/jump-to-this-page.php")); 

그러나 예상대로 작동하지 않습니다.

htdocs과 비교했을 때 [PROJECT_ROOT]의 상대적 위치는 유연합니다. htdocs/project/, htdocs/ 등일 수 있습니다.

즉, $_SERVER['DOCUMENT_ROOT']도 caller.php도 예측할 수 없습니다. 사실 하나만 존재합니다 : 'page-get-included.php'와 'jump-to-this-page.php'는 서로 옆에 있습니다.

이 경우 가능한 모든 발신자 파일에서 'jump-to-this-page.php'로 리디렉션하는 방법은 무엇입니까?

+1

'__FILE__'에는 경로를 통해 공개적으로 액세스 할 수 없기 때문에 헤더 위치에서 보내지 않을 파일 시스템 경로가 포함되어 있습니다. '$ _SERVER [ 'SCRIPT_NAME']' –

답변

2

당신의 파일 시스템 경로 및 공공 HTTP 경로의 차이를 찾아 관계없이 문서 루트 또는 별칭 (이 경우에없는 프로젝트 루트) 파일을 포함하는 HTTP 경로를 설정할 수 있습니다 page-get-included.php

$path_real = __DIR__; 
$path_real_difference = str_replace($path_real, '', $_SERVER['SCRIPT_FILENAME']); 
$path_web = str_replace($path_real_difference, '', $_SERVER['SCRIPT_NAME']); 

이제이 경로가 포함 된 위치와 상관없이이 경로를 jump-to-this-page.php으로 참조 할 수 있습니다.

header("Location: $path_web/jump-to-this-page.php"); 
+0

에 상대 경로를 사용하십시오. 솔루션에 감사드립니다. 나는 당신의 생각에 기초하여 그것을 작동하게 만든다. '$ debug_page_dir = __DIR __;' '$ host = $ _SERVER [ 'HTTP_HOST'];' '$ relative_dir = explode ("htdocs", $ debug_page_dir, 2) [1];' '$ url = "http : // ". $ 호스트. $ relative_dir. $ pageName;' –