StefanRSA Posted March 3, 2010 Share Posted March 3, 2010 My function works as follow to check if a url entered by a user is a valid website url or not: <?php //a function to check whether the url exists or not and validate it function check_url($url) { $check = @fopen($url,"r"); // we are opening url with fopen if($check) $status = true; else $status = false; return $status; } //$url = $_GET["url"]; // you can get the parameter from GET $url = "http://www.google.com"; if(check_url($url)) { echo "<div><a href=$url>$url</a> is a <b>valid</b> URL</div>"; } else { echo "<div><a href=$url>$url</a> is a <b>invalid</b> URL</div>"; } ?> It works instantly if a valid url is given but takes AGES if the url is invalid... How can I shorten the time it takes to validate? Link to comment https://forums.phpfreaks.com/topic/194022-url-validation-works-but-takes-to-long-if-invalid/ Share on other sites More sharing options...
StefanRSA Posted March 3, 2010 Author Share Posted March 3, 2010 Sorry, have tested it again... Now its fast... Might be connection problem... Thanks Any other suggestions regarding this script will be nice... Link to comment https://forums.phpfreaks.com/topic/194022-url-validation-works-but-takes-to-long-if-invalid/#findComment-1020994 Share on other sites More sharing options...
StefanRSA Posted March 3, 2010 Author Share Posted March 3, 2010 For those of you that wonder how to do this with Ajax.... Here is the solution! The page that the user will enter his/her domain: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>test url by Stefan</title> <meta http-equiv="content-type" content="text/html; charset=windows-1250"> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <script language="javascript" type="text/javascript"> function getXMLHTTP() { //fuction to return the xml http object var xmlhttp=false; try{ xmlhttp=new XMLHttpRequest(); } catch(e) { try{ xmlhttp= new ActiveXObject("Microsoft.XMLHTTP"); } catch(e){ try{ xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch(e1){ xmlhttp=false; } } } return xmlhttp; } function valUrl(strURL) { var req = getXMLHTTP(); if (req) { req.onreadystatechange = function() { if (req.readyState == 4) { // only if "OK" if (req.status == 200) { document.getElementById('urlval').innerHTML=req.responseText; } else { alert("There was a problem while using XMLHTTP:\n" + req.statusText); } } } req.open("GET", strURL, true); req.send(null); } } </script> </head> <body onLoad="if (self != top) top.location = self.location"> http://<input type="text" maxlength="50" size="20" id="url" name="url" onBlur="valUrl('<?echo $root;?>/urlvalidate.php?url='+this.value)"> <div id="urlval">Only type the link in as follow: www.domain.co</div> And then the "urlvalidate.php" page that does the work: <?php //a function to check whether the url exists or not and validate it function check_url($url) { $check = @fopen($url,"r"); // we are opening url with fopen if($check) $status = true; else $status = false; return $status; } $url = 'http://'.$_GET["url"]; // you can get the parameter from GET //$url = "http://www.wsdwd.dda"; if(check_url($url)) { echo "<div><a href=$url>$url</a> is a <b>valid</b> URL</div>"; } else { echo "<div><a href=$url>$url</a> is a <b>invalid</b> URL</div>"; } ?> Link to comment https://forums.phpfreaks.com/topic/194022-url-validation-works-but-takes-to-long-if-invalid/#findComment-1021009 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.