NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming > CODE
Reload this Page Logging class

CODE This forum is for posting code snippets and example scripts that aren't quite tutorials, but could be useful for others. You may post code snippets and/or completed scripts that you've written and want to share here.

Advanced Search
7 members in live chat ~  


Closed Thread
 
LinkBack Thread Tools
Old 01-27-2008, 01:19 PM THREAD STARTER               #1 (permalink)
Danltn.com
 
Daniel's Avatar
Join Date: May 2007
Location: Danltn.com / Nottingham, UK
Posts: 1,201
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"], 0250);
        
$this->data['user_agent'] = substr($_SERVER['HTTP_USER_AGENT'], 0250);
        
$this->data['url'] = substr($_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"], 0250);
????: NamePros.com http://www.namepros.com/code/424339-logging-class.html
        
$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']}');";
????: NamePros.com http://www.namepros.com/showthread.php?t=424339
        
$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.
Daniel is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


 
All times are GMT -7. The time now is 07:18 PM.

Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger