Make php check if domain is resolved (or ping [domain name])

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Make php check if domain is resolved (or ping [domain name])

Post 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 :-)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post 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()
d_d
Forum Commoner
Posts: 33
Joined: Wed Jul 07, 2004 4:56 pm
Location: UK

Post 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>
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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&trade;</a>.
<pre>
<?php print_r ($hosts); ?>
</pre>
</div>
</body>
</html>
Post Reply