how to check if url already exists in database in PHP?
<p>I am having one scenarion where I am checking if user submitted URL is already present in database or not. My concern is user can submit the same url in different format.
 e.g. URL <a href="http://mysite.com/rahul/palake/?&amp;test=1" rel="nofollow">http://mysite.com/rahul/palake/?&amp;test=1</a>  &amp; URL <a href="http://www.mysite.com/rahul/palake/?&amp;test=1" rel="nofollow">http://www.mysite.com/rahul/palake/?&amp;test=1</a> should be considered one and the same. If I have already stored the url as <a href="http://mysite.com/rahul/palake/?&amp;test=1" rel="nofollow">http://mysite.com/rahul/palake/?&amp;test=1</a> in my database then searching for url <a href="http://www.mysite.com/rahul/palake/?&amp;test=1" rel="nofollow">http://www.mysite.com/rahul/palake/?&amp;test=1</a> in database should give me message as url already existing. For this I am using following code, the following code works for me, I want to make sure it covers all possible scenarios? or this code can be improvised?</p>

<pre><code>$url="http://dev.mysite.com/rahul/palake/?&amp;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/?&amp;test=1 

    }

    //so that $url &amp;&amp; $url1 should be considered as one and the same
    //i.e. mysite.com/rahul/palake/?&amp;test=1  is equivalent to  www.mysite.com/rahul/palake/?&amp;test=1
    //should also be equivalent to http://mysite.com/rahul/palake/?&amp;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
</code></pre>
