PHP PROJECT

README.md

Count user visits and display in a image widget

This class count user visits and display in a image widget. It can record the accesses of site visitors in a MySQL database. The class can also generate an image for a widget that displays the number of visitors of the site pages. The image widget details like font color, font style, font, background color can be configured.

Files


NameTypeSizeLast Modified
HCW_class.php phpfile icon 6289 31 January 2017 01:34:20
HCW_widget.php phpfile icon 384 14 January 2016 09:51:20
example.php phpfile icon 337 14 January 2016 09:51:20
HCW_example.php phpfile icon 337 14 January 2016 09:51:20
HCW_SQL.txt textfile icon 615 14 January 2016 09:51:20
hc directory icon 4096 31 March 2017 08:37:26
HCW_config.php phpfile icon 314 14 January 2016 09:51:20
template directory icon 4096 31 March 2017 08:37:26

Source

  1. <?
  2. class hitcounterwidget
  3. {
  4. var $link;
  5. var $debug=true;
  6. var $iid;
  7. var $ar=array();
  8. var $content;
  9.     public function __construct($host$user$pass$db
  10.     {
  11.     $this->link mysqli_connect($host$user$pass$db);
  12.         if (!$this->link && $debug) {
  13.         echo "Error: Unable to connect to MySQL." PHP_EOL;
  14.         echo "Debugging errno: " mysqli_connect_errno() . PHP_EOL;
  15.         echo "Debugging error: " mysqli_connect_error() . PHP_EOL;
  16.         exit;
  17.     }    
  18.     $this->ar=array('width'=>'5','ffstyle'=>'3','fcolor'=>'000000''bgcolor'=>'1FFF4B''fsize'=>'20''bcolor'=>'FFD13B''bwidth'=>'3''cstart'=>'00001''ctype'=>'0','md'=>$this->uid());
  19.     }
  20.     
  21.     public function __destruct()
  22.     {
  23.     mysqli_close($this->link);
  24.     }
  25.     
  26.     public function hit($md)
  27.     {
  28.     $this->link->query("UPDATE `hcw_hitcounter`  SET `cstart` = `cstart` + 1 WHERE `md`='".$md."' LIMIT 1");
  29.     }
  30.     
  31.     public function render_temp($file$data = array()) {
  32.         if (file_exists($file)) {
  33.             extract($data);
  34.             ob_start();
  35.             require($file);
  36.             $out ob_get_contents();
  37.             ob_end_clean();
  38.             return $out;
  39.         } else {
  40.         return false;
  41.         }
  42.     }
  43.     
  44.     public function findID($md){
  45.     $sql "SELECT * FROM `hcw_hitcounter` WHERE  `md` = '".$md."' LIMIT 1";
  46.     $q $this->link->query($sql);
  47.     if($q->num_rows 0){
  48.             return $q->fetch_assoc();
  49.         }
  50.     else
  51.     return false;    
  52.     }    
  53.     
  54.     public function findIDd($md)
  55.     {
  56.     $sql "SELECT * FROM `hcw_hitcounter` WHERE  `md` = '".$md."' LIMIT 1";
  57.     $query $this->link->query($sql);
  58.         if ($query->num_rows 0)
  59.         return mysqli_fetch_object($query);
  60.         else
  61.         return false;
  62.     }
  63.     
  64.     
  65.     public function insert($data){
  66.     
  67.     if(!is_array($data))
  68.     return false;
  69.     
  70.         $cols implode('`, `'array_keys($data));
  71.             foreach (array_values($data) as $value)
  72.             {
  73.             
  74.             isset($vals) ? $vals .= ',' $vals '';
  75.             $vals .= '\''.$this->link->real_escape_string(str_replace("#","",$value)).'\'';
  76.             }
  77.         $this->link->query('INSERT INTO `hcw_hitcounter` (`'.$cols.'`) VALUES ('.$vals.')');
  78.         
  79.         }
  80.     
  81.     public function update($data,$where){
  82.     if(!is_array($data))
  83.     return false;    
  84.     
  85.             foreach ($data as $k=>$v)
  86.             {
  87.             isset($vals) ? $vals .= ',' $vals '';
  88.             $vals .= '`'.$k.'` = \''.$this->link->real_escape_string(str_replace("#","",$v)).'\'';
  89.             }    
  90.         $this->link->query('UPDATE `hcw_hitcounter` SET '.$vals.' WHERE '.$where.'');
  91.         
  92.         }
  93.         
  94.         
  95.     public function uid() {
  96.         return sprintf'%04x-%04x-%04x-%04x',mt_rand00x0fff ) | 0x4000,mt_rand00x3fff ) | 0x8000,mt_rand00xffff ), mt_rand00xffff ));
  97.     }
  98.     
  99.     public function transfer($post)
  100.     {
  101.     if(!is_array($post))
  102.         return false;
  103.         foreach($this->ar as $k=>$v){
  104.         if(isset($post[$k]))
  105.         $this->ar[$k]=$post[$k];
  106.         }
  107.     $this->ar['stat'] = 1;    
  108.     if($this->ar['fsize'] < || $this->ar['fsize'] > 40
  109.     $this->ar['fsize'] = 16;
  110.         if(strlen($this->ar['cstart']) < || strlen($this->ar['cstart']) > 9
  111.         $this->ar['cstart'] = 5;
  112.             
  113.         if($this->findID($this->ar['md'])){
  114.         $this->update($this->ar,"`md` = '".$this->ar['md']."'");
  115.             if(is_file("./template/HCW_cutpaste.php"))
  116.                 $this->ar['mc'] = $this->render_temp("./template/HCW_cutpaste.php",array("url"=>url,"fullurl"=>fullurl,"md"=>$this->ar['md']));    
  117.         }
  118.         else{
  119.         $this->insert($this->ar);
  120.         if(is_file("./template/HCW_cutpaste.php"))
  121.                 $this->ar['mc'] = $this->render_temp("./template/HCW_cutpaste.php",array("url"=>url,"fullurl"=>fullurl,"md"=>$this->ar['md']));    
  122.         }
  123.     }
  124.     
  125.     public function hex2rgb($hex) {
  126.        $hex str_replace("#"""$hex);
  127.        if(strlen($hex) == 3) {
  128.           $r hexdec(substr($hex,0,1).substr($hex,0,1));
  129.           $g hexdec(substr($hex,1,1).substr($hex,1,1));
  130.           $b hexdec(substr($hex,2,1).substr($hex,2,1));
  131.        } 
  132.        elseif(strlen($hex) == 6) {
  133.           $r hexdec(substr($hex,0,2));
  134.           $g hexdec(substr($hex,2,2));
  135.           $b hexdec(substr($hex,4,2));
  136.        }
  137.        else
  138.        {
  139.        $r=0;
  140.        $g=0;
  141.        $b=0;
  142.        }
  143.        $rgb = array("red"=>$r"green"=>$g"blue"=>$b);
  144.        return $rgb;
  145.     }
  146.     public function rgb2hex($rgb) {
  147.        $hex "#";
  148.        $hex .= str_pad(dechex($rgb[0]), 2"0"STR_PAD_LEFT);
  149.        $hex .= str_pad(dechex($rgb[1]), 2"0"STR_PAD_LEFT);
  150.        $hex .= str_pad(dechex($rgb[2]), 2"0"STR_PAD_LEFT);
  151.        return $hex;
  152.     }
  153.     
  154.     public function counterOUTPUT($ar)
  155.     {
  156.     $bcolor $this->hex2rgb($ar['bcolor']);
  157.     $bgcolor $this->hex2rgb($ar['bgcolor']);
  158.     $fcolor $this->hex2rgb($ar['fcolor']);
  159.     if(is_fileTTF ."/".$ar['ffstyle']))
  160.     $fontname TTF "/"$ar['ffstyle'];
  161.     else
  162.     exit;
  163.     
  164.     if(strlen($ar['cstart']) < || strlen($ar['cstart']) > 9
  165.     $ar['cstart'] = 1;
  166.     $text $ar['cstart'];
  167.     if($ar['fsize'] < || $ar['fsize'] > 40
  168.     $ar['fsize'] = 16;
  169.     $font $ar['fsize'];
  170.     $text=sprintf("%'.0".$ar['width']."d"$ar['cstart']);
  171.     $text str_pad($text$ar['width'], "0"STR_PAD_LEFT);
  172.     $_bx imageTTFBbox($font,0,$fontname,$text);
  173.     $textwidth abs($_bx[4] - $_bx[0]);
  174.     $textheight abs($_bx[5] - $_bx[1]);
  175.     $resh = (int)($textheight 2);
  176.     $resw = (int)($textwidth 5);
  177.     $imagewidth $textwidth+$resw+$ar['bwidth'];
  178.     $imageheight $textheight+$resw;
  179.     $xcord = ($imagewidth/2)-($textwidth/2);
  180.     $ycord = ($imageheight/2)+($textheight/2);
  181.     $im imagecreate$imagewidth$imageheight );
  182.     $bg ImageColorAllocate($im,$bgcolor['red'], $bgcolor['green'], $bgcolor['blue']);
  183.     imagesetthickness($im$ar['bwidth']);
  184.     $border ImageColorAllocate($im$bcolor['red'], $bcolor['green'], $bcolor['blue']);
  185.     ImageRectangle($im00$imagewidth 1$imageheight 1$border);
  186.     $textcolor ImageColorAllocate($im$fcolor['red'], $fcolor['green'], $fcolor['blue']);
  187.     imagettftext($im$font0$xcord$ycord$textcolor$fontname,$text);
  188.     imagepng($im);
  189.     imagedestroy($im);
  190.     }
  191.     
  192.     public function findTTF(){
  193.     if( !is_dirTTF ) ) {
  194.         die ( "Invalid directory" );
  195.     }
  196.     
  197.         foreach(glob(TTF "/*.ttf") as $key => $file) {
  198.         $fname    basename($file);
  199.         $ffname preg_replace('/\\.[^.\\s]{3,4}$/'''$fname);
  200.             if(!isset($FILE_LIST))
  201.             $FILE_LIST '';    
  202.                 else            
  203.                 $FILE_LIST .= "<option value='$fname'>$ffname</option>\n";
  204.         }      
  205.         
  206.             if(!empty($FILE_LIST))
  207.                 return $FILE_LIST;
  208.              else 
  209.                 die( "No FONT files found!\n\n" );
  210.     }
  211. }

Demo

You can find a demo available here: Hit Counter

Requirment

  • mySql
  • PHP >= 5.4
  • Apache Server

Contribute

  • Vishv Sahdev

Example

  1. <?
  2. require("HCW_class.php");
  3. require("HCW_config.php");
  4.     if(isset($_POST['ok']) && $_POST['ok'] == "ok"){
  5.     $hcw->transfer($_POST);
  6.     }
  7.     else
  8.     $hcw->ar['mc']='';
  9.     
  10.     $hcw->ar['font']=$hcw->findTTF();
  11.     
  12.     if(is_file("./template/HCW_template.php")){
  13.             echo $hcw->render_temp("./template/HCW_template.php",$hcw->ar);
  14.     }
  15.     

>>

Download