2010-11-02 17 views
0

我最近部署了Symfony 1.3.6網站。我選擇在服務器上保留frontend_dev.php,以便在絕對需要時在本地機器上進行調試。如何防止調試工具欄出現在產品環境中

我修改frontend_dev.php這樣的:

<?php 

require_once(dirname(__FILE__).'/../config/ProjectConfiguration.class.php'); 

$configuration = ProjectConfiguration::getApplicationConfiguration('frontend', 'dev', true); 

// this check prevents access to debug front controllers that are deployed by accident to production servers. 
// feel free to remove this, extend it or make something more sophisticated. 
if (!in_array(@$_SERVER['REMOTE_ADDR'], array('127.0.0.1', '::1'))) 
{ 
    //in case something screwy happens ... 
    try 
    { 
     // die('You are not allowed to access this file. Check '.basename(__FILE__).' for more information.'); 
     sfContext::createInstance($configuration)->getController()->forward('security', 'error404'); 
     exit(); 
    } 
    catch(Exception $e) 
    { 
     //if we got here, all bets are off anyway, just go away .... 
     exit(); 
    } 
} 

sfContext::createInstance($configuration)->dispatch(); 

我所做的一切是爲了將請求定向到一個404錯誤頁面。然而,我注意到當我輸入http://www.mywebsite.com/frontend_dev.php/some_valid_url.html

我被引導到404頁面(正如我想的那樣) - 但顯示調試工具欄 - 顯然這是一個安全風險。從非本地計算機訪問dev控制器時禁用工具欄的最佳方法是什麼?

我想在error404動作中放入檢查代碼,然後在需要時禁用調試工具欄,但我不確定這是否是最符合symfonic的做法。

在這種情況下最好的做法是什麼?

+0

就我個人而言,我剛剛在代碼中將我的IP地址添加到數組中,以允許臨時訪問frontend_dev控制器,然後將其刪除。 – Tom 2010-11-03 12:33:26

回答

2

您正在開啓調試的開發環境中初始化配置。嘗試是這樣的:

// this check prevents access to debug front controllers that are deployed by accident to production servers. 
// feel free to remove this, extend it or make something more sophisticated. 
if (!in_array(@$_SERVER['REMOTE_ADDR'], array('127.0.0.1', '::1'))) 
{ 
    //in case something screwy happens ... 
    try 
    { 
     // die('You are not allowed to access this file. Check '.basename(__FILE__).' for more information.'); 
     $configuration = ProjectConfiguration::getApplicationConfiguration('frontend', 'prod', false); 

     sfContext::createInstance($configuration)->getController()->forward('security', 'error404'); 
     exit(); 
    } 
    catch(Exception $e) 
    { 
     //if we got here, all bets are off anyway, just go away .... 
     exit(); 
    } 
} 
else 
{ 
    $configuration = ProjectConfiguration::getApplicationConfiguration('frontend', 'dev', true); 
    sfContext::createInstance($configuration)->dispatch(); 
} 
8

sfConfig::set('sf_web_debug', false);

+1

這隻會關閉調試工具欄。您仍然可以在可能具有不同配置的開發環境中運行您的應用程序。 – 2010-11-03 04:36:31

+0

Coronatus:第一個(error404)頁面的調試工具欄會打開,但該頁面上的鏈接(例如回到主頁)仍然顯示調試工具欄 – skyeagle 2010-11-04 11:42:46

+0

因此,將該行放在全局文件中。 – 2010-11-04 11:53:05

5

難道你只是想關閉它在settings.yml文件?

dev: 
    .settings: 
    web_debug: false