我有一個情況,我正在檢查用戶提交的URL是否已經存在於數據庫中。我關心的是用戶可以以不同的格式提交相同的網址。 例如URL http://mysite.com/rahul/palake/?&test=1 & URL http://www.mysite.com/rahul/palake/?&test=1應該被認爲是一個一樣的。如果我已經在我的數據庫中存儲了url爲http://mysite.com/rahul/palake/?&test=1,那麼在數據庫中搜索url http://www.mysite.com/rahul/palake/?&test=1應該會給我留言,因爲url已經存在。爲此,我使用以下代碼,下面的代碼適用於我,我想確保它涵蓋所有可能的場景?或者這個代碼可以即興創作?如何檢查PHP數據庫中是否已經存在url?
$url="http://dev.mysite.com/rahul/palake/?&test=1";
$parse_url=parse_url($url);
//first check if www is present in url or not
if(!strstr($parse_url['host'],'www'))
{
$scheme=trim($parse_url['scheme']);
//assign default scheme as http if scheme is not defined
if($scheme =='')
$scheme='http';
//create new url with 'www' embeded in it
$url1=str_replace($scheme."://",$scheme."://www.",$url);
//now $url1 should be like this http://www.mysite.com/rahul/palake/?&test=1
}
//so that $url && $url1 should be considered as one and the same
//i.e. mysite.com/rahul/palake/?&test=1 is equivalent to www.mysite.com/rahul/palake/?&test=1
//should also be equivalent to http://mysite.com/rahul/palake/?&test=1
//code to check url already exists in database goes here
//here I will be checking if table.url like $url or table.url like $url1
//if record found then return msg as url already exists
一般來說,不能保證「www.somesite.com」和「somesite。com「是相當的,順便說一下... – Dmitri 2012-03-02 14:52:32