[advanced search]
Next Live Event: NamePros Live Auction, May 23rd at 6PM EDT
Results from the May 8th live auction are here.
15 members in the live chat room. Join Chat!
Register Rules & FAQ NP$ Store Active Threads Mark Forums Read
Go Back   NamePros.Com > Design and Development > Programming > CODE
User Name
Password

Old 01-27-2008, 01:19 PM   · #1
Daniel
Danltn.com
 
Daniel's Avatar
 
Name: Daniel Neville
Location: Danltn.com / Nottingham, UK
Trader Rating: (65)
Join Date: May 2007
Posts: 1,162
NP$: 638.56 (Donate)
Daniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond repute
Ethan Allen Fund Ethan Allen Fund
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.


Please register or log-in into NamePros to hide ads
Daniel is offline   Reply With Quote
Reply

NamePros is a revenue sharing forum.

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


Site Sponsors
Proof is in the Parking Proof is in the Parking Find out now!
Advertise your business at NamePros
All times are GMT -7. The time now is 06:17 AM.


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