gusucode.com > 同城苏州黄页系统php源码程序 > lib/client.class.php

    <?

class client
{
###################################
# 初始化
	function client( $create_if_none = true )
	{
		$this->now = time();
		
		global $db ;
		$this->db =& $db;

		$this->client['ip']						 = $_SERVER['REMOTE_ADDR'] ;
		$this->client['http_accept_language']	 = $_SERVER['HTTP_ACCEPT_LANGUAGE'] ;
		$this->client['http_user_agent']		 = $_SERVER['HTTP_USER_AGENT'] ;
		$this->http_referer						 = @$_SERVER['HTTP_REFERER'] ;

		$this->how_to_identification_client = ( $value=getSystem('how_to_identification_client') ) ?	$value : 'ip' ;
		$this->client_expire = getSystem('client_expire') ;

		$this->_load_client( $create_if_none ) ;

	}

###################################
# 公有方法

	function log_show_ad( &$putin )
	{
		if( !$this->_is_op( $putin['id'], 'show' ) )
		{
			$this->_insert_client_log( 'show', $putin ) ;
			$putin['show_once'] = true ;
		}

		$this->db->increase( 'client', 'show', "`id`={$this->client_id}"  );
	}

	function log_click_ad( &$putin )
	{
		if( !$this->_is_op( $putin['id'], 'show' ) )
		{ 
			$this->db->debug();
			return false ;
		}

		if( !$this->_is_op( $putin['id'], 'click' ) )
		{
			$this->_insert_client_log( 'click', $putin ) ;
			$putin['click_once'] = true ;
		}
 
		$this->db->increase( 'client', 'click', "`id`={$this->client_id}"  );
	}

	/*
	 * 清除记录
	 */
	function clear_cookie()
	{
		// 删除 cookie
		setcookie ("client_id", "", $this->now - 3600) ;
		$this->db->delete( 'client',"`id`={$this->client_id}" ) ;
	}
	function clear_table_client()
	{
		// 清除 client 表
		$this->db->delete( 'client',"`time` < ".($this->now - $this->client_expire) ) ;
		return mysql_affected_rows();
	}
	function clear_table_client_log()
	{
		// 清除 client_log 表
		if( $r = $this->db->select( "SELECT client_log.client FROM `client_log` LEFT JOIN `client` ON client_log.client = client.id WHERE client.id IS NULL or `client_log`.`time` < ".($this->now - $this->client_expire) ) )
		{
			foreach($r as $rr)
				$arr[] = $rr['client'] ;
			$this->db->delete( 'client_log', '`client` in (' . implode(',',$arr) . ')' ) ;
		}

		return mysql_affected_rows();
	}

###################################
# 私有方法

	function _is_op( $putin_id, $op )
	{
		return ( $this->db->getrow( 'client_log', "`client`={$this->client_id} and `putin`={$putin_id} and `op`='{$op}'") != false ) ;
	}

	function _insert_client_log( $op, $putin )
	{
		$data = array(
			'client'		=> $this->client_id ,
			'putin'			=> $putin['id'] ,
			'ad'			=> $putin['ad'] ,
			'time'			=> $this->now ,
			'ip'			=> $this->client['ip'] ,
			'op'			=> $op ,
			'site'			=> $putin['site'] ,
			'http_referer'	=> $this->http_referer
		);

		return $this->db->insert( 'client_log', $data ) ;
	}

	function _create_client()
	{
		$this->client['time'] = $this->now ;

		$this->db->insert( 'client', $this->client ) ;
		$this->client_id = $this->client['id'] = mysql_insert_id() ;

		$this->_save_client() ;
	}

	function _load_client( $create_if_none )
	{
		$client_expire_where = "and `time` > " . ( $this->now - $this->client_expire ) ;
		
		switch( $this->how_to_identification_client )
		{
			case 'ip' :
				$client = $this->db->getrow('client',"`ip`='{$this->client['ip']}' {$client_expire_where}") ;	
				break ;

			case 'cookie' :
				if( !empty($_COOKIE['client_id']) )
					$client = $this->db->getrow('client',"`id`='{$_COOKIE['client_id']}' {$client_expire_where}") ;	

				break ;
		}

		if( @$client )
		{
			$this->client_id = $client['id'] ;
			$this->client['show'] = $client['show'] ;
			$this->client['click'] = $client['click'] ;
		}
		else if( $create_if_none )
			$this->_create_client() ;
	}

	function _save_client()
	{
		setcookie( 'client_id', $this->client_id, $this->now + $this->client_expire ) ;
	}


###################################
# 属性
	
	var $db ;

	var $client_id ;


	var $client ;
/*	var $client['ip'] ;
	var $client['time'] ;
	var $client['http_accept_language'] ;
	var $client['http_user_agent'] ;
	var $client['show'] = 0 ;
	var $client['click'] = 0 ;	*/


	var $how_to_identification_client ;
	var $client_expire ;
	var $http_referer ;

	var $now ;

}
?>