Image

Imagespilltheblood wrote in Imagephp

File problem.

Hello all.

I have to make a site counter for a site I have hosted on someone else's server. However, I do not have access to Mysql, the guy gave me all the info, I lost it, and I don't really talk to him for reasons I prefer not to discuss, so I'll just say "mysql is not available to me". Anyways, my code is supposed to do the following, but is failing miserably. By the way, I am testing it on XP/IIS/PHP 4.3.2 (i think).

- Read contents of file as an array.
- Contents of file are IP addresses.
- If the current IP address is not found in the file contents array, write the new IP address to a new line in the file.
- Read the file, return the count.

Here is part #1 of my code, which handles the whole operation:




<?php

/***
* @filename counter.php
* @author Stephen Dill <stephen.dill@phoenixpowered.com>
* @copyright Copyright (C) 2004 Stephen Dill.
*/

    // System Settings
        
ini_set('error_reporting', E_ALL);
        
    
// Require Application Libraries
        
require('class_lib.php');
        
    
// Instantiate File classes
        
$file = new file_connection();
        
?>

<html>
<head>
<title>Counter Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>

    <?php
    
        
/***
         * Define some variables.
         */
             
$remote_addr = $_SERVER['REMOTE_ADDR'];
    
        
/***
         * Open counter file.
         */
             
$file->open('a ', 'counter.txt');
             
        
/***
         * Read contents of file.
         */
             
$file_reader = $file->return_reader();
             
$contents = $file_reader->read_as_array();
             
        
/***
         * Determine if this IP is in $contents.
         * Write IP to file if IP is not in $contents.
         */
             
$search = ( in_array($remote_addr, $contents) ) ? true : false;
             if ( !
$search )
             {
                 
$str = $remote_addr . "\r\n";
                 
$file_writer = $file->return_writer($str);
                 
$file_writer->write();
                 
$contents = $file_reader->read_as_array();
             }
             
        
/***
         * Return counter
         */
             
echo count($contents) . ' visitor(s).';
             
    
?>

</body>
</html>








Here is the file classes, if you should wonder what the hell is going on in them. lol.. they aren't guaranteed to work either, files are pain!






<?php

/***
* @filename class_lib.php
* @author Stephen Dill <stephen.dill@phoenixpowered.com>
* @copyright Copyright (C) 2004 Stephen Dill.
*/

/***
* @class file_writer
* @rev 1.0
*/     
    
class file_writer
    
{
        var
$conn_obj;
        var
$str;
        
        function
file_writer($str, $conn_obj)
        {
            
$this->str = trim($str);
            
$this->conn_obj = $conn_obj;
        }
        
        function
write()
        {
            if ( !
$this->conn_obj->is_connected() )
            {
                
$this->conn_obj->open();
            }
            
$this->rewind_file();
            
$this->lock_file();
            
$rslt = @fwrite($this->conn_obj->get_connection_id(), $this->str);
            
$this->unlock_file();
            if ( !
$rslt )
            {
                
$err_str  = '<b>file_writer::write()</b> - Failed attempt.<br />';
                
$err_str .= 'Could not write to file \'' . $this->conn_obj->filename .'\'[' . $this->conn_obj->filemode . '].';
                return new
error_manager($this->conn_obj->get_connection_id(), $err_str);
            }
            else
            {
                return
true;
            }
        }
        
        function
rewind_file()
        {
            return @
rewind($this->conn_obj->get_connection_id());             
        }
        
        function
lock_file()
        {
            return @
flock($this->conn_obj->get_connection_id(), LOCK_EX);             
        }
        
        function
unlock_file()
        {
            return @
flock($this->conn_obj->get_connection_id(), LOCK_UN);             
        }
    }

/***
* @class file_reader
* @rev 1.0
*/     
    
class file_reader
    
{
        var
$conn_obj;
        
        function
file_reader($conn_obj)
        {
            
$this->conn_obj = $conn_obj;
        }
        
          function
rewind_file()
        {
            return @
rewind($this->conn_obj->get_connection_id());             
        }        
        
        function
read_as_array()
        {
            if ( !
$this->conn_obj->is_connected() )
            {
                
$this->conn_obj->open();
            }
            
$this->rewind_file();
            
$rslt = @file($this->conn_obj->filename);
            if ( !
$rslt )
            {
                
$err_str  = '<b>file_reader::read_as_array()</b> - Failed attempt.<br />';
                
$err_str .= 'Could not read file \'' . $this->conn_obj->filename .'\'[' . $this->conn_obj->filemode . '] as array.';
                return new
error_manager($this->conn_obj->get_connection_id(), $err_str);
            }
            else
            {
                return
$rslt;
            }
        }
        
        function
read_as_str()
        {
            if ( !
$this->conn_obj->is_connected() )
            {
                
$this->conn_obj->open();
            }
            
$this->rewind_file();
            
$rslt = @fread($this->conn_obj->get_connection_id(), filesize($this->conn_obj->filename));
            if ( !
$rslt )
            {
                
$err_str  = '<b>file_reader::read_as_str()</b> - Failed attempt.<br />';
                
$err_str .= 'Could not read file \'' . $this->conn_obj->filename .'\'[' . $this->conn_obj->filemode . '] as string.';
                return new
error_manager($this->conn_obj->get_connection_id(), $err_str);
            }
            else
            {
                return
$rslt;
            }
        }
      }

/***
* @class file_connection
* @rev 1.0
*/       
    
class file_connection
    
{
        var
$connection_id;
        var
$filename;
        var
$filemode;
        
        function
file_connection($filename = '', $filemode = 'a ')
        {
            
$this->filename = trim($filename);
            
$this->filemode = trim($filemode);
        }
        
        function
open($filemode = '', $filename = '')
        {
            
$filename = trim($filename);
            
$filemode = trim($filemode);
            
$this->filename = ( empty($filename) ) ? $this->filename : $filename;
            
$this->filemode = ( empty($filemode) ) ? $this->filemode : $filemode;
            
            if ( !
$this->is_connected() )
            {
                
$this->connection_id = @fopen($this->filename, $this->filemode);
                if ( !
$this->is_connected() )
                {
                    
$err_str  = '<b>file_connection::open()</b> - Failed attempt.<br />';
                    
$err_str .= 'Could not open \'' . $this->filename . '\'[' . $this->filemode . ']';
                return new
error_manager('', $err_str);
                }
            }
        }
        
        function
close()
        {
            if (
$this->is_connected() )
            {
                
$this->connection_id = @fclose($this->connection_id);
            }
        }
        
        function
is_connected()
        {
            if (
is_resource($this->connection_id) )
            {
                return
true;
            }
            else
            {
                return
false;
            }
        }
        
        function
get_connection_id()
        {
            if (
$this->is_connected() )
            {
                return
$this->connection_id;
            }
        }
        
        function
return_reader()
        {
            return new
file_reader($this);
        }
        
        function
return_writer($str)
        {
            return new
file_writer($str, $this);
        }
    }
    
?>




Any ideas, opinions or help is greatly appreciated. Thanks much in advance! :)