Jump to content

PHP fread error


peregrinewonder

Recommended Posts

Hello All,

 

I keep getting this error:

 

"Warning: fread() [function.fread]: Length parameter must be greater than 0. in /home/ooatwopw/public_html/serverstatus.php on line 85"

 

by running this code:

 

<?php

// Specify ip address or name of NWN server
$ipaddr = '64.72.59.205'; // Example: The World Of Avlis, main server

// Specify port of server (5121 is default)
$port = '5121';

// Specify timeout (maximum time to wait for connection and data)
$timeout = 5; // five seconds;

function Connect()
{
        global $ipaddr, $port, $timeout;

        // open connection to server
        $fp = fsockopen("udp://" . $ipaddr, $port, $errno, $errstr, $timeout);

        // return error if connection fsockopen failed
        if (!$fp)
                echo "ERROR: $errno - $errstr<br>\n";

        // Set timeout on socket
        socket_set_timeout($fp, $timeout);

        // return connection
        return $fp;
}

function Disconnect($fp)
{
        // Done reading, close connection
        fclose($fp);
}

function QueryServer($fp)
{
        // Send query request to server
        $query = '\\status\\';
        fwrite($fp,$query);

// this counter acts as a safety net
$passes = 0;

        // Read in everything the server sends us back
        while (!$status['eof'] && ($passes < 10)) 
        {
        $singlechar = fread($fp,1); 
        $serverinfo = $serverinfo . $singlechar;

                $status = socket_get_status ($fp);
                $bytes = $status["unread_bytes"];
                $serverinfo = $serverinfo . fread($fp, $bytes);

                if (strstr($serverinfo, '\\final\\\\queryid'))
                {
                        break;
                }
                
                $passes++;
        }               

        // The string 'queryid' is at the end of every packet
        // We need to delete it so parsing will be easier later on
        $del = "\\queryid\\";
        while (strstr($serverinfo, $del)) 
        {
                // preg_replace seems to be unable to handle backslashes, so...
        
                $start = strpos($serverinfo, $del);
                $left = substr($serverinfo, 0, $start);
                $right = substr($serverinfo, $start + strlen($del));
                
                $right = substr($right, strpos($right, '\\'));
                $serverinfo = $left . $right;
        }

        // return what the server sent us
        return $serverinfo;
}

//
// Parse server info and return data as a map / associative array
//
function ParseServerInfo($serverinfo)
{
        $lines = explode("\n", $serverinfo);
        $data = explode("\\", $lines[0]);

        for ($i = 1; $i < count($data); $i+=2)
        {
                $name = $data[$i];
                $value = $data[$i+1];
                $map[$name] = $value;
                //print "$name = $value\n";
        }
        
        return $map;
}

//
// Pare module description and return it as a string
// TODO: Needs some formatting
//
function ParseModuleInfo($serverinfo)
{
        // extract module description, part 1
        // (everything except last line)
        $lines = explode("\n", $serverinfo);
        
        for ($i = 1; $i < count($lines) - 1; $i++)
                $moduledesc = $moduledesc . $lines[$i];

        // extract module description, part 2
        // (extract description part of last line)
        $lastDescLine = $lines[$i];
        $moduledesc = $moduledesc . substr($lastDescLine, 0, strpos($lastDescLine, "\\"));
        
        return $moduledesc;
}

//
// Parse player list and return it in a two dimensional, associative array
//
function ParsePlayerList($serverinfo, $numPlayers)
{
        // parse player list
        $i=0;
        while ( ($i < 63) && (count($playerdata) < $numPlayers)) // max 63 players online
        {
                $pos_start = strpos($serverinfo, "player_" . $i);
                if ($pos_start != 0) // player was found
                {
                        $playerinfo = substr($serverinfo, $pos_start, 144);  // umm. yes. that's ugly.
                        //print "<p>player $i info:$playerinfo</p>";                        
                        $playerinfo = explode("\\", $playerinfo);
                        
                        $newEntry = count($playerdata);
                        $playerdata[$newEntry]['playername'] = $playerinfo[1];
                        $playerdata[$newEntry]['type'] = $playerinfo[3];
                        $playerdata[$newEntry]['charname'] = $playerinfo[5];
                        $playerdata[$newEntry]['charlevel'] = $playerinfo[7];
                } 
                $i++;
        }
        return $playerdata;
}

?>

<html>
<head><title> Players online </title></head>

<body>
<h3> Server status </h3>

<?php

$fp=Connect();
$serverinfo = QueryServer($fp);
Disconnect($fp);

$server = ParseServerInfo($serverinfo);
$moduledesc = ParseModuleInfo($serverinfo);
$playerdata = ParsePlayerList($serverinfo, $server['numplayers']);

/*****************************************
* Create HTML output                    *
*****************************************/

// Output online / offline status

print "<p>";
print "Server is <b>";
        
if (strlen($server['gamename']) > 0)
{
        print "online</b></p>\n";
        
        // Output server information
        ?>
        
        <table border=1>
        <caption> Full server information</caption>
        <tr>
          <th>Property</th>
          <th>Value</th>            
        </tr>

        <tr><td>Gamename</td> <td><?php echo $server['gamename']?></td></tr>
        <tr><td>Version</td> <td><?php echo $server['gamever']?></td></tr>
        <tr><td>Location</td> <td><?php echo $server['location']?></td></tr>
        <tr><td>Group id</td> <td><?php echo $server['groupid']?></td></tr>
        <tr><td>Host Address</td> <td><?php echo $ipaddr ?></td></tr>        
        <tr><td>Host port</td> <td><?php echo $server['hostport']?></td></tr>
        <tr><td>Server Name</td> <td><?php echo $server['hostname']?></td></tr>
        <tr><td>Module Name</td> <td><?php echo $server['mapname']?></td></tr>
        <tr><td>Game Type</td> <td><?php echo $server['gametype']?></td></tr>
        <tr><td>Players</td> <td><?php echo $server['numplayers']?></td></tr>
        <tr><td>Max Players</td> <td><?php echo $server['maxplayers']?></td></tr>
        <tr><td>Password Required</td> <td><?php 
                if ($server['password'] == 0) echo 'no';
                else echo 'yes'; ?></td></tr>
        <tr><td>Game mode</td> <td><?php echo $server['gamemode']?></td></tr>
        <tr><td>Allow Local Characters</td> <td><?php 
                if ($server['localvault'] == 0) echo 'no';
                else echo 'yes'; ?></td></tr>
        <tr><td>Level Range min</td> <td><?php echo $server['levelrnglow']?></td></tr>
        <tr><td>Level Range max</td> <td><?php echo $server['levelrnghigh']?></td></tr>
        <tr><td>Player vs Player</td> <td><?php echo $server['PVP']?></td></tr>
        <tr><td>Only One Party</td> <td><?php 
                if ($server['onepartyonly'] == 0) echo 'no';
                else echo 'yes'; ?></td></tr>
        <tr><td>Player Pause Enabled</td> <td><?php 
                if ($server['playerpause'] == 0) echo 'no';
                else echo 'yes'; ?></td></tr>
        <tr><td>Server Version</td> <td><?php echo $server['build']?></td></tr>
        <tr><td>Enforce Legal Characters</td> <td><?php 
                if ($server['legalchars'] == 0) echo 'no';
                else echo 'yes'; ?></td></tr>
        <tr><td>Item restriction</td> <td><?php 
                if ($server['itemrestrict'] == 0) echo 'no';
                else echo 'yes'; ?></td></tr>

        </table>


        <p><b>Module details</b><br><br>
        <?php echo $moduledesc ?></p>

        <?php

        // Output player information
        if ($server['numplayers'] > 0)
        { 
                if ($server['numplayers'] == 1)    
                        print "<p>There is one player online.</p>\n";   
                else
                        print "<p>There are " . $server['numplayers'] . " players online.</p>\n";  
                ?>
                
                <table border=1>
                <caption>Playerlist</caption>
                <tr>
                  <th>Player</th>
                  <th>Character</th>            
                  <th>Level</th>                
                </tr>

                <?php
                foreach ($playerdata as $player)
                {
                        print "<tr>\n  <td>" . $player['playername'] . "</td>\n";
                        print "  <td>" . $player['charname'] . "</td>\n";
                        print "  <td>" . $player['charlevel'] . "</td>\n</tr>\n";       
                }
                print "</table>";
        }
        else
        {
                print "<p>There are no players online.</p>\n";
        }        
        
}        
else  // server hostname is empty
{
        print "offline</b>. No additional info available, sorry.</p>\n";
}      


?>

</body>
</html>

 

This is line 85:

 

                $serverinfo = $serverinfo . fread($fp, $bytes);

 

 

Any ideas why this isn't working? Thanks in advance! :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.