Reham Fahmy
Reham Fahmy

Reputation: 5063

How to compare between links

I'm trying to do simple ban tool system for urls that entered my website but still have problem of url compare.

I've created the following database table badurl with url I'm going to ban

CREATE TABLE `badurl`(
`id` int(3) NOT NULL auto_increment,
`url` text,
PRIMARY KEY (`id`),
KEY `id` (`id`))

Now let say I've stored the following url www.no_good_site.com and someone posted http://www.no_good_site.com however it is the same but it won't ban it.

$user = "http://www.no_good_site.com" // visitor post this

// i'm gonna do compare by the stored banned urls
// I've stored this one www.no_good_site.com

$qry="select * from badurls where url='$user'";
$result=mysql_query($qry) or die($qry);
if(mysql_num_rows($result)=='0'){

echo "Good";

}else{

while($line=mysql_fetch_array($result)){

echo "Bad";

}}

So is there any idea I can set both (posted and stored url) without www. http:// end slash / then compare it. [no_good_site.com]

Because it failed as they might enter with many possibilities and my way needs both to be exactly the same.

no_good_site.com
no_good_site.com/
www.no_good_site.com/
http://no_good_site.com
http://no_good_site.com/
http://www.no_good_site.com
http://www.no_good_site.com/

Upvotes: 1

Views: 799

Answers (4)

Nishant
Nishant

Reputation: 3694

One easy thing would be, if you will extract the domain name from the URL entered and then search for it in the DB, not the complete URL.

You can easily get the domain name from the $_SERVER variable as ''SERVER_NAME''.

Upvotes: 1

CoursesWeb
CoursesWeb

Reputation: 4237

Try remove http://, www. and end '/' before query:

$user = "http://www.no_good_site.com/";  // visitor post this
$user = rtrim(str_ireplace(array('http://', 'www.'), '', $user), '/');

Upvotes: 2

user1299518
user1299518

Reputation:

perhaps this?

$qry="select * from badurls where url LIKE '%$user%'";

domains can come in forms with protocols as prefixes (http,ssl), along with queries (?blah=boo)

Upvotes: 0

Ganesh Bora
Ganesh Bora

Reputation: 1153

make all URL in similar format before comparing..

no_good_site.com => www.no_good_site.com
no_good_site.com/ = > www.no_good_site.com
www.no_good_site.com/ => www.no_good_site.com
http://no_good_site.com => www.no_good_site.com
http://no_good_site.com/ => www.no_good_site.com
http://www.no_good_site.com => www.no_good_site.com
http://www.no_good_site.com/ => www.no_good_site.com

<?php
    function formatURL($url) {

        //an array of strings I wish to remove
        $remove = array(
                        "http://",
                        "www.",
                        "index.html",
                        "index.php"
                        );

        //loop through array
        foreach($remove as $value) {
            //replace array values with nothing
            $url = str_ireplace($value, '', $url);
        }

        //if the url ends in a / then trim it off
        $url = rtrim($url,'/');

        return $url;

    }
?>

Upvotes: 2

Related Questions