2012-06-23 70 views
1

我正在嘗試創建一個PHP腳本,該腳本將始終在每個頁面加載時調用,而不會改變網站的現有代碼,並儘可能少地增加額外的配置。PHP重定向不影響標題

這是我的問題,我不希望它影響有關實際請求(標題)的任何內容。以下是所希望的流動:

請求 - > somescript.php - >實際目的地

所以,我不能使用header()函數,並將該溶液必須完全服務器端。

我在想,在腳本完成它的任務之後,它可能會包含/需要打算使用的PHP文件。

任何想法/建議將非常感激。

問候,

布蘭登·馬丁

PS:我不能用php_value .htaccess文件裏面因爲有時會在CGI環境中使用。

底線:需要一些代碼在每個服務器的請求

+0

所以,你不能改變ini選項,如['auto_prepend_file'](http://php.net/auto_prepend_file)? – Wiseguy

回答

0

你想要的任何其他腳本之前執行的腳本運行?如果你使用Apache我能想到的一個可能的解決方案......如果你能在.htaccess重寫......

.htaccess

RewriteEngine On 
RewriteBase /test 
RewriteRule !(common.php) common.php/$1 

common.php

<?php 
$request=substr($_SERVER['REQUEST_URI'],6); 
if($request==""){ 
    $request="index.php"; 
} 
if(substr($request,0,1)=="/"){ 
    // Prevent browsing local files (*nix) 
    header("HTTP/1.1 403 Forbidden",true,403); 
}else if(!file_exists($request)){ 
    // Not found 
    header("HTTP/1.1 404 Not Found",true,404); 
}else{ 
    // Request URI should be valid 
    do_what_you_want_to_do_before_anything_else(); 
    // Include the original request URI... 
    if(substr($request,-4)==".php"){ 
     include $request; 
    }else{ 
     // Prevent executing non-PHP code 
     echo file_get_contents($request); 
    } 
} 
?> 

仍然有很多代碼編寫,包括安全措施,如果兒童腳本關心,則更改$_SERVER陣列。

0

如果你可以修改服務器的php.ini,也許你可以使用auto_prepend_file指令?

您需要小心:如果直接將它添加到.ini,它對於由該Web服務器託管的所有網站都處於活動狀態。您也可以通過apache或php-fpm中的php_value將其添加到每個站點上,或者您可以在腳本中構建站點檢測。

就配置它的容易程度而言,你不能簡單得多。