Skip navigation.
Home

Why is my code so slow?

Hall of Fame | Bad Architecture

Don't you hate it when the Internet makes your PHP run slow? I do. Props to Andrew Lindeman for sending this snippet in. I trimmed it to reduce the boring.

This code checks the status of a bunch of Diablo II game servers and generates a pretty table that shows a server's status. Unfortunately the code runs really, really slow. Damn the Internet!

<style type="text/css">
<!--
...
-->
</style>
<p align="center" class="style1">Overall PKA Server Status</p>
<table width="100%" border="0" align="center" cellspacing="0">
 <tr bordercolor="#990000" bgcolor="#990000">
   <td><strong>Diablo II Realms </strong></td>
   <td bgcolor="#FF0000"><span class="style5">Status</span></td>
   <td bgcolor="#660000"><strong>Diablo II Game Servers 1 </strong></td>
   <td bgcolor="#660000"> </td>
   <td bgcolor="#660000"><span class="style6">Diablo II Game Servers 2 </span></td>
   <td bgcolor="#660000"> </td>
 </tr>
 <tr>
   <td width="172">Diablo II  Open Server is :</td>
   <td width="121"><?php
if ($fp = @fsockopen("bnet.pkaclan.com","6112")) { echo "<img src=images/open.gif>"; } else { echo "<img src=images/closed.gif>"; }
?></td>
...
 </tr>
 <tr>
   <td>PKA Realm is :</td>
   <td><?php
if ($fp = @fsockopen("bnet.pkaclan.com","6113")) { echo "<img src=images/open.gif>"; } else { echo "<img src=images/closed.gif>"; }
?></td>
   <td>D2GS PKA1.1(Soulzek):</td>
   <td><?php
if ($fp = @fsockopen("68.56.114.98","4000")) { echo "<img src=images/open.gif>"; } else { echo "<img src=images/closed.gif>"; }
?></td>
   <td> </td>
   <td> </td>
 </tr>
 <tr>
   <td>PvP Realm is :</td>
   <td><?php
if ($fp = @fsockopen("bnet.pkaclan.com","7113")) { echo "<img src=images/open.gif>"; } else { echo "<img src=images/closed.gif>"; }
?></td>
   <td>D2GS PvP1.09(<SPAN class=postbody>Shiznoo</SPAN>):</td>
   <td><?php
if ($fp = @fsockopen("141.158.150.28","4000")) { echo "<img src=images/open.gif>"; } else { echo "<img src=images/closed.gif>"; }
?></td>
   <td> </td>
   <td> </td>
 </tr>
 <tr>
   <td>Ancestrall Recall Realm is :</td>
   <td><?php
if ($fp = @fsockopen("bnet.pkaclan.com","8113")) { echo "<img src=images/open.gif>"; } else { echo "<img src=images/closed.gif>"; }
?></td>
   <td>D2GS AR1.10(Wallbot):</td>
   <td><?php
if ($fp = @fsockopen("wow.pkaclan.com","4000")) { echo "<img src=images/open.gif>"; } else { echo "<img src=images/closed.gif>"; }
?></td>
   <td> </td>
   <td> </td>
 </tr>
 <tr>
   <td>Hell Unleashed Realm is : </td>
   <td><?php
if ($fp = @fsockopen("bnet.pkaclan.com","9113")) { echo "<img src=images/open.gif>"; } else { echo "<img src=images/closed.gif>"; }
?></td>
   <td>D2GS HU1.10 (Gnecromancer): </td>
   <td><?php
if ($fp = @fsockopen("24.71.57.148","4000")) { echo "<img src=images/open.gif>"; } else { echo "<img src=images/closed.gif>"; }
?></td>
   <td>D2GS HU1.10 (D2Classic): </td>
   <td><?php
if ($fp = @fsockopen("69.133.108.231","4000")) { echo "<img src=images/open.gif>"; } else { echo "<img src=images/closed.gif>"; }
?></td>
 </tr>
 <tr bgcolor="#990000">
... more of the same ...

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

this script may miss several responses. any way to set the timeout up to 60000 ms? Chances will be better to find a responding server...

Mwaha!

Yes, in fact you can, with the optional fifth parameter. =D Whether it works depends on your sockets library.

int fsockopen ( string target, int port [, int errno [, string errstr [, float timeout]]] )

Too bad the guy never heard of caching....

Hmm.. Neat idea but lousy execution.. :-)

I would probably do the lookup in a small separate script and call that script via an IFRAME. Page pops up and status-images are shown when they are ready.. :-)

Great WTF! :)

Did this guy realize that his script would make a connection to every server each time the page is loaded? I wonder if he knew what a cron job is... Heck, I wonder if he knew as well what a Denial-Of-Service attack is. :D

And don't forget the "CopyPaste-Based Serverlist Management"... :D

Or, if it just *had* to display the status of each server on every page refresh, the page should be static HTML and each image request should have a src="server_status_image.php?server=blah&port=blah"...

Just put your PHP code into the "image" file, and return an appropriate image.

Voila, instant multi-tasking...

Raise the clock speed, make the program smaller, and try reworking those server addresses, it'll go fine...

But still... WTF!?

My favorite thing about this is how none of the sockets ever get closed.

I wonder what his netstat output looks like.

And what's the deal with this guy? They can open socket connections, but can't use a foreach loop?

You'll be surprised to the amount of site with this kind of code ;)

how about fclose lol.

Post new comment




*

  • Web and e-mail addresses are automatically converted into links.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <pre> <p> <br /> <br>