Issue with checking server status - php

I have a simple script to check if a server is online or offline, however this script doesn't seem to work on the servers I want.
This is the script:
$value = mysql_result($result,$i,"servername");
$value2 = mysql_result($result,$i,"serveraddress");
$value3 = mysql_result($result,$i,"portnumber");
$value4 = mysql_result($result,$i,"description");
$id = mysql_result($result,$i,"id");
ob_start();
if (!$socket = #fsockopen($value2, $value3, $errno, $errstr, 1))
{
echo " <font color='red'><CENTRE><strong>OFFLINE</strong></CENTRE></font>";
}
else
{
echo " <font color='green'><CENTRE><strong>ONLINE</strong></CENTRE></font>";
fclose($socket);
}
$status = ob_get_contents();
ob_end_clean();
?>
The problem is if I say use a Google address and port 80 it will show Google's online, if I use an external IP address and a port like 7000 or so it won't show if the server is online, despite it being online. I'm not sure why this is, I'm thinking its possibly because no data is being sent through the port at the time showing it's closed.
Can someone shed some light on this please and maybe rectify the situation.

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

Check server status and output in HTML

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');
}
?>

Bann one or more IP address with PHP withouthtaccess using .htaccess

I'm looking for good code in PHP for Banning some spammers IP's My server is giving me error 500 if I'm using .htaccess
This will do the work
$getip = $_SERVER["REMOTE_ADDR"];
$banned_ip = array();
$banned_ip[] = '194.9.94.*';
$banned_ip[] = '77.105.2.*';
foreach($banned_ip as $banned)
{
$blacked=str_replace('*', '', $banned);
$len=strlen($blacked);
if ($getip==$blacked || substr($getip, 0, $len)==$blacked)
{
$_banned_ip=true;
}
}
if($_banned_ip==true){
echo 'THIS IP IS BANNED!';
exit;
}
The simplest way would be to have a database that keeps a list of the banned ip addresses, if you want to do it on the PHP end rather than directly in the server.
for($i = 0;$i < count($listOfIps);$i++) {
if($listOfIps[$i] == filteredIP($_SERVER['REMOTED_ADDR'])) { //filteredIP is not a native function, it's just a representation of however you want to filter the ip addresses which are sent to you
$banned = true;
}
}
if($banned):
//redirect user or kill script
else:
//render page
endif;
However, there may be better solutions based on the page or application specifics, but this is the best solution I can think of based on your question

PHP does not resolve .local dns names with gethostbyname

I have werid problem with name resolution. I am trying to connect to active directory server. I can successfully get the ldap server address from the SRV recodrs. Then I try to resolve the dns names to IP addresses and it fails:
<?php
echo 'example.com.:' . PHP_EOL;
echo gethostbyname('example.com.');
echo PHP_EOL;
echo 'dc1.veracomp.local.:' . PHP_EOL;
echo gethostbyname('dc1.my-company.local.');
echo PHP_EOL;
echo 'nslookup dc1.my-company.local.:' . PHP_EOL;
echo `nslookup dc1.my-company.local.`;
The example.com is resolved correctly, then the gethostbyname('dc1.my-company.local.') fails after a few seconds returning dc1.my-company.local. instead of the IP address. Still the same PHP script can call nslookup which correctly resolves the domain name...:
example.com.:
93.184.216.119
dc1.my-company.local.:
dc1.my-company.local.
nslookup dc1.my-company.local.:
Server: xxx.xxx.254.117
Address: xxx.xxx.254.117#53
Name: dc1.my-company.local
Address: 192.168.12.21
What is wrong here?
EDIT:
I am asking for name resolution beacuse the real problem i have is that I can connect to ldap://192.168.12.21 or to ldap://dc1.my-company.pl, but I cannot connect to ldap://dc1.my-company.local.
Unfortunatelly the SRV records for _ldap._tcp.my-company.pl returns only local addresses. I do not want to hardcode the .pl address. And I do not understand why I have to manually resolve the local addresses before passing them to Zend_Ldap as a host option.
You should avoid its use in production. DNS Resolution may take from 0.5 to 4 seconds, and during this time your script is NOT being executed.
I use this one; this will be faster and more efficient:
<?php
function getAddrByHost($hosts, $timeout = 3) {
$returnString = '';
foreach ($hosts as $host) {
$query = `nslookup -timeout=$timeout -retry=1 $host`;
if (preg_match('/\nAddress: (.*)\n/', $query, $matches))
$returnString .= trim($matches[1]) . '<br>';
$returnString .= $host . '<br>';
}
return $returnString;
}
$hostArray[] = 'www.example.com';
$hostArray[] = 'dc1.my-company.local';
$returnString = getAddrByHost($hostArray);
echo $returnString;
?>

trying to make simple WakeOnLan script with PHP

Trying to make a little script that will turn on server. I found few examples on the net, but wanted to keep this basic/simple also to get better hold of how it all fits together. But this doesn't work, I realize I've to specify subnet 255 255 255 0 somewhere...
any ideas?
<?php
//check if server is up and running
$alive = fsockopen("XXX.168.1.1", 80, $errno, $errstr, 2);
if (!$alive) {
echo "<h1>Server is Down!</h1>";
echo "I will try to turn it on now...";
//Creating magic packet
$mac_address = str_repeat("XXX5XXXX5XXX", 16);
$msg = "FFFFFFFFFFFF " . "$mac_address" . "000000000000";
$host_addr = "XXX.168.1.1";
$host_port = "X";
//Connect send and close connection
$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
$socket_data = socket_send($socket, $msg, strlen($msg), 0, $host_addr, $host_port);
socket_close($socket);
//testing
//echo
} else {
echo "<h1>Server is Up!</h1>";
fclose($alive);
}
?>
Check this: http://www.php.net/manual/en/function.socket-sendto.php#57746
And this: http://www.php.net/manual/en/function.socket-send.php#58574
Maybe even this: http://www.codeproject.com/Articles/11469/Wake-On-LAN-WOL
You'll find out that you need to set the ip address argument to '255.255.255.255' to make a broadcast :)

Categories