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

Started by
10 comments, last by cobru 16 years, 11 months ago
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!
my blog contains ramblings and what I am up to programming wise.
Advertisement
IIRC DoS attacks are generally handled at the router/firewall level.

-me
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)
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
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.
my blog contains ramblings and what I am up to programming wise.
"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!
my blog contains ramblings and what I am up to programming wise.
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.
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.
my blog contains ramblings and what I am up to programming wise.
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.
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?
my blog contains ramblings and what I am up to programming wise.
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.
my blog contains ramblings and what I am up to programming wise.

This topic is closed to new replies.

Advertisement