[help] standard code for game server status (php)

Join Discord

Far

tsniffer
Staff member
Developer
May 19, 2003
20,184
30
2,787
540
I know theres scripts on here for checking if a server is online or offline, but is it possible to create a generic script which works for any online game?

i want to create a list of private servers (similar to xtremetop100.com) but have the online/offline server status listed.

is it possible to send a request or anything (aslong as their ip and port is known) and check if they are online?

im only presuming this is php (since its web based) and doesnt require anything like js.
 

DiabloVodkaShot

LOMCN VIP
VIP
Feb 20, 2009
2,272
182
265
Yes this is possible, and how this works i believe is that. What the php script does with mir, is Gets the ip and ports they have enterd, for eg 7000, and checks if enything is running on port 7000 and if it is it produces it as Online!
 

Far

tsniffer
Staff member
Developer
May 19, 2003
20,184
30
2,787
540
do you have the code (obviously in its simplest form) which shows if the server is online/offline i could use?

im really no good with being able to fathom things like this.
 

Kaori

LOMCN MiR3 Queen!
VIP
Jun 3, 2004
3,584
38
285
Canada
This is what I have...
checks upto 3 times with a 5 second timeout.
it also waits 1 second to see if the connection holds.

PHP:
function checkLoginGate($host, $port) {
	for ($i = 0; $i < 3; $i++) {
		$fp = @fsockopen($host, $port, $errno, $errstr, 5);
		if (!$fp) {
			$ret = "DOWN";
		} else {
			sleep(1);
			if (feof($fp)) {
				$ret = "DOWN";
				echo "checkLoginGate Failed: $host $port Connection Closed\n";
			} else {
				$ret = "UP";
			}
			fclose($fp);
			break;
		}
	}
	return $ret;
}
 

Primal

LOMCN VIP
VIP
Jun 28, 2004
1,756
102
259
This looks very different to mine...

but then there is a few ways to code this...

its different to mine, kaoris is probably better tho :)


btw farphit, from kaoris code you just need to change these parts to the IP and Port for the servers

$host, $port,

or put at the top $host = IP HERE
and
$port = PORT HERE

or set up your script around them for however u need them, mine gets the ip from a mysql db (for the server listing) which people have posted but i have the ports as standard they arnt saved within the mysql
 

Far

tsniffer
Staff member
Developer
May 19, 2003
20,184
30
2,787
540
im yet to try this, but many thanks for the code. ive never used functions in php before. do they just run through in the place where you post it, or do you have to call it?
 

Primal

LOMCN VIP
VIP
Jun 28, 2004
1,756
102
259
im yet to try this, but many thanks for the code. ive never used functions in php before. do they just run through in the place where you post it, or do you have to call it?

either i think
 

Kaori

LOMCN MiR3 Queen!
VIP
Jun 3, 2004
3,584
38
285
Canada
just call it anywhere outside that function... before or after doesn't matter
I usually stick functions at the bottom and put codes before them.

checkLoginGate("127.0.0.1", 7000);
function etc....
 

Far

tsniffer
Staff member
Developer
May 19, 2003
20,184
30
2,787
540
ah thats great. many thanks. will give it a go shortly.

edit: works like a dream. thanks again.
 
Last edited:

Far

tsniffer
Staff member
Developer
May 19, 2003
20,184
30
2,787
540
Just an update on this, ive currently got 20+ servers shown on a single page (soon to be alot more)

if i put too many on one page it either takes several minutes to load up the page, or gives up (this is being ran locally, so no idea how much slower it would be for other people)

is there anyway to make the code more efficient?
 

Primal

LOMCN VIP
VIP
Jun 28, 2004
1,756
102
259
yea, forgot the name of what it is but you get it with most hosts (kaori probs knows n i use it)
basically it runs the script you have in a php file every so often (whatever time you set it to) then you add to the script to add the info (or update) to a MSSQL database or whichever you have in your website, then you make your website just get the information which is in your MSSQL database which takes seconds then you can also make search querys etc to find things within your db.

this is what i use in my server listing, hope you understand what i mean :)
 

Kaori

LOMCN MiR3 Queen!
VIP
Jun 3, 2004
3,584
38
285
Canada
like adam said...

it's best to run a cron job instead of letting everyone hitting your server thus hitting other servers.

cron job of like 10 minutes apart at least and write your data to file or or db.

gui/siggy/stats page would get info from those files or db and show users. that means nearly instant response.