Question

I am trying to make a script where I can chek the server status (Online/Offline) for servers like: Teamspeek, Minecraft and others.

I found this script:

<?php
    function getStatus($ip,$port){
           $socket = @fsockopen($ip, $port, $errorNo, $errorStr, 3);
           if(!$socket) return "offline";
             else return "online";
        }
    echo getStatus("server.com", "80");
?>

But instead of having to change the sever name and port I wnat to get it from a mysql database. So I've made this, but my problem is: I can't get the string connected to the script. My Code to get the string looks like this:

<?php 
// Check to see the URL variable is set and that it exists in the database
if (isset($_GET['id'])) {
// Connect to the MySQL database  
include "connect_mysql.php"; 
$id = preg_replace('#[^0-9]#i', '', $_GET['id']); 
// Use this var to check to see if this ID exists, if yes then get the product 
// details, if no then exit this script and give message why
$sql = mysql_query("SELECT * FROM servers WHERE id='$id' LIMIT 1");
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) {
    // get all the product details
    while($row = mysql_fetch_array($sql)){ 
        $id = $row["id"];
        $ip = $row["ip"];
        $port = $row["port"];
        $getit = $row["getit"];
        $name = $row["name"];
        $content = $row["content"];
     }

} else {
    echo "That item does not exist.";
    exit();
}       
}
?>

So I thought I just could change the echo getStatus("server.com", "80"); To echo getStatus("$ip", "$port"); But that does not work. Is there anyone that know what I have to do?

Was it helpful?

Solution

First things first. Use mysqli_ instead of mysql.

Secondly, if you are limiting the result to 1, then you dont need while loop.

Now, when you get id in get method, you can check it in database and get details:

You will have to fire the function getStatus() once you get the details from database.

if ($productCount == 1){
   echo getStatus($row["ip"],$row["port"]);
} else {
         echo "That item does not exist.";
}   
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top