Getting information from a Text File

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nate660
Forum Newbie
Posts: 5
Joined: Sun Aug 24, 2008 12:36 am

Getting information from a Text File

Post by nate660 »

Alright i have this working 100%
http://sampbrowser.hostwebsfree.com/test.php
But How can skip an unresponsive server IP and go to the next line in the .txt

Part of the code is here

Code: Select all

$handle = fopen("http://sampbrowser.hostwebsfree.com/Servers.txt", "r");
 
?>
        <table width="701" border="0" bordercolor="#000000">
        <tr>
        <td bgcolor="#2B5486">Hostname:</td>
        <td bgcolor="#2B5486">Players:</td>
        <td bgcolor="#2B5486">Gamemode:</td>
        <td bgcolor="#2B5486">Password:</td>
        </tr>
<?php   
    
    while (!feof($handle))
    {
        $Data = fgets($handle);
        $IP = split("[ ]+", $Data);
        $Loop = count($IP);
 
        for($i=0;$i<$Loop;$i++)
        {
            $DATOS = split(":", $IP[$i]);
            $Ipx = $DATOS[0];
            $Portx = $DATOS[1];
                        
            ////// COMIENZO SCRIPT QUERY ///////
            
            $ip = $Ipx;
 
            $port = $Portx;
 
            $fp = fsockopen('udp://' . $ip, $port, $errno, $errstr);
            if (!$fp)
            {
                echo "<tr><td bgcolor=\"#2B5486\">Socket Error: $errno - $errstr</td></tr>\n";
            }
            else
            {
            $packet = 'SAMP';
            $packet .= chr(strtok($ip, '.'));
            $packet .= chr(strtok('.'));
            $packet .= chr(strtok('.'));
            $packet .= chr(strtok('.'));
            $packet .= chr($port & 0xFF);
            $packet .= chr($port >> 8 & 0xFF);
    
            fwrite($fp, $packet.'i');
            fread($fp, 11);
            $is_passworded = ord(fread($fp, 1));
            $plr_count     = ord(fread($fp, 2));
            $max_plrs      = ord(fread($fp, 2));
            $strlen        = ord(fread($fp, 4));
            $hostname      = fread($fp, $strlen);
            $strlen        = ord(fread($fp, 4));
            $gamemode      = fread($fp, $strlen);
            $strlen        = ord(fread($fp, 4));
            $mapname       = fread($fp, $strlen);
    
            echo '      <tr><td bgcolor="#2B5486" colspan="1" align="center"><b>' . $hostname . '</b></td>'."\n";
            echo '      <td bgcolor="#2B5486" width="100">' . $plr_count . ' / ' . $max_plrs . '</td>'."\n";
            echo '      <td bgcolor="#2B5486">' . $gamemode . '</td>'."\n";
            //echo '      <td bgcolor="#2B5486">MapName: </td><td bgcolor="#2B5486">' . $mapname . '</td></tr>'."\n";
            
            if ($is_passworded)
            {
                echo '      <td bgcolor="#2B5486">Yes</td>'."\n";
            } else {
                echo '      <td bgcolor="#2B5486">No</td></tr>'."\n";
            }
    fwrite($fp, $packet.'c');
Thanks for you PHP developers who help
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Getting information from a Text File

Post by califdon »

I would test whether $handle == False and if so, skip the attempts to read from it, or substitute a warning message to the user.
nate660
Forum Newbie
Posts: 5
Joined: Sun Aug 24, 2008 12:36 am

Re: Getting information from a Text File

Post by nate660 »

hmm it seems you might no more about skipping the file then me,

Can you point me in the direction
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Getting information from a Text File

Post by califdon »

nate660 wrote:hmm it seems you might no more about skipping the file then me,

Can you point me in the direction
Do you know anything about programming in PHP?
nate660
Forum Newbie
Posts: 5
Joined: Sun Aug 24, 2008 12:36 am

Re: Getting information from a Text File

Post by nate660 »

Very Little, Just enough to get me this far
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Getting information from a Text File

Post by califdon »

nate660 wrote:Very Little, Just enough to get me this far
I'm sorry to be so hard on you, but this is a forum for PHP developers to help each other, and you're unlikely to find someone who will just rewrite scripts for you. That said, you could at least do this:

Code: Select all

...
$handle = fopen("http://sampbrowser.hostwebsfree.com/Servers.txt", "r");
if ($handle == False) {
  echo "Server is not responding."
} else {
...    (all the rest of your code)  ...
}
That final closing bracket is critical, and it must come before the end of the PHP code ?>.
nate660
Forum Newbie
Posts: 5
Joined: Sun Aug 24, 2008 12:36 am

Re: Getting information from a Text File

Post by nate660 »

Ahh you didn't really understand what i meant

When its getting the Server IP from the .txt everything works until it reaches an unresponsive server IP and then it just stops the rest of the IP's from loading

So is there a way i can Block out the unresponsive IP's??? so it don't try to load them

Also If you need to see the script your more than welcome to

Code: Select all

<html>
<head>
<title>Nate and Kevins SA:MP Browser</title>
</head>
<body bgcolor="#1B304B" text="#EEEEEE">
<BR><BR><center><IMG SRC="http://www.sa-mp.com/images/logo.gif" WIDTH="655" HEIGHT="155" BORDER="0" ALT="">
<div style="width:700px;height:450px;border:1px solid black;">
<h2>SA:MP Webbrowser Server List</h2>
 
<p style="text-align: left;">&nbsp;</p>
<?php
set_time_limit(900);
$handle = fopen("http://master.sa-mp.com/0.2.2/servers", "r");
 
?>
        <table width="701" border="0" bordercolor="#000000">
        <tr>
        <td bgcolor="#2B5486">Hostname:</td>
        <td bgcolor="#2B5486">Players:</td>
        <td bgcolor="#2B5486">Gamemode:</td>
        <td bgcolor="#2B5486">Password:</td>
        </tr>
<?php   
    
    while (!feof($handle))
    {
        $Data = fgets($handle);
        $IP = split("[ ]+", $Data);
        $Loop = count($IP);
 
        for($i=0;$i<$Loop;$i++)
        {
            $DATOS = split(":", $IP[$i]);
            $Ipx = $DATOS[0];
            $Portx = $DATOS[1];
                        
            ////// COMIENZO SCRIPT QUERY ///////
            
            $ip = $Ipx;
 
            $port = $Portx;
 
            $fp = fsockopen('udp://' . $ip, $port, $errno, $errstr);
                       
            
                        if($fp){
                        @socket_set_timeout($fp,1);
                         $packet = 'SAMP';
            $packet .= chr(strtok($ip, '.'));
            $packet .= chr(strtok('.'));
            $packet .= chr(strtok('.'));
            $packet .= chr(strtok('.'));
            $packet .= chr($port & 0xFF);
            $packet .= chr($port >> 8 & 0xFF);
      
            fwrite($fp, $packet.'i');
            fread($fp, 11);
$is_passworded = ord(fread($fp, 1));
            $plr_count     = ord(fread($fp, 2));
            $max_plrs      = ord(fread($fp, 2));
            $strlen        = ord(fread($fp, 4));
            $hostname      = fread($fp, $strlen);
            $strlen        = ord(fread($fp, 4));
            $gamemode      = fread($fp, $strlen);
            $strlen        = ord(fread($fp, 4));
            $mapname       = fread($fp, $strlen);
    
            echo '      <tr><td bgcolor="#2B5486" colspan="1" align="center"><b>' . $hostname . '</b></td>'."\n";
            echo '      <td bgcolor="#2B5486" width="100">' . $plr_count . ' / ' . $max_plrs . '</td>'."\n";
            echo '      <td bgcolor="#2B5486">' . $gamemode . '</td>'."\n";
            //echo '      <td bgcolor="#2B5486">MapName: </td><td bgcolor="#2B5486">' . $mapname . '</td></tr>'."\n";
            
            if ($is_passworded)
            {
                echo '      <td bgcolor="#2B5486">Yes</td>'."\n";
            } else {
                echo '      <td bgcolor="#2B5486">No</td></tr>'."\n";
            }
    fwrite($fp, $packet.'c');
 
            
                fclose($fp);
                }           
            }
}
 
        
 
    ?>;
 
 
 
</table>
<p style="text-align: left;">&nbsp;</p>
<br></br>
<br></br>
<br></br>
<br></br>
<br></br>
<br></br>
<br></br>
<br></br>
<br></br>
   
      
<font size="0.5">Copyright &copy;Nate and Kevin 2008
</div>
</body>
</html>
 
Thanks alot for your help
Post Reply