I want the users of my website to check if any other website (http and/or https) is up. There are sites out there that use google analytics for that (if I understood it right). But I don't understand how they do it.
Question 1) How do I use google-analytics on my website to check if some other site is up?
Question 2) How do I do it by myself? Using php or javascript? I wonder if google-analytics might be more reliable in terms if they use multiple server locations to check whether the site is online compared to a single location that I would use with my own code.
You can use server side Curl and monitor http response header, site timeouts.
One can try to connect directly to the http(s) port of the server.
$canConnect = FALSE;
$host = 'www.example.com';
$service_port = 80; // http, for https use 443;
$address = gethostbyname ($host);
$socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket !== FALSE) {
$result = socket_connect ($socket, $address, $service_port);
if ($result) {
$canConnect = TRUE;
}
socket_close($socket);
}
You could ping the servers and monitor the responses. This link shows you the implementation in PHP: http://www.darian-brown.com/php-ping-script-to-check-remote-server-or-website/
Related
I have a weighing scale connected to a client pc with a serial port. The client PC is installed with SERPROXY. Using telnet xx.xx.xx.xx port 25003, the data is shown successfully. It means SERPROXY is working fine and port forwarding is done properly in router too. I will share the real public ip upon request to see the data using telnet.
How do I about to read and echo it on the web page ?
The following piece of code fails on binding using sockets?
error_reporting(E_ALL);
// Server IP address
$address = "xx.xx.xx.xx"; // here goes the real public ip address
// Port to listen
$port = 25003;
$mysock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_bind($mysock,$address, $port) or die('Could not bind to address');
socket_listen($mysock, 5);
$client = socket_accept($mysock);
// read 1024 bytes from client
$input = socket_read($client, 1024);
echo $input ;
socket_close($client);
socket_close($mysock);
Its my first time attempt in socket. Does it have to be compulsorily two way communication, ie, for example, server.php and client.php ? Otherwise the sockets don't work.
Is there any other way we can read the output of the said ip address & port in php web page ?
Spent several hours but to no luck. Help would be greatly appreciated.
NOTE: server is hosted on bluehost. socket_extensions are enabled, verified.
EDIT: Also confirmed with bluehost that the port 25003 is open from their side.
EDIT: Tried following way too, but to no avail.
$fp = fsockopen("tcp://xx.xx.xx.xx", 25003, $errno, $errstr);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
while (!feof($fp)) {
$weight = trim(fgets($fp, 64)," ");
}
}
echo $weight;
fclose($fp);
Where I could be going wrong ? Am I totally on the wrong track ?
I'm in the process of creating my own service status script as both a chance to become more familiar with the PHP language and to design it from the ground up as being as efficient as possible for my needs.
A section of my code used in both my cron job and testing a connection parts queries the IP/Port of a service to make sure it is online. My issue is that the script simply queries whether the port is "Unblocked" on that IP so if for instance I was querying port 21 with an FTP server and that FTP server crashed my script would not detect any changes meaning its not doing what I want it to do. Instead I would be wanting the IP and port to be queried and for my script to see if there is actually something running on that port, if there is show online if not error out. I've had a look on google and it seems like I would have to send a packet/receive a response so PHP can tell there's something active? I'm not sure.
This is my current code below:
<?php
$host = $_POST['servip'];
$port = $_POST['servport'];
if (!$socket = #fsockopen($host, $port, $errno, $errstr, 3)) {
echo "Offline!";
} else {
echo "Online!";
fclose($socket);
}
?>
http://php.net/manual/en/function.fsockopen.php
fsockopen — Open Internet or Unix domain socket connection The socket
will by default be opened in blocking mode. You can switch it to
non-blocking mode by using stream_set_blocking(). The function
stream_socket_client() is similar but provides a richer set of
options, including non-blocking connection and the ability to provide
a stream context.
Since fsockopen will either connect or not connect (timeout) then that tells you whether or not a connection is available ("open") or being blocked (firewall, etc).
// Ping by website domain name, IP address or Hostname
function example_pingDomain($domain){
$starttime = microtime(true);
$file = #fsockopen($domain, 80, $errno, $errstr, 10);
$stoptime = microtime(true);
$status = 0;
if (!$file) {
$status = -1; // Site is down
} else {
fclose($file);
$status = ($stoptime - $starttime) * 1000;
$status = floor($status);
}
return $status;
}
If you really want to know if the FTP server is working or not, your best option is to actually send FTP commands through to it.
An FTP server, upon connect, should typically reply with the first three bytes "220" or "120". 220 is a "greeting". You can read more in RFC 959.
To be completely sure, you might be better off using ftp:// handling in PHP, e.g. actually authenticating a user (maybe user authentication is broken, but it's still able to send a greeting - does that count is "down"?)
Anyway, if you want better than "was I able to connect on that port?" or "did the connect succeed in a timely fashion?", you have to delve into actual communication over the socket. Ultimately, this means you have to do something special for each type of service (for some, read bytes, for others write bytes, etc.)
I want to ping a single ip address fast and easy with a little ping script, I dont know if this is possible I have looked at other techniques but those required me to have root access to my server which I dont. So I was wondering if anyone knew how to do this I have seen a couple of people using Nmap but I cant get the hold of that.
If anyone has any ideas please post.
You may want to use the socket functions to create a ICMP ping packet with a pre-calculated checksum. Here's the code I use to ping from PHP:
function pingHost($host, $timeout=1) {
$payload = "\x08\x00\x7d\x4b\x00\x00\x00\x00PingHost";
$socket = socket_create(AF_INET, SOCK_RAW, 1);
socket_set_options(
$socket,
SOL_SOCKET,
SO_RCVTIMEO,
array(
'sec' => $timeout,
'usec' => 0,
)
);
socket_connect($socket, $host, 0);
socket_send($socket, $payload, strlen($payload), 0);
if (socket_read($socket, 255) === false) {
$result = true;
} else {
$result = false;
}
socket_close($socket);
return $result;
}
echo pingHost('myhostname');
Ah, I missed that you need root for this. Then you cannot ICMP ping the host. You can however do other types of pinging that may be just as good. The key is to find out what services the host is running. Is it a web server running HTTP or HTTPS?
Then you can use cURL.
// http://css-tricks.com/snippets/php/check-if-website-is-available/
function isDomainAvailible($domain)
{
//check, if a valid url is provided
if(!filter_var($domain, FILTER_VALIDATE_URL))
{
return false;
}
//initialize curl
$curlInit = curl_init($domain);
curl_setopt($curlInit,CURLOPT_CONNECTTIMEOUT,10);
curl_setopt($curlInit,CURLOPT_HEADER,true);
curl_setopt($curlInit,CURLOPT_NOBODY,true);
curl_setopt($curlInit,CURLOPT_RETURNTRANSFER,true);
//get answer
$response = curl_exec($curlInit);
curl_close($curlInit);
if ($response) return true;
return false;
}
If curl is not available you should be able to use get_file_contents() with a known valid URL.
Is it a db server, say mysql or mssql? Do something similar but use PDO to open a connection to it and run a command.
If the service is operational, then the host is online. Of course, this checks if the host networking stack is online AND if the service is running. But since you can't ping without root, this might be your best bet.
Lastly, you can always try and see if a exec() or system() call to 'ping' will work, or create a shell script that has root privileges then execute it using PHP.
Those are all the ways I know work.
I am trying to make a simple TCP/IP connection to a given IP and Port using sockets in PHP.
I am getting an error message that says "A non-blocking socket operation could not be completed immediately." below is my code:
<?php
$address = 'myhost.com';
$service_port = 1234;
$timeout = 1000;
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_nonblock($socket);
$error = NULL;
$attempts = 0;
while (!($connected = #socket_connect($socket, $address, $service_port)) && ($attempts < $timeout)) {
$error = socket_last_error();
if ($error != SOCKET_EINPROGRESS && $error != SOCKET_EALREADY) {
echo socket_strerror($error) . "\n";
socket_close($socket);
return NULL;
}
usleep(1000);
$attempts++;
}
?>
any ideas? i know there are no issues with the target host ip or port.
You are calling connect() on a non-blocking socket. The connect() call will always need to block to complete its operation, and since you explicitly told the socket to be non-blocking it immediately returns with an error telling you that it did not (yet) complete its operation.
You are a bit at the mercy of the OS you are using when using sockets directly. The code looks good overall. You will retry the connect until you get EINPROGRESS. Is that indeed the error code you are seeing? Maybe you are checking for the wrong error code?
Anyway your code looks to me like you try to connect until the connection is established. You can use blocking mode directly to achieve that.
Just leave the socket as blocking until you connected it and set the socket to non-blocking (assuming you really need that) after it is connected.
non-blocking socket usually used for servers, servers are waiting for connection, so instead of using socket_connect, try using socket_listen instead
if you want to establish a connection to a server (be a client) then use Johannes's suggestion
I know that PHP does allow you to create a server but what about client? I would need a script that connects to my TCP/IP server on given port and send some data. Is that possible in PHP and if so, could you help me please? I did not find anything useful.
I have my TCP/IP server running on port 1301 and I would need users to be able by clicking on web page send one char to the server.
It's similar to how you would create a server. I'd recommend taking a look at the documentation for socket_connect.
Summaries:
socket_create
socket_bind
socket_connect
socket_write
socket_read
socket_close
Workflow:
Create the socket
Optionally bind it
Connect to the server
Read/write data
Close the socket
I've used this piece before. It's fairly simple; it connects to $ip_address on port $port, and sends the $sendData data to the server, and then reads the response and returns the response.
$sendData = chr(6).chr(0).chr(255).chr(255).'info';
function sendAndGetResponse($ip_address, $port, $sendData){
$socketHandler=#fsockopen($ip_address, $port, $errno, $errstr, 1);
if(!$socketHandler)
{
return false; //offline
}
else
{
$response = '';
stream_set_timeout($socketHandler, 2);
fwrite($socketHandler, $sendData);
while (!feof($socketHandler))
{
stream_set_timeout($socketHandler, 2);
$response .= fgets($socketHandler, 1024);
}
fclose($socketHandler);
return $response;
}
}
You can use CURL if it is HTTP server or create a socket connection http://php.net/manual/en/function.socket-connect.php
Yes, php can act as a HTTP-client with CURL, fsockopen and most easiest way to fetch URL - with file_get_contents()