Web Hosting Talk







View Full Version : php script problem


MGCJerry
08-12-2002, 02:31 AM
Guess who? Its me again with a petty script problem.

I'm trying to have a script query a page that is dynamically created and have the script display whether or not a predetermined value was found.

I have some ereg_replace() commands in there in case some of the queried page didnt have anything that php doesn't like.

Everything works fine all the way up to the "if ereg... etc" statement (verified by using echo $buff5; ) . Regardless of the value to look for, it always returns false.

I have dug into the manual, but everything I try returns false. I tried working on this for 5 hours and I havent gone past this part.

Quick summary:
I want this script to see if my OpenRPG game server is running by probing the central server. If it is running, the script tells you if its running, otherwise it says it isn't running. I want to put this on my site so my visitors can see if my server is up.

Any suggetions?

Thanks in advance.


<?php

$fd = fopen("http://my.openrpg.com/openrpg_servers.php", "r");
while (!feof ($fd)) {
$buffer = fgets($fd, 4096);
//Replace the crap in the metaserver
$buff0 = ereg_replace ("\"", " ", $buffer);
$buff1 = ereg_replace ("\'", "", $buff0);
$buff2 = ereg_replace ("<servers>", "", $buff1);
$buff3 = ereg_replace ("<server", "", $buff2);
$buff4 = ereg_replace ("/>", "", $buff3);
$buff5 = ereg_replace ("2 The Xtreme", "2TX", $buff4);
//Look for Our Server
if (ereg ("OpenRPG Dev", $buff5)) {
echo "Our server is running\n";
return;
} else {
echo "Our Server is not Running\n";
return;
}
}
fclose ($fd);

?>


edit
If you want to check for a server that is always running, have the script search for OpenRPG Dev.

michaeln
08-12-2002, 08:09 AM
First you are simply searching the string $buffer to see if "OpenRPG Dev" is in it. So why go through the trouble and CPU resources to take all of that other crap out. It doesn't matter.

Your script doesn't work because you have put your conditional IF statements in your while loop taking in the information from the server.

So what is happening is your script enters the while loop fgets the first line into $buffer searches for all of those things that aren't there because there is only one line. It then hits the IF and checks to see if that string is there. It isn't since there is only the first line in the string you are searching through so it hits the else. Prints to the screen the server is not up and ends the script.

Take the If statements out of the while loop. What I have below should work perfectly....

You might try this:

<?php
$fd = fopen("http://my.openrpg.com/openrpg_servers.php", "r");
$buffer = fread($fd, 4096);
fclose ($fd);

//Look for Our Server
if (strstr($buffer, "OpenRPG Dev"))
{
echo "Our server is running\n";
return;
}
else
{
echo "Our Server is not Running\n";
return;
}
?>

michaeln
08-12-2002, 10:34 AM
After thinking about it some more this one is actually a bit faster. I have tested both of my scripts and they work.


<?php
$url = "my.openrpg.com";
$query = "/openrpg_servers.php";
$fp = fsockopen($url, 80, $errno, $errstr, 30);
if(!fp)
{
$content = "$errstr ($errno)";
}
else
{
fputs($fp, "GET $query HTTP/1.0\r\nHost: $url\r\n\r\n");
$buffer = fread($fp, 4096);
fclose($fp);
//Look for Our Server
if (strstr($buffer, "OpenRPG Dev"))
{
$content = "Our server is running\n";
}
else
{
$content = "Our Server is not Running\n";
}
}

echo $content;
?>

MGCJerry
08-12-2002, 11:19 AM
Thanks a bunch michaeln.

Should have known it was going to be a loop. The first snippet was easer to understand and thet let me see what was wrong :o plus your explination was a great help too.

michaeln
08-12-2002, 12:08 PM
No problem... :D