# [web] [PHP] Testing if a server is online

This topic is 3867 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi! Currently, I am using the following to test to see if my game server is online. It works, but I lingering in the back of my head, I see a security problem here. If I can connect with my webpage, someone else can, and with a loop, I see a DOS problem arising or someone could just refresh the crap out of my page. Is this a good way to test the server?
<?php
echo "testing if server is online...";
$fp = fsockopen("server here", port here,$errno, $errstr, 3); if (!$fp)
echo "Server is offline!";
else echo "Server is online!";
?>


Possible solutions I was thinking of... And for clarification... webpage = webpage, server = game server 1. I make the webpage only check say, once every 10 min and store that in the MySQL DB? This would stop the refreshing problem. But someone elses page (software) could easily do this. 2. I update my server code to allow my webpage to log in to check the status? The idea being, maybe that if the webpage logged in, I can ignore other sites that repeatedly try to connect. Of course, I don't know how to make my server program ignore an ip if it (the server) hasn't connected to the socket yet... probably not possible. 3. I worry too much. Idea being my server's accept is in its own thread, it wouldn't bog down the server. 4. Your idea here! Thanks for any help!

##### Share on other sites
IIRC DoS attacks are generally handled at the router/firewall level.

-me

##### Share on other sites
Caching is always a good idea, just store the latest results in your db along with a timestamp and only check it again if it is more than X seconds (or minutes) old.

You might also want to use a separate port for status / info checks. (That way its easy to tell your router or firewall to restrict access to that port) (This port can then be used to actually talk to the server and obtain more information, number such as number of connected clients, etc)

##### Share on other sites
So, idea #3 was correct for a different reason.
Okay, but is my code the general consensus of how to check the game server? I know it works, but have come to the realization a while ago, just because it works, doesn't mean it is the correct/best way to do it.

##### Share on other sites
"You might also want to use a separate port for status / info checks. (That way its easy to tell your router or firewall to restrict access to that port) (This port can then be used to actually talk to the server and obtain more information, number such as number of connected clients, etc)"

Thanks, never really thought of that!

##### Share on other sites
I would setup a cron job, cron jobs work in the background, so you could setup your page so that your server only has access to it, and every so often the script executes which in turn updates your MySQL database about the status of your server. ofcourse you have to support cron jobs but if you do I believe this is one of the best ways to do it albeit a more secure method, so you won't get users refreshing your page to try and perform a Denial Of Service attack.

##### Share on other sites
I will have a look at cron jobs when I get home from work tomorrow. But you gave me an idea (unless this actually is what a cron job is). I can just use my server to access the database at regular intervals, indicating it is online and place a timestamp. Then on my page, it reads the online status and time stamp and displays appropriatly. If the time stamp is > than the time interval of the server update, the page displays offline. But this doesn't allow for game info (stats and whatnot) in real time, but hey, I guess 5 or so minutes old of data wont be to far off.

Thanks again.

##### Share on other sites
A cron job is a scheduled task in the form of a simple PHP script.

The question you should ask yourself is, is it absolutely necessary for the status info to be real-time, or is it perfectly acceptable it only polls every five minutes? If someone visits your page and stays there for two minutes, the information is no longer real-time anyway (unless you make regular requests from the client-side).

Logging the server status every so often gives you the extra benefit of having a history of the server's uptime along with other information. You could for instance log the number of players every so often, and you'll end up with nice graph. :)

On the DoS attacks: if I really wanted to shut your game server down, believe me there are still other (and 'better') ways to that. As mentioned earlier, (D)DoS attacks should be (but will not always) stopped at the earliest possible moment, which is the (hardware/software) firewall. Fixing your game server status polling is not going to prevent that anyway.

##### Share on other sites
Ok, well, google shows that cron jobs are Unix based. Now..I have to admit I only looked at the first page of results (I will look into it more late this morning) I must say I am using my personal computer for web server and game server which is Windows XP PRO. This is just temporary as I learn some PHP and get my game server working better. No sense paying for a host when I dont really know what I am doing (web programming that is).

Now after that speech, is there a quick Windows based Cron site you can recommend?

##### Share on other sites
WanMaster: Well, just saw your post after I did my last. I can understand, DoS attacks with no matter what I do (look at Tibia my favorite MMO, been around for 10 years and just recently went under an attack)/Edit/ no matter what I do /Edit/. I just don't want Joe Wannabe "the hacker" causing trouble with something so simple to do.
But onto your other comments/suggestions, I have decided to not make Stats "live" but up to the quarter/half hour. The server on/offline will be handled every 5 mins or so.

/Edit/ Crap, didn't even finish the sentence, NEED sleep. Just spent 10 hours at the Ole Amazon.Com, I need the sleep.

##### Share on other sites
Quote:
 Original post by ImgellingNow after that speech, is there a quick Windows based Cron site you can recommend?

Quote:
 I need the sleep.

##### Share on other sites
If the servers are in a LAN, heartbeat would be how i'd do this:

1. server with webpage has a daemon listening on udp multicast for heartbeats. (you can write the daemon in php or c or whatever).
2. game server has a 'heartbeat daemon' which periodically (every 5 mins or whatever, you can determine interval after a few experiments) sends (via udp multicast) a 'heartbeat packet'
3. whenever the daemon on the web server receives the heartbeat, updates a timestamp in a database (or just a file on disk, whatever).
4. the webpage just checks the timestamp and if it's older than heartbeat interval, it means the game server is down (for whatever reason).

no DOS problem if someone refreshes the crap out of your page.

also, if they are not in a lan, you could put a 'heartbeat.php' script on the webserver and have a 'send_heartbeat.php' running on the game server, basically doing same thing over HTTP (via fopen(http://blabla/heartbeat.php).
(and on the webserver you can also check REMOTE_ADDR to see if it's a valid heartbeat, or whatever).

##### Share on other sites

This topic is 3867 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.