NamePros.Com (http://www.namepros.com/)
-   CODE (http://www.namepros.com/code/)
-   -   Logging class (http://www.namepros.com/code/424339-logging-class.html)

Daniel 01-27-2008 01:19 PM

Logging class
 
PHP Code:
<?php

/**
* Danltn | http://danltn.com/
* No warranty is given to code used
* Under Attribution-Noncommercial-No Derivative Works 3.0 Unported License
* http://creativecommons.org/licenses/by-nc-nd/3.0/
* Version 0.1
*/

/** SQL Dump:

CREATE TABLE `logs` (
`id` int(11) NOT NULL auto_increment,
`get` text NOT NULL,
`post` text NOT NULL,
`cookie` text NOT NULL,
`time` int(11) NOT NULL,
`referral` varchar(255) NOT NULL,
`user_agent` varchar(255) NOT NULL,
`url` varchar(255) NOT NULL,
`request_method` varchar(255) NOT NULL,
`ip` varchar(255) NOT NULL,
`host` varchar(255) NOT NULL,
`port` varchar(255) NOT NULL,
PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

**/

class logger
{
    
protected $mysql_user = "";
    
protected $mysql_pass = "";
    
protected $mysql_db = "";
    
protected $mysql_server = "";

    
public $data = array();

    
public $error = "";

    
public function __construct($mysql_user = "", $mysql_pass = "", $mysql_db = "", $mysql_server = "localhost", $log = 1)
    {
        if (
$log)
        {
            
/* Log is probably always on, It's here for further functionality (in the future.) */
            
$this->mysql_user = $mysql_user;
            
$this->mysql_pass = $mysql_pass;
            
$this->mysql_db = $mysql_db;
            
$this->mysql_server = $mysql_server;

            
$this->post_get_cookie();
            
$this->time();
            
$this->server();

            
$this->fix_nones();

            
$return = $this->query();

            return
$return;
        }
    }

    
protected function connect()
    {
        
$connection = @mysql_connect($this->mysql_server, $this->mysql_user, $this->mysql_pass);
        if (!
$connection)
        {
            
$this->error = mysql_error();
            return
false;
        }
        
$db_connection = @mysql_select_db($this->mysql_db, $connection);
        if (!
$db_connection)
        {
            
$this->error = mysql_error();
            return
false;
        }
        return
$connection;
    }

    
protected function post_get_cookie()
    {
        global
$_COOKIE;
        global
$_POST;
        global
$_GET;
        
/* Just in case */

        
$get = serialize($_GET);
        
$post = serialize($_POST);
        
$cookie = serialize($_COOKIE);

        
$this->data['get'] = $get;
        
$this->data['post'] = $get;
        
$this->data['cookie'] = $cookie;

        return
true;
    }

    
protected function time()
    {
        
$this->data['time'] = time();

        return
true;
    }

    
protected function server()
    {
        global
$_SERVER;

        
$this->data['referral'] = substr($_SERVER["HTTP_REFERER"], 0, 250);
        
$this->data['user_agent'] = substr($_SERVER['HTTP_USER_AGENT'], 0, 250);
        
$this->data['url'] = substr($_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"], 0, 250);
        
$this->data['request_method'] = $_SERVER["REQUEST_METHOD"];
        
$this->data['ip'] = $_SERVER["REMOTE_ADDR"];
        
$this->data['host'] = $_SERVER["REMOTE_HOST"];
        
$this->data['port'] = $_SERVER["REMOTE_PORT"];

        return
true;
    }

    
protected function fix_nones()
    {
        unset(
$name, $value);
        foreach (
$this->data as $name => &$value)
        {
            if (!
$value and $name != "time")
            {
                
$value = "[none]";
            }
        }
        unset(
$name, $value);
        return
true;
    }

    
protected function query()
    {
        
$data = $this->data;
        
$connection = @$this->connect();
        if (!
$connection)
        {
            
$this->error = mysql_error();
            return
false;
        }
        
$data = @array_map('mysql_real_escape_string', $data);
        
$sql = "INSERT INTO `logs` (`id`, `get`, `post`, `cookie`, `time`, `referral`, `user_agent`, `url`, `request_method`, `ip`, `host`, `port`) VALUES (NULL, '{$data['get']}', '{$data['post']}', '{$data['cookie']}', '{$data['time']}', '{$data['referral']}', '{$data['user_agent']}', '{$data['url']}', '{$data['request_method']}', '{$data['ip']}', '{$data['host']}', '{$data['port']}');";
        
$mysql_sql_send = @mysql_query($sql, $connection);
        if (
$mysql_sql_send)
        {
            return
true;
        }
        else
        {
            
$this->error = mysql_error();
            return
false;
        }
    }
}



/**

To do a basic log:

new logger("MySQL_Username", "MySQL_Password", "MySQL_Database", "MySQL_Server");

To check data after logging:
$log = new logger("MySQL_Username", "MySQL_Password", "MySQL_Database", "MySQL_Server");
echo $log->data['ip']; (Or whatever type of data you want to show.)

**/

?>


Constructive criticism is welcome via PM.
This class is still in development, a paginated output is on the way (so you can get your results out the DB and display them.)

Any suggestions for extending this code are welcome in the topic.

Any other comments, please do post in this topic.

Written in the hope it will be helpful, but no guarantee at all.


All times are GMT -7. The time now is 12:02 AM.
Site Sponsors
Advertise your business at NamePros

Powered by: vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 2.4.0