...

View Full Version : Test a domain



rfresh
12-09-2008, 01:55 AM
What's the best way (in PHP) to test if a given domain name is valid? By valid I mean that it connects to a website. Is there a PHP function for this?

ptmuldoon
12-09-2008, 03:24 AM
I think the best you can do is verify if its a valid web address/link with .xxx extension. Or maybe be able to limit to strictly index.xxx pages. You could use preg_match for it.

Fou-Lu
12-09-2008, 04:26 AM
To check the life of a site (and limit you're handling), I'd use curl in conjunction with a head request. Almost no data to handle (so little work to do) and will tell you if the site is actually responding. Good for checking uptime status. This is how you'd test verification.

If you just need to check the validity of a domain, simple pattern matching is all you need.

murrayd77
12-09-2008, 06:02 AM
Hi rfresh,

This can be easily done using PHP regular expression. There are a couple of regular expressions, ereg functions or POSIX & preg functions (perl compatible) to make this script run perfect.

--Dave

rfresh
12-10-2008, 01:24 AM
@Fou-Lu

This is what I'm looking for (and I'm not trying to write an uptime checker) - what would be in the headers?

Fou-Lu
12-10-2008, 02:44 AM
Been awhile.
It should return some server information (apache address for example), the modification date, lessee... connection type, maybe some cookies, and other defined stuff.
I'm trying to remember what curl returns if there is an error that occures, I think its just false, but it may still populate the results as an empty array. That would take some playing around.

rfresh
12-10-2008, 05:32 PM
Here is a simple URL test. Obviously this domain does not exist yet when I run this script it returns True - huh?



$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://sfhsfhshfjsdhf.com");
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 0);
$status = curl_exec($ch);
curl_close($ch);
if ($status)
{
print "true";
}
else
{
print "false";
}

kokjj87
12-11-2008, 10:16 AM
I think the one below would be faster, as you are just getting the header only.

you can add some regular expression for checking before connecting to the site.
The test will only pass if it is a 200, 301, 302(live, ok, and no error).

If there is a 404Error(File not found) or something, the function will return it as a invalid site.


function isValidUrl($url)
{
//connect to the website and get the header information
$header = @get_headers($url);
//if header status is 200, 301 or 302, the website is live and valid
return preg_match('/^HTTP(.*)(200|301|302)(.*)$/i', $header[0], $status);
}

Fou-Lu
12-11-2008, 01:31 PM
I think the one below would be faster, as you are just getting the header only.

you can add some regular expression for checking before connecting to the site.
The test will only pass if it is a 200, 301, 302(live, ok, and no error).

If there is a 404Error(File not found) or something, the function will return it as a invalid site.


function isValidUrl($url)
{
//connect to the website and get the header information
$header = @get_headers($url);
//if header status is 200, 301 or 302, the website is live and valid
return preg_match('/^HTTP(.*)(200|301|302)(.*)$/i', $header[0], $status);
}

get_headers is a better idea. I completely forgot that they added these in PHP5. Save yourself some work and make use of this one (unless you've got PHP4 of course).

rfresh
12-11-2008, 05:50 PM
No matter what URL I use, the code below always returns 0



function isValidUrl($url)
{
//connect to the website and get the header information
$header = @get_headers($url);
//if header status is 200, 301 or 302, the website is live and valid
return preg_match('/^HTTP(.*)(200|301|302)(.*)$/i', $header[0], $status);
}
print isValidUrl("sfsfsdfsdfsdf5.com");

kokjj87
12-11-2008, 07:03 PM
function isValidUrl($url)
{
//if no protocol define, use http as default.
if(!preg_match('/(((http(s?))|(ftp))\:\/\/)/i', $url))
{
$url = 'http://'.$url;
}
//connect to the website and get the header information
$header = @get_headers($url);
//if header status is 200, 301 or 302, the website is live and valid
return preg_match('/^HTTP(.*)(200|301|302)(.*)$/i', $header[0], $status);
}
print isValidUrl("google.com");

That is because you didn't pass in the PROTOCOL..
anyway i have added a function inside which will check whether did you supply th protocol or not, if not it would be treated as a http.

rfresh
12-11-2008, 07:57 PM
Both of these still return the same results: 1

print isValidUrl("google.com");
print isValidUrl("googleslkfjslkjfkslfjks.com");

kokjj87
12-11-2008, 08:33 PM
It is working on my computer..

print isValidUrl("google.com"); //return 1
print isValidUrl("googleslkfjslkjfkslfjks.com"); //return 0

rfresh
12-11-2008, 09:09 PM
Well, your results is what I'm after but I can't get the same results as you. I cleared my IE7 cache and tried it using Firefox...both return 1 for me - I'm doomed... :confused:

kokjj87
12-12-2008, 06:41 AM
can you run this 2 line on your computer and post the result here?..


print_r(get_headers("http://google.com"));
echo "<br/><br/><br/><br/><br/><br/><br/><br/><br/>";
print_r(get_headers("http://googleslkfjslkjfkslfjks.com"));

fl00d
12-12-2008, 11:18 PM
You could use checkdnsrr for something like this. I use the following function to validate email addresses (I check for address and domain validity)



function ValidateEmail($email){
$email = $email;

if(filter_var($email,FILTER_VALIDATE_EMAIL)){
//Check url after @ to make sure it is valid - dns
$email = explode('@',$email);
if(!checkdnsrr($email[1],'ANY')){
return FALSE;
}else{
//Valid format and domain
return TRUE;
}
}else{
//Invalid email format
return FALSE;
}
}

http://ca.php.net/checkdnsrr
If you're using a Windows server you're out of luck; checkdnsrr is unsupported on Windows :/

rfresh
12-12-2008, 11:44 PM
This was the only way I could get this to work for me:



$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://mywebsite.com");
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 0);
$view = curl_exec($ch);
if (eregi("a string from my site home page", $view))
{
echo "ok";
}
else
{
echo "fail";
}
curl_close($ch);



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum