Login   Register  
PHP Classes
elePHPant
Icontem

File: count.php3

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of ton geen  >  counter.php  >  count.php3  >  Download  
File: count.php3
Role: ???
Content type: text/plain
Description: Copyright Korlyakov A. 1999
Class: counter.php
Author: By
Last change:
Date: 2000-02-08 02:41
Size: 6,223 bytes
 

Contents

Class file image Download
<?php
/*  Copyrihgt Korlyakov A. 1999
It is the simple counter with registration of visitings and
users agent - Internet Explorer(green),Netscape(blue) and Other(Red).
INSTALL:
  1. Create the necessary tables. See below.
  2. Create in MySql user counter,pass counter with access to these tables.
  3. Save code in file count.php3
  4. Add string <img src="http://your.host/count.php3"> where you want to put the counter.
  Note 1
   array $exception contains ip-addresses from which visiting do not count.
  Note 2
   If you have one page with different names like
    Id     URL
    4   http://www.some.com/
    12  http://www.some.com/index.html
    Choose Id from the table counter and change them in the table eq.
    WAS:   Id Id1
            4  4
            12 12
    make: update eq set Id=4 where Id1=12; 
    Became:Id Id1
            4  4
            4 12
==== CREATE TABLE ========
Make this command in mysql
create database count;
use count;
create table agents (
  Id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
  agent VARCHAR(255) NOT NULL,
  type ENUM("IE","Netscape","Other"),
  counts INT NOT NULL DEFAULT 0,
  last TIMESTAMP,
  open TIMESTAMP,
  KEY (Id),
  INDEX (agent(64)),
  UNIQUE (agent)
);
create table counters (
  Id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
  URL VARCHAR(255) NOT NULL,
  counts   INT UNSIGNED NOT NULL DEFAULT 0,
  IE       INT UNSIGNED NOT NULL DEFAULT 0,
  Netscape INT UNSIGNED NOT NULL DEFAULT 0,
  Other    INT UNSIGNED NOT NULL DEFAULT 0,
  last TIMESTAMP,
  open TIMESTAMP,
  KEY (Id),
  INDEX (URL(64)),
  UNIQUE (URL)
);
create table eq (
   id SMALLINT,
   id1 SMALLINT
);
*/
Header(  "Content-type:  image/gif");
  function ErrorGif($str) {
    $len=StrLen($str);
    $im=ImageCreate($len*6,16);          
    $black = ImageColorAllocate($im, 0,0,0);
    $red = ImageColorAllocate($im,255,0,0);
    ImageFill($im,0,0,$red);             
    ImageString($im,2,0,0,$str,$black);  
    ImageGif($im);
    ImageDestroy($im);
    exit;
  }
  function MySqlErrorGif($str) {
    $str=$str." ".mysql_errno().":".mysql_error();
    ErrorGif($str);
  }
$exception="195.82.31.54 192.168.22.22 ";
if (isset($HTTP_REFERER)) {
    if (@mysql_connect("count","","")<1) {
     ErrorGif("Can\'t connect to Mysql"); }
    if (!mysql_select_db("count") ) { 
     MySqlErrorGif ("select_db"); }
    if (ereg("$REMOTE_ADDR ",$exception)) {
	$result = mysql_query("select eq.id from eq,counters where (eq.id1=counters.id) AND URL='$HTTP_REFERER'");
	$id=mysql_result($result,0);
    } else {
	$result = mysql_query("SELECT id FROM counters WHERE URL='$HTTP_REFERER'");
	if (mysql_errno() != 0 ) { MySqlErrorGif ("select from counters"); }
	if(@mysql_numrows($result)!=1) { 
	    if (!mysql_query("INSERT counters set URL='$HTTP_REFERER',open=NULL")) { 
               MySqlErrorGif ("insert into counters"); }
     	    $id=mysql_insert_id();
	    if (!mysql_query("INSERT eq VALUES($id,$id)")) { 
               MySqlErrorGif ("insert into eq"); }
	} else { 
            $id=mysql_result($result,0); }
	if (!mysql_query("UPDATE counters SET counts=counts+1 WHERE id=$id")) { 
MySqlErrorGif ("UPDATE counters"); }
	$result = mysql_query("select id from eq where id1=$id");
	$id=mysql_result($result,0);
	if (mysql_errno() != 0 ) { 
MySqlErrorGif ("select from eq"); }
	$result = mysql_query("SELECT type FROM agents WHERE agent='$HTTP_USER_AGENT'");
	if (mysql_errno() != 0 ) { MySqlErrorGif ("select from agents"); }
	if(@mysql_numrows($result)!=1) {
	    if (ereg("^Mozilla/",$HTTP_USER_AGENT)) {
		if (ereg("MSIE",$HTTP_USER_AGENT)) {
		    if (!mysql_query("INSERT agents SET agent='$HTTP_USER_AGENT',open=NULL,type='IE'")) { 
                     MySqlErrorGif ("insert into agents"); } 
		} elseif (ereg("compatible",$HTTP_USER_AGENT)) {
	    	    if (!mysql_query("INSERT agents SET agent='$HTTP_USER_AGENT',open=NULL,type='Other'")) { 
                     MySqlErrorGif ("insert into agents"); }
		} else {
 	    if (!mysql_query("INSERT agents SET agent='$HTTP_USER_AGENT',open=NULL,type='Netscape'")) { 
                 MySqlErrorGif ("insert into agents"); }
		}	        
	    } else {
    		if (!mysql_query("INSERT agents SET agent='$HTTP_USER_AGENT',open=NULL,type='Other'")) { 
                 MySqlErrorGif ("insert into agents"); }
	    }
    	    $result = mysql_query("SELECT type FROM agents WHERE agent='$HTTP_USER_AGENT'");
	    if (mysql_errno() != 0 ) { 
             MySqlErrorGif ("select from agents"); }
	    if(@mysql_numrows($result)!=1) { 
              MySqlErrorGif ("Can\'t make second select from agents"); }
	}
	$b=mysql_result($result,0);
	if (!mysql_query("UPDATE agents SET counts=counts+1 WHERE agent='$HTTP_USER_AGENT'")) { 
           MySqlErrorGif ("UPDATE agent 1");}
	if (!mysql_query("UPDATE counters SET $b=$b+1 WHERE URL='$HTTP_REFERER'")) { 
           MySqlErrorGif ("UPDATE counters 2"); }
    }  
    $result = mysql_query("SELECT sum(counts),sum(IE),sum(Netscape),sum(Other) from counters left join eq on eq.id1=counters.id where eq.id=$id group by eq.id");
    if (mysql_errno() != 0 ) { 
       MySqlErrorGif ("select from counters"); }
    list($c,$i,$n,$o) = mysql_fetch_row($result);
    if ($i>$n) { $m=$i;} else {$m=$n;}
    if ($m<$o) { $m=$o;} 
    if ($m == 0) { $m=1; }
    $cs=sprintf("%05d",$c);
    $len=StrLen($cs)*9+3;
    $im=ImageCreate($len,22);
    $white = ImageColorAllocate($im,255,255,255);
    $black = ImageColorAllocate($im, 0,0,0);
    $red = ImageColorAllocate($im,255,0,0); 
    $blue = ImageColorAllocate($im,0,0,255);
    $limegreen = ImageColorAllocate($im,50,205,50);
    ImageFill($im,0,0,$white);
    ImageString($im,5,3,2,$cs,$black);
    ImageLine($im,0,16,$i*$len/$m,16,$limegreen);
    ImageLine($im,0,17,$i*$len/$m,17,$limegreen);
    ImageLine($im,0,18,$n*$len/$m,18,$blue);
    ImageLine($im,0,19,$n*$len/$m,19,$blue);
    ImageLine($im,0,20,$o*$len/$m,20,$red);
    ImageLine($im,0,21,$o*$len/$m,21,$red);
    ImageGif($im);
    ImageDestroy($im);
} else {
  ErrorGif("Usage: <img src=\"http://$HTTP_HOST$REQUEST_URI\">");
}
?>