2013-01-07 28 views
0

我開發了一個Joomla!系統插件。 我想在執行該插件時檢測到錯誤的URL。如何使用Joomla檢測錯誤的URL!系統插件?

例如

如果我輸入一個URL 「的http://本地主機/錯網址」,我要趕在系統插件錯誤。

我怎麼知道系統會顯示錯誤頁面(404)?

+0

你的意思是在前端? – Craig

回答

0

您可以使用以下方法

檢查URL功能

function checkURL($URL){ 
    $ch = curl_init($URL); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 5); 
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    $data = curl_exec($ch); 
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
    curl_close($ch); 
    if ($httpcode != 200) { 
     return false; 
    }else{ 
     return true; 
    } 
} 

使用CheckURL功能

/*URL May be a Joomla OR Non Joomla Site*/ 

if(checkURL("http://adidac.github.com/jmm/index.html")){ 
echo "URL Exist"; 
}else{ 
echo "URL NOT Exist"; 
//JError::raiseWarning(500,$URL. " is not exists"); 
} 


if(checkURL("http://adidac.github.com/jmm/indexsdadssdasaasdaas.html")){ 
echo "URL Exist"; 
}else{ 
echo "URL NOT Exist"; 
//JError::raiseWarning(500,$URL. " is not exists"); 
} 

注做到這一點:檢查你有PHP捲曲lib安裝了

+0

我認爲他在問Joomla的時候是否會抓404?應用程序決定不會處理該網址... – Craig

0

在用於捕獲404的系統插件中,您必須從插件添加一個函數作爲JError錯誤處理程序數組的回調。

我會看看com_redirect這樣做的方式,它是系統插件。例如

function __construct(&$subject, $config) 
{ 
    parent::__construct($subject, $config); 

    // Set the error handler for E_ERROR to be the class handleError method. 
    JError::setErrorHandling(E_ERROR, 'callback', array('plgSystemRedirect', 'handleError')); 
} 


static function handleError(&$error) 
{ 
    // Get the application object. 
    $app = JFactory::getApplication(); 

    // Make sure we are not in the administrator and it's a 404. 
    if (!$app->isAdmin() and ($error->getCode() == 404)) 
    { 
     // Do cool stuff here 
    } 
} 

唯一的問題是JError是貶值所以我不知道前進時,這將打破例如3.0,3.1,3.2和3.5應該沒問題,但在那之後誰知道呢?

+0

謝謝你的回覆。 如果同一級別(E_ERROR)有多個錯誤,則最後的處理程序將覆蓋其他錯誤。所以,系統將使用最新執行的插件的處理程序。 如果我的插件是最後一個,plg_redirect將不起作用。 這很糟糕,因爲所有404錯誤都是E_ERROR級別。 –