Reputation: 49
I have a site where people add their sites in my database , i already have done like any user can add only one entry of same site e.g if user1 adds: http://site1.com then no other user can add http://site1.com they are shown this site is already in DB , now some people do this thing they make sub domains, http://subdomain.site1.com , then they can add UL number of subdomains of the main domain which is already in the Database. Now My question is how to prevent this being done? using PHP
My Try:
....
$check = mysql_query("SELECT * FROM domain WHERE url LIKE '%$suburl%'");
if($check=="$url"){
echo"You cannot add a subdomain";
}
?>
Upvotes: 0
Views: 78
Reputation: 16462
Given the new entry is http://subdomain.site1.com
, get the site1.com
part using the function below (I got it here) and put the result inside the LIKE '%%'
// Get the domain name from URL
function get_domain($url)
{
$host = parse_url($url, PHP_URL_HOST);
if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $host, $regs)) {
return $regs['domain'];
}
return false;
}
$host = get_domain("http://subdomain.site1.com"); // site1.com
$qry = mysql_query("SELECT * FROM domain WHERE url LIKE '%$host%' LIMIT 1");
if (mysql_num_rows($qry)) {
// Site already exist
}
Upvotes: 2