Website URL validation using PHP

Discussion in 'PHP' started by krinad'suza, Jan 29, 2010.

  1. #1
    I am looking for website url validation using php. Can anyone help me with it?

    Thanks
     
    krinad'suza, Jan 29, 2010 IP
  2. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #2
    Are you looking to have a script that can do this?
     
    HuggyStudios, Jan 29, 2010 IP
  3. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #3
    built-in function parse_url() is a good starting point.
     
    SmallPotatoes, Jan 29, 2010 IP
  4. nice12

    nice12 Member

    Messages:
    37
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #4
    Use fopen - in this case you check if this is a real url.
     
    nice12, Jan 29, 2010 IP
  5. new2seoo

    new2seoo Peon

    Messages:
    143
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Are you looking for some thing like this ?
    
    
    /* - Usages for check_url function.
    	If(check_url("http://www.weberdev.com/get_example-4335.html")) {
    	    Echo"URL Exists";
    	}Else{
    	    Echo"URL doesnot exist";
    	}
    */
    
    function check_url($url) {
        $c = curl_init();
        curl_setopt($c, CURLOPT_URL, $url);
        curl_setopt($c, CURLOPT_HEADER, 1); // get the header
        curl_setopt($c, CURLOPT_NOBODY, 1); // and *only* get the header
        curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); // get the response as a string from curl_exec(), rather than echoing it
        curl_setopt($c, CURLOPT_FRESH_CONNECT, 1); // don't use a cached version of the url
        if (!curl_exec($c)) { return false; }
    
        $httpcode = curl_getinfo($c, CURLINFO_HTTP_CODE);
        return ($httpcode < 400);
    }
    
    PHP:
     
    new2seoo, Jan 29, 2010 IP
  6. leftnode

    leftnode Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    You may be interested in us2.php.net/manual/en/filter.filters.validate.php
     
    leftnode, Jan 30, 2010 IP
  7. BRUm

    BRUm Well-Known Member

    Messages:
    3,086
    Likes Received:
    61
    Best Answers:
    1
    Trophy Points:
    100
    #7
    Hi krinad'suza,

    Do you mean validation of the URL's syntax? As in the placements of the TLD components, such as checking whether a given URL is in valid URL format, or are you looking to validate that the URL itself exists and is accessible?

    If the former, then you need to read up on regular expressions - such as PHP regex functions - if the latter, then you need to read up on PHP's cURL library - as it's quicker and more powerful than the built-in data stream functions and it allows you to configure the HTTP headers meaning your request isn't going to be refused by the target URL.

    I have experience with connections, streams and sockets being used in this way as I had to research considerably for an older project's webcrawler.

    I hope this helps,
    Lee.
     
    Last edited: Jan 30, 2010
    BRUm, Jan 30, 2010 IP