Check server status and output in HTML - php

I'm trying to output the status of a server (online/offline) in html.
I have a PHP snippet that checks the status of the server but I need the output to be in HTML format so I can use it elsewhere on my website.
I'm running wordpress with the tablera plugin, I want to output the status (just online or offline text) in the table but it does not accept PHP, only HTML.
Edit:
This is my PHP snippet:
<?php
$array="www.php.net:80";
$fp = #fsockopen($array[0], $array[1], $errno, $errstr,1);
if(!$fp){
$status = $array[0]."==><font color=\"#FF0000\">online</font>";
}
else{
$status = $array[0]."==><font color=\"#FF0000\">offline</font>";
}
fclose($fp);
echo $status;
?>
It just needs to check if the server is online or offline, if it's online it should display 'online' if its offline, it should display 'offline'.

This script checks your server to see if the server is online or not.
<?php
if (fsockopen('www.yourwebsite.com', 80)) {
echo('Reports Online');
} else {
echo('Reports Offline');
}
?>

Related

PHP - How would i scan a specific UDP Port to see if my game server is online or not

i recently got back into php! I have been trying to get this to work but i just can't seem to get it :(
So, i am trying to check to see if a UDP port (the port i am using for my game server is running or not) it says "Online" but it stays online even when i turn the server off :( can someone explain to me what is going and correct me? thanks!
<?php
$host = '47.39.46.24';
$port = 14242;
$waitTimeoutInSeconds = 7;
if($fp = fsockopen("udp://".$host,$port,$errCode,$errStr))
{
$write = fwrite($fp,"x00");
if (!$write)
{
echo 'offline';
}
else
{
echo 'online';
}
}
else
{
echo 'offline';
}
fclose($fp);
?>
And before you say i shouldn't be giving my ip don't worry it isn't real :D

How to receive data from ESP8266 Wifi Module using php

I tried with sending a GET request using ESP8266 wifi module using Arduino.The module successfully responded with :
SEND OK +IPD
On server, I want to receive the data and write it in a text file. So i tried the following codes
>parse_str( html_entity_decode( $_SERVER['QUERY_STRING']) , $out); $data= $out['data'];
$fileStatus=file_put_contents('myFile.txt',$data,FILE_APPEND);
if($fileStatus!=false){
echo "SUCCESS";
} else{ echo "FAIL"; }
But the data failed to store.
Assuming your GET request from the ESP to the server was made to the URL http://example.com/myPhpScript.php?parameter1=xxxx&parameter2=yyyy
you should be able to get the the value of parameter1 and paramter2 like this:
<?php
$p1 = $_GET['parameter1'];
$p2 = $_GET['parameter2'];
$data = $p1.','.$p2;
file_put_contents('myFile.txt',$data,FILE_APPEND);
?>

See if a server is listening on a port in PHP?

Before answering, please note that I am completely new to PHP. I have heard it is powerful.
What I'm trying to do is have a page on my (Apache 2) web server that when a user clicks a button on the page, the server will check if a port is running on it's own IP with a preset port for each button, I want multiple buttons with the same IP but different ports to be pinged.
Example:
Button 1 (Terraria Server) is clicked, server pings 127.0.0.1:7777 and tells the user if it gets a response or not.
Button 2 (Minecraft Server) is clicked, server pings 127.0.0.1:25565 and tells the user etc etc.
I already have PHP installed and working, all I need is some code :)
Attempt a connection on the port and return the result:
<?php
function Connect($port) {
$serverConn = #stream_socket_client("tcp://127.0.0.1:{$port}", $errno, $errstr);
if ($errstr != '') {
return false;
}
fclose($serverConn);
return true;
}
if(isset($_POST['portTest'])){
switch ($_POST['portTest']){
case 'minecraft': $port= '25565';
break;
case 'Terraria': $port= '7777';
break;
default: exit;
}
if (Connect($port)){
echo "Server is running!";
}else{
echo "Server is down";
}
}
?>
<form method="POST">
<input type="submit" name="portTest" value="minecraft">
<input type="submit" name="portTest" value="Terraria">
</form>

Check if domain is available fails

I have been using this metode.
I have set it up on my site, but for some reason, it doesn't seems to work for all extensions.
As I set I did setup that code on my site, but modified it to check the requested domain name.
You can try it on my site here.
So here you can see some working examples:
try: just.com, just.net, example.com and test.com.
Some not working examples:
try: just.dk, example.dk and test.dk
Here is the complete code I have on the site:
<?php
function checkDomainAvailability($domain_name){
$server = 'whois.crsnic.net';
// Open a socket connection to the whois server
$connection = fsockopen($server, 43);
if (!$connection) return false;
// Send the requested doman name
fputs($connection, $domain_name."\r\n");
// Read and store the server response
$response_text = ' :';
while(!feof($connection)) {
$response_text .= fgets($connection,128);
}
// Close the connection
fclose($connection);
// Check the response stream whether the domain is available
if (strpos($response_text, 'No match for')) return true;
else return false;
}
$domainname = 'accurst.com';
if (isset($_GET['domain']))
$domainname = $_GET['domain'];
if(checkDomainAvailability($domainname)) echo 'Domain : '.$domainname.' is Available';
else echo 'Domain : '.$domainname.' is Already Taken';
?>
Does anyone have any idea how to fix this issue?
Probably because the whois server doesn't support those top level domains. Take a look at whomsy

Check if form file exists on page load using PHP

So I have a simple form that takes a user input, passes it to a separate PHP script that does some processing on a different domain, and posts a txt file if successful. Example:
<form method="GET" action="inventory_check.php" target="_blank">
Part Number <input type="text" name="part" /><input type="submit" value="Check Inventory" />
</form>
<?php
$filename = $userInput;
if (file_exists('ftpMain/'.$filename.'')) {
$handle = fopen("ftpMain/".$filename."", "r");
$output = fread($handle, filesize('ftpMain/'.$filename.''));
fclose($handle);
$output = trim($output, '&l0O(10U');
$output = trim($output, 'E ');
echo $output;
}
else {
echo 'Failure.';
}
?>
So, inventory_check.php obviously is an inventory lookup for us, however, it's contained on another server (different domain) so it completes its processing and posts it to a file, that I read, cleanup, and display. Now my issue is twofold, I need to grab and keep the input from the user to find the filename and the second is I need to page to either reload or recheck if the file exists. What is the best approach to do this?
Note: We use an awful in house DBMS, so posting and retrieving from a DB is not an option, it took us a while to get it to read the input and FTP it correctly, so it looks like this is the only path.
Why don't you make the request in your server A? by using curl, so you could get the response right after the query.
Firstly, you'll need to get the user's input properly, and sanitize it. I'll leave out the details of the sanitize() method, as that's not really what you're asking.
<?php
if(isset($_POST)) {
$part_number = sanitize($_POST['part']);
$filename = "ftpMain/$part_number";
if (file_exists($filename)) {
$handle = fopen($filename, "r");
$output = fread($handle, filesize($filename));
fclose($handle);
/* Do things with output */
} else {
echo 'Failure.';
}
}
?>
However, you say that the file is on another server - looking for ftpMain/... is only going to look for a directory called ftpMain in your current directory. Is the file publicly available on the internet? If it is, you could do something like this:
<?php
$url = "http://yourserver.com/parts/$part_number.txt";
$response = get_headers($url, 1);
if ($response[0] == 'HTTP/1.1 200 OK') {
/* The file exists */
} else {
/* The file does not exist */
}
?>
I hope I've understood your question correctly - this assumes that the form action is pointing to itself. That is, your file with this code is also called inventory_check.php.

Categories