Page 1 of 1
Make php check if domain is resolved (or ping [domain name])
Posted: Tue Jul 27, 2004 2:12 pm
by Chris Corbyn
Hi,
I'm moving a site to a new address and I want to create a redirect page on the old site.
Just out of pure curiosity to see if it's possible is there any way I can get some PHP to check if the domain is resolved, and if it is redirect to the new domain name, otherwise, redirect to a different address (the IP that works before domain resolves).
I've moved my files too since I switched host at the same time so visitors do need to be redirected.
Is this possible? Check domain resolved -> if true { redirect to domain } else { redirect to IP } kinda thing....
Like I say, just out of curiosity and for future reference.
Thanks

Posted: Tue Jul 27, 2004 2:32 pm
by feyd
Posted: Tue Jul 27, 2004 2:52 pm
by Joe
Code: Select all
$ip = gethostbyname($DomainName);
if ($ip == $DomainName)
{
echo "Available";
exit;
}
else
{
echo "Not Available!";
exit;
}
Not planned greatly but works perfect! If you are planning to ping a site in order to view the results, try
exec()
Posted: Tue Jul 27, 2004 3:37 pm
by d_d
Proberly best done on the client side or not at all. If it works with the dns the server is using doesn't mean that it will work with the dns the client is using.
Perhaps you can use javascript to try to load an image from the new name, if it works send the user to the name and if it doesn't send the user to the ip.
Something like this
Code: Select all
<html>
<head>
<title>redirect</title>
<script type="text/javascript">
<!--
function Load()
{
img = new Image();
img.onload = Loaded;
img.onerror = Failed;
img.src = 'http://www.sitethatmayexist.com/test.png';
}
function Loaded()
{
document.write('<a href="http://www.sitethatmayexist.com/">go to new site</a>');
}
function Failed()
{
document.write('<a href="http://127.16.0.1/">go to new site</a>');
}
//-->
</script>
</head>
<body onLoad="javascript:Load()">
</body>
</html>
Posted: Tue Jul 27, 2004 3:48 pm
by Chris Corbyn
Thanks guys gethostbynamel() works great.
Code: Select all
<?
$hosts = gethostbynamel('healthpointuk.com');
if(!$hosts) {
$site = "http://d11wtq.thefreebizhost.com";
} else {
$site = "http://healthpointuk.com";
}
?>
<html>
<head>
<title>
Redirect to <?php echo $site; ?>
</title>
<META HTTP-EQUIV=Refresh CONTENT="20; URL=<?php echo $site; ?>">
</head>
<body>
<div style="width:550px; margin:15px; padding:10px; border:1px solid red; font-family:verdana; font-size:11pt; color:#014585">
<b>Health Point has moved to <a style="link:#014585" href="http://healthpointuk.com">http://healthpointuk.com</a></b><p>.
<?php
if ($hosts) {
echo 'You will be redirected there in twenty seconds. If you are not redirected please click the link above.<p>';
} elseif (!$hosts) {
echo '<b><font color="red">The domain has not yet resolved to the host server, you will therefore be taken to <a href="http://d11wtq.thefreebizhost.com/">http://d11wtq.thefreebizhost.com/</a> in 20 seconds. This is still the same website just under a special domain name. if you are not redirected in 20 seconds please click the link above.</font></b><p>';
}
?>
This redirection tool was created by <a href="http://tauruscodeworks.com/">Taurus Codeworks™</a>.
<pre>
<?php print_r ($hosts); ?>
</pre>
</div>
</body>
</html>