Get the site status - up or down
The hostname does not contain http://
, that is only the scheme for an URI.
Remove it and try this:
<?php
$host = 'google.com';
if($socket =@ fsockopen($host, 80, $errno, $errstr, 30)) {
echo 'online!';
fclose($socket);
} else {
echo 'offline.';
}
?>
What's the best way to check if a website is up or not via JavaScript
Based on Spliffster's comment:
This code will based on the browser timeout try to reach a specific IP until it is available asynchronously. You may want to add code to prevent it from trying to long.
<head>
<script>
function check_available(ip){
var el=document.getElementById("check_pic");
el.src="https://"+ip+"/images/powered_by.gif?now="+Math.random();
}
function check_success(url){
alert("redirect now :) - url:"+url);
}
</script>
</head>
<body>
<img style="visibility:hidden" id='check_pic' src="/images/powered_by.gif" onabort="alert('interrupted')" onload="check_success('http://10.0.0.1/redirect/me/here')" onerror="check_available('10.17.71.150')"/>
</body>
[Edit]
Sidenote:
xmlhttprequest will not work as the browser will throw a cross origin exception. This mozilla.dev link gives more background infos and shows examples using access control header response statements. Be aware that the access control header field is serverside (the site that is being probed) and you might not be able to control that field (not enabled by default).
timing issues
There are differences in timing when using xmlhttprequests for cross origin calls. Since the browser must wait for the response to evaluate possible access control header fields, a call to a non existent website will run into the browsers request timeout. A call to an existing website will not run into this timeout and error out earlier with a cross origin exception (only visible in the browser, javascript never gehts this info!). So there's also the possibility to measure the time from xmlhttprequest.send() to first response (in callback). An early callback call will indicate that the website is up from the browsers point of view but at least with xmlhttprequest you wont be able to evaluate the returncode (since this is blocked bei cross origin policy).
self.xmlHttpReq.open('POST', strURL, true);
self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
self.xmlHttpReq.onreadystatechange = function() {
//stopwatch.stop and calc timediff. timediff < default browser request timeout indicates website is up from this browsers point of view. No clues on request status or anything else, just availability
}
self.xmlHttpReq.send(null);
//stopwatch.start
Is it possible to find the right server by spending less time?
Try this,
What is the easiest way to use the HEAD command of HTTP in PHP?
you will get HTTP Response Code without BODY. It will save time
Ways to programatically check if a website is up and functioning as expected
If your get request on a page that displays info from database comes back with status 200 and matching keywords are found, you can be pretty certain that your site is up and running.
And you don't really need to write your own script to do that. There are free services such as GotSiteMonitor, Pingdom, UptimeRobot etc. allows you to monitor your site.
Related Topics
Curl Error 60: Ssl Certificate Prblm: Unable to Get Local Issuer Certificate
When to Generate a New Application Key in Laravel
What Are the Differences of System(), Exec() and Shell_Exec() in PHP
Environment Driven Database Settings in Laravel
Get All the Images from a Folder in PHP
How to Convert Date/Time from 24-Hour Format to 12-Hour Am/Pm
MySQL Database Won't Start in Xampp Manager-Osx
How to Load Return Array from a PHP File
PHP Preg_Replace/Preg_Match VS PHP Str_Replace
Apple Push Notification with Sending Custom Data
How to Implement MVC from Scratch in PHP
How to Design a Hierarchical Role Based Access Control System
Package PHP5 Have No Installation Candidate (Ubuntu 16.04)
Strategy to Override a Class in a Library Installed with Composer