2013-12-17 48 views
0

我運行一個購物網站代理,接受來自用戶的訂單。一個關鍵的步驟是允許用戶向我發送他們希望訂購的物品的URL(從一個海外網站稱爲淘寶網,這個網站沒有發往澳大利亞),所以我可以訂購它們。Zend的URL驗證失敗

當我得到很多錯誤的網址,我想這可能是添加URL驗證用戶提交訂單之前是個好主意。我使用Zend框架,所以我只是用被Zend提供的URI檢查:

// allow unwise characters in URL, used in user input URL validation 
Zend_Uri::setConfig(array('allow_unwise' => true)); 

// add http:// or https:// if not present 
if (strlen($tblurl[$rowid]) > 7) 
{ 

if ((substr($tblurl[$rowid],0,7) != 'http://') && (substr($tblurl[$rowid],0,7) != 'https:/')) 
{ 
$tblurl[$rowid] = 'http://'.$tblurl[$rowid]; 
} 

} 

// Check if URL is valid 
$isValidURL = Zend_Uri::check($tblurl[$rowid]); 

這裏唯一的預處理是添加http://或https://開頭,如果用戶錯過(如「WWW .chinabuy.com.au「將返回無效但」http://www.chinabuy.com.au「將返回有效)。

我已經抱怨他們得到的URL驗證失敗,錯誤消息中收到幾封電子郵件客戶。我真的沒有看到我做錯了什麼,我一直在用許多不同的奇怪的URL來測試它,但它看起來代碼是按照預期工作的。

任何想法?我的網站是https://www.chinabuy.com.au/order,所以你可以測試它(只是去直下訂購詳情,鍵入一些網址,然後點擊提交。如果有錯誤消息「必須有每個項目一個有效的URL。」那麼就意味着確認有。失敗

感謝

編輯: 正如@ArendE建議我已經更新了我的代碼,並把新的代碼住在我的網站:

$tblurl[$rowid] = trim($tblurl[$rowid]); 
// Check if the url contain the words http:// or https:// 
if (stripos($tblurl[$rowid],'http://') === false && stripos($tblurl[$rowid],'https://') === false) { 
    $tblurl[$rowid] = 'http://' . $tblurl[$rowid]; 
} 
$isValidURL = Zend_Uri::check($tblurl[$rowid]); 

回答

0

有一個錯字用https:/(缺少一個斜槓),我建議你改變你檢查的方式網址;或者在輸入url上使用trim(它可能包含空格,製表符,換行符等)。下面是你可以使用的腳本,如果你沒有其他的URL作爲GET變量。

Zend_Uri::setConfig(array('allow_unwise' => true)); 

// Check if the url contain the words http:// or https:// 
if(strpos($tblurl[$rowid],'http://') !== false || strpos($tblurl[$rowid],'https://') !== false) { 
    $isValidURL = Zend_Uri::check($tblurl[$rowid]); 
} else { 
    $isValidURL = Zend_Uri::check('http://' . $tblurl[$rowid]); 
} 
+0

感謝ArendE。這是一個打算的錯字,因爲我檢查字符串是否至少有7個字符長,然後如果是,我會檢查前7個字符是什麼。因此,對於https://,因爲它是1個字符的長度,我只需檢查https:/ – user3109346

+0

反正你的代碼看起來比我的好。我會使用它,也會預先修剪字符串。任何其他想法我做錯了什麼?感謝堆! :D – user3109346

+0

嗯,是的,我明白了,應該不是那麼重要:-)修剪對於strpos來說是不必要的,strpos將檢查這個單詞是否存在,不管這個位置。最後的建議,如果你仍然遇到問題;您可以調試一段時間,使用php的error_log將無效URL放入,以便稍後查看它們,以查看發生了什麼問題。詳情參見關於PHP的錯誤日誌http://php.net/manual/en/function.error-log.php – ArendE