0

I want to see if a website can be accessed or not with a PHP page.

Here is my plan:

<?php 
  $website = /* bool to see if site is up */
  if($website)
{
  echo'<iframe src="http://64.126.89.241/" width="100%" height="100%"/>';  
}else
{
  echo'<iframe src="http://tsiserver.us/backup/" width="100%" height="100%"/>';  
}
?>

The website will be hosted on another server, therefore if my internet goes down, a user may access the backup version of a site.

GTAWWEKID
  • 51
  • 1
  • 7

3 Answers3

2

Here is a simple function that will determine if a website exists using PHP and cURL

function urlExists($url=NULL)  
{  
    if($url == NULL) return false;  
    $ch = curl_init($url);  
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);  
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
    $data = curl_exec($ch);  
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);  
    curl_close($ch);  
    if($httpcode>=200 && $httpcode<300){  
        return true;  
    } else {  
        return false;  
    }  
}  

This was grabbed from this post on how to check if a URL exists. Because Twitter should provide an error message above 300 when it is in maintenance, or a 404, this should work perfectly.

reference : https://stackoverflow.com/a/1239090/568414

Community
  • 1
  • 1
Ofir Hadad
  • 1,800
  • 3
  • 26
  • 47
0

Based on your approach, you would need 3 different servers:

  1. The server that hosts the script
  2. The server that hosts the website
  3. The server that hosts the fallback website

This is not so efficient and many hosting services provide fallback or cached versions of your site in case it's down, automatically. You don't have to mess with a script for this, but if you insist, you can refer to PHP's curl manual.

arielnmz
  • 8,354
  • 9
  • 38
  • 66
  • It is just a hassle to run a email server on Ubuntu, I wanted the main domain on a webhost's server for email, then to point website to my own server to use my mySQL database. – GTAWWEKID Jun 17 '14 at 17:47
  • In that case you may only want to open your mysql port on the firewall and make it accessible from the webhost, but why wouldn't you just export your database to the webhost? – arielnmz Jun 17 '14 at 17:55
  • Security reasons. The database should not be accessible from anything but localhost. – GTAWWEKID Jun 17 '14 at 17:57
  • If you export your database, it'd still be only accesible for localhost, only that localhost is now another host, but still, your db wouldn't be open to the internet. – arielnmz Jun 17 '14 at 18:00
0

The answer is here, thanks guys!

<?php 
    function ping($host, $port, $timeout) { 
      $tB = microtime(true); 
      $fP = fSockOpen($host, $port, $errno, $errstr, $timeout); 
      if (!$fP) { return "down"; } 
      $tA = microtime(true); 
      return round((($tA - $tB) * 1000), 0)." ms"; 
    }


    $website = ping("64.126.89.241", 80, 10);
    if($website != "down"){
      echo'<iframe src="http://64.126.89.241/" width="100%" height="100%"/>';  
    }else{
      echo'<iframe src="http://tsiserver.us/backup/" width="100%" height="100%"/>';  
    }
?>
GTAWWEKID
  • 51
  • 1
  • 7