URL Validation in PHP -


the topic has been discussed lot here @ stackoverflow answers managed explore fail produce results need. want check before inserting url database value url. default function of php filter_validate_url returns true if provide httpp://exampl

but need validate value if true domain example.net, example.com etc.. let's try example:

case 1:

$url = "http://example"; if(!filter_var($url, filter_validate_url) === false) {                 return true;             } 

this above returns true domain isn't valid.

case 2:

$url = "http://google.com"; if(!filter_var($url, filter_validate_url) === false) {                 return true;             } 

returns true , that's okay.

but possible solution case 1? please help.

p.s.: used curl , works response slow (more 5 seconds). solid solution appreciated.

i've coded quick script may achieving need :

<?php //error_reporting(e_all); //ini_set('display_errors', 1); $url = "http://www.google.com";   if(validateurl($url)){     echo "valid"; }else{     echo "invalid"; }  function validateurl($url){  //first validate url using regex  if (!preg_match('%^(?:(?:https?)://)(?:\s+(?::\s*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\x{00a1}-\x{ffff}0-9]-*)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.(?:[a-z\x{00a1}-\x{ffff}0-9]-*)*[a-z\x{00a1}-\x{ffff}0-9]+)*(?:\.(?:[a-z\x{00a1}-\x{ffff}]{2,}))\.?)(?::\d{2,5})?(?:[/?#]\s*)?$%uis', $url)) {      return false; }   //if url valid, "curl it" , expect 200 header response in order validate it.  $ch = curl_init($url); curl_setopt($ch, curlopt_header, true);    // want headers curl_setopt($ch, curlopt_nobody, true);    // don't need body (faster) curl_setopt($ch, curlopt_returntransfer,1); curl_setopt($ch, curlopt_followlocation,1); // follow redirections curl_setopt($ch, curlopt_timeout,10); $output = curl_exec($ch); $httpcode = curl_getinfo($ch, curlinfo_http_code); curl_close($ch);   if($httpcode == "200"){     return true; }else{     return false; }   } 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -