Question

I am trying to create a registration system that allows users to submit their website URLs. Now when a user enters a URL, it checks against the database to see if it already exists, and rejects it if it does.

However, my problem is because of this :

If http://www.example.com is in the database and I enter http://example.com, this counts as a different URL as far as the check is concerned, and it allows the submission.

Is there a proper way to handle this, apart from retrieving all records, removing the www if present, and then comparing? (Which is a terribly inefficient way to do so!)

Note : Adding Laravel tag in case it has any helper functions for this (I am using a laravel-4 installation).

EDIT : This is my current logic for the check :

$exists_url = DB::table("user_urls")
        ->where('site_url', 'like', $siteurl)
        ->get();
        if($exists_url)
        {
            return Redirect::to('submiturl')->withErrors('Site already exists!');
    }

EDIT 2 : One way is to take the given URL http://www.example.com, and then search the database for http://www.example.com, www.example.com, http://example.com and example.com. However I'm trying to find a more efficient way to do this!

Was it helpful?

Solution

I think before you implement a solution you should abstractly flesh out your policy more thoroughly. There are many parts of a URL which may or may not be equivalent. Do you want to treat protocols as equivalent? https://foo.com vs http://foo.com. Some subdomains might be aliases, some might not. http://www.foo.com vs http://foo.com, or http://site1.foo.com vs http://foo.com. What about the path of the the URL? http://foo.com vs http://foo.com/index.php. I wouldn't waste your time writing a comparison function until you've completely thought through your policy. Good luck!

UPDATE:

Something like this perhaps:

$ignore_subdomains = array('www','web','site');
$domain_parts = explode('.',$siteurl); 
$subdomain = strtolower(array_shift($domain_parts));
$siteurl = (in_array($subdomain,$ignore_subdomains)) ? implode('.',$domain_parts) : $siteurl;
//now run your DB comparison query

OTHER TIPS

You can check before sending data to database using PHP. Following is a small example. Obviously you can make it more advanced as per your liking.

<?php
   $test = "http://www.google.com";
   $testa = "http://google.com";
   if (str_replace("www.","",str_replace("http://","",$testa)) == str_replace("www.","",str_replace("http://","",$test))) {
     echo "same";
   }
   else {
     echo "different";
   }
?>

Following is MySQL Replace example. In this example 'url' is database field.

SELECT * FROM `urls` WHERE REPLACE(url, "http://www","") = "google.com" OR REPLACE(url,"http://","") = "google.com"

Once again this is very basic just for your better understanding.

In case you need any further assistance kindly do let me know

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top