One link, Multiple IP addresses

Javascript coding ..

Moderators: macek, egami, gesf

User avatar
popeye
New php-forum User
New php-forum User
Posts: 120
Joined: Sat Jan 03, 2004 7:06 pm
Location: New York, NY

One link, Multiple IP addresses

Postby popeye » Fri Feb 03, 2006 8:08 am

I do not know which category this question belongs to but here it goes.
Is it possible to have a single link that tests the availibilty of a website and in case of failure to connect it tries to connect to a different website? :?

User avatar
Coditor
New php-forum User
New php-forum User
Posts: 243
Joined: Wed Feb 01, 2006 9:18 am
Location: Netherlands
Contact:

Postby Coditor » Fri Feb 03, 2006 3:40 pm

Well, it doesn't belong in Javascript... :)

You could do it via PHP, try to open a socket to that IP address (port 80, HTTP). If it fails, try another one. If succesfull, redirect to that address.

Code: Select all

<?php
$serverList = array(
   "192.168.0.1",
   "10.0.0.6",
   "127.0.0.1",
);
foreach ($serverList as $ipAddress) {
   if (@fsockopen($ipAddress, 80, $errnr, $errmsg, 5)) {
      header("Location: " . $ipAddress);
      exit();
   }
}

echo "We are currently unable to forward you to the correct server. Please try again later";
?>


However, if you're talking about hosting a website on multiple servers as fallback, you should do it via a DNS RR (Round-Robin). Add multiple IP address to the www A record and every self-respecting browser will try each address until a connection can be established.

Coditor

User avatar
popeye
New php-forum User
New php-forum User
Posts: 120
Joined: Sat Jan 03, 2004 7:06 pm
Location: New York, NY

Postby popeye » Tue Feb 07, 2006 6:10 am

This looks great. I think the alternative using the DNS sound more dynamic. I will give it a try. Thanks.


Return to “JavaScript”

Who is online

Users browsing this forum: No registered users and 0 guests