how to validate url and how to check that url already in database

Discussion in 'PHP' started by dineshsingh1984, Jun 22, 2011.

  1. #1
    I want to validate url like this :
    only this type accepted url ...........
    http://www.example.com (.com, .in, .co.in, etc)
    http://example.com (.com, .in, .co.in, etc)


    and not accepted url......................
    www.example.com
    http://www.example
    http://www.example.
    http://www.example.jdsfjsdlfsd (only accepted valid extension)
    http://www.example.com.ldflsdjfsjdfjjlas (only accepted valid extension)


    if url present in data base then how to check that url already present in database
    if url present without www and second times enter with www then how to check or compare with database url.


    plz help me............
    I wasted a long time but i'm sucess so plz help me...............

    thanks
     
    dineshsingh1984, Jun 22, 2011 IP
  2. niks00789

    niks00789 Well-Known Member

    Messages:
    188
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    110
    #2
    
    function find_tld($url)
    {
    return end(explode(".", $url));
    }
    
    PHP:
    this will return you the tld, i.e. com or in or org or net...
    use an if statement to select only those which you want to be accepted...
    it wont work for .co.in coz it will give "in" even if domain has co.in at the end but the domain will be accepted...

    now to find if the domain already exists in the database just execute a query to search for the domain just entered, if the query is not empty, it means the domain exists...

    and to solve the http://www.domain.com and http://domain.com problem...
    (there could be better ways, but all i could think at first go is this)

    take the domain in a variable say $domain
    now replace http:// using str_replace("http://","",$domain) in a new variable say $domain_wo_www
    now append www to that and store in new variable, say $domain_w_www = "www.".$domain_wo_www;

    now execute a query so to search for an entry with domain = $domain_w_www OR domain = $domain_wo_www;

    if the result of the query is not empty then it means, an entry exists for this domain in the database, either as http://www.domain.com or http://domain.com

    Hope it helps :)
     
    niks00789, Jun 22, 2011 IP
  3. The Webby

    The Webby Peon

    Messages:
    1,852
    Likes Received:
    30
    Best Answers:
    1
    Trophy Points:
    0
    #3
    You can use preg_match to check if a url is valid, like this:
    
    $url = "www.something.com";
    if (!preg_match("#^http://www\.[a-z0-9-_.]+\.[a-z]{2,4}$#i",$url)) {
    echo "URL is not valid";
    exit();
    }
    
    PHP:
    Now as for checking the tld you can use this code to isolate the tld:
    
    $urlComponent = parse_url($url);
    $host = explode(".", $urlComponent['host']);
    $hostSize = sizeof($host);
    if($hostSize > 3)
    	$tld = $host[$hostSize-2].".".$host[$hostSize-1];
    else if(($host < = 3) && ($host[0] != "www"))
    	$tld = $host[$hostSize-2].".".$host[$hostSize-1];
    else
    	$tld = $host[$hostSize-1];
    echo $tld;
    
    PHP:
    It works for tlds like .com, .co.in, .cc etc.

    Please Note, that it is not 100% foolproof code, it will return wrong data if a url is something like: http://subdomain.subdomain.domain.tld

    And to check whether it is a valid tld or not, you have to maintain a database of all valid tlds and check against it (hard one)
     
    The Webby, Jun 22, 2011 IP
  4. dineshsingh1984

    dineshsingh1984 Active Member

    Messages:
    154
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #4
    thanks for this script,
    I think it's working, I will try............

    thanks for script...............

     
    Last edited: Jun 22, 2011
    dineshsingh1984, Jun 22, 2011 IP