Here goes a sample code.
The db interaction is throurh db abstraction layer but easily can be
translated to used directly mysql_* functions.
<?php
    include 'db.lib.php';
include 'webstats.lib.php';
class ClientBrowser{//-=-=-=-=-=--==-=-=-=--==-=-=-=-=-=-=-=-=-=-=-=-class
ClientBrowser-=-=-==-=-
  var $browser='unknown';
  var $mozilla='';
  var $major='0.0';
  var $minor='0.0';
  var $build='';
  var $platform='';
  var $windowses=array(
       'win95'=>'windows 95',
       'windows 95'=>'windows 95',
       'win98'=>'windows 98',
       'windows 98'=>'windows 98',
       'winnt'=>'windows nt 4.0',
       'windows nt'=>'windows nt 4.0',
       'windows'=>'windows 95',
       'windows 3.1'=>'windows 3.1',
       'win16' => 'windows 3.1',
       'win32' => 'windows 95',
       'windows'=> 'win-unknown',
       'windows nt 4.0'=>'windows nt 4.0',
       'windows nt 5.0'=>'windows 2000',
       'windows 2000' => 'windows 2000',
       'win2k' => 'windows 2000',
       'windows millennium'=>'windows me',
       'windows ce' => 'windows ce',
       'windows ce 2.0' => 'windows ce 2.0',
       'windows ce 3.0' => 'windows ce 3.0'
      );

  function ClientBrowser($http_agent='none'){
    if ($http_agent=='none') $http_agent=$GLOBALS['HTTP_USER_AGENT'];
    if($cap = $this-> _client_is_MSIE($http_agent)){
  $this->browser='MSIE';
 }elseif($cap = $this->_client_is_MSPIE($http_agent)){
  $this->browser='MSPIE';
 }elseif($cap = $this->_client_is_Opera($http_agent)){
  $this->browser='OPERA';
 }elseif($cap = $this->_client_is_NS($http_agent)){
  $this->browser='NAVIGATOR';
 }
    if ($this->browser!='unknown'){
      $this->mozilla=$cap['mozilla'];
      $this->major=$cap['major'];
      $this->minor=$cap['minor'];
      $this->build=$cap['build'];
      $this->platform=$cap['platform'];
    }
  }

  function _client_is_MSIE($s){
   $to_return = FALSE;
   if(preg_match('~^Microsoft Internet Explorer/([^ ]+) \\(([^)]+)\\)~i',
$s, $a)){
    $to_return = array('mozilla' => '', 'major' => '1', 'minor' => '0',
'build' => $a[1]);
    if (strpos(' '.strtolower($a[2]),'win')){
     $to_return['platform'] = isset($this->windowses[strtolower($a[2])]) ?
$this->windowses[strtolower($a[2])]:'win-unknown';
    }else{
   $to_return['platform'] = strtolower($a[2]);
    }
    return $to_return;
   }
   if(preg_match('~^Mozilla/([^ ]+) \\(compatible;.* MSIE
([^.]+)\\.([\d.]+)([^; )]*).*?
((Win|Windows|Mac_PowerPC|Mac_68000|Mac_68K|Mac_PPC|OS/2|SUNOS|sunos 4|sunos
5|i86|irix|irix 6|irix
5|hp-ux|aix|aix1|aix2|aix3|aix4|linux|unix_system_v|ncr|reliantunix|bsd)[^);
]*)~i', $s, $a)){
    $to_return = array('mozilla' => $a[1], 'major' => $a[2], 'minor' =>
$a[3], 'build' => $a[4]);
    if (strpos(' '.strtolower($a[5]),'win')){
     $to_return['platform'] = isset($this->windowses[strtolower($a[5])]) ?
$this->windowses[strtolower($a[5])]:'win-unknown';
    }else{
   $to_return['platform'] = strtolower($a[5]);
    }
    return $to_return;
   }
   if(preg_match('~^Mozilla/([^ ]+) \\(compatible;.* MSIE
([^.]+)\\.([\d.]+)([^; )]*)~i', $s, $a)){
    $to_return = array('mozilla' => $a[1], 'major' => $a[2], 'minor' =>
$a[3], 'build' => $a[4], 'platform' => '');
    return $to_return;
   }
   return $to_return;
  }

  function _client_is_MSPIE($s){
   $to_return = FALSE;
   if(preg_match('~^Microsoft Pocket Internet
Explorer/([^.]+)\\.([\d.]+)(.*)~i', $s, $a)){
    $to_return = array('mozilla' => '', 'major' => $a[1], 'minor' => $a[2],
'build' => $a[3], 'platform' => 'windows ce');
    return $to_return;
   }
   if(preg_match('~^Mozilla/([^ ]+) \\(compatible;.* MSPIE
([^.]+)\\.([\d.]+)([^; )]*).*?
((Win|Windows|Mac_PowerPC|Mac_68000|Mac_68K|Mac_PPC|OS/2|SUNOS|sunos 4|sunos
5|i86|irix|irix 6|irix
5|hp-ux|aix|aix1|aix2|aix3|aix4|linux|unix_system_v|ncr|reliantunix|bsd)[^);
]*)~i', $s, $a)){
    $to_return = array('mozilla' => $a[1], 'major' => $a[2], 'minor' =>
$a[3], 'build' => $a[4]);
    if (strpos(' '.strtolower($a[5]),'win')){
     $to_return['platform'] = isset($this->windowses[strtolower($a[5])]) ?
$this->windowses[strtolower($a[5])]:'wince-unknown';
    }else{
   $to_return['platform'] = strtolower($a[5]);
    }
    return $to_return;
   }
   return $to_return;
  }

  function _client_is_NS($s){
   $to_return = FALSE;
   if(preg_match('~^Mozilla/([^.]+)\\.([\d.]+)([^ ]*).*?\\(([^;)]+)~i', $s,
$a)){
    if(strtolower($a[4]) == 'compatible') return FALSE;
    if(strtolower($a[4]) == 'macintosh'){
     if(strpos($s, '68K')) $a[4] = 'Mac_68K';
     elseif(strpos($s, 'PPC')) $a[4] = 'Mac_PPC';
    }
    $to_return = array('mozilla' => $a[1] . "." . $a[2] . $a[3], 'major' =>
$a[1], 'minor' => $a[2], 'build' => $a[3]);
    if (strpos(' '.strtolower($a[4]),'win')){  // heading space added
because specific return value of strpos()
     $to_return['platform'] = isset($this->windowses[strtolower($a[4])]) ?
$this->windowses[strtolower($a[4])]:'win-unknown';
    }else{
   $to_return['platform'] = strtolower($a[4]);
    }
    return $to_return;
   }
   return $to_return;
  }

  function _client_is_Opera($s){
   $to_return = FALSE;
   if(preg_match('~Mozilla/([^ ]+)
\\(((Win|Windows|Mac_PowerPC|Mac_68000|Mac_68K|Mac_PPC)[^);]*).*Opera
([^.]+)\\.([\d.]+)([^; )]*)~i', $s, $a)){
    $to_return = array('mozilla' => $a[1], 'major' => $a[4], 'minor' =>
$a[5], 'build' => $a[6]);
    if (strpos(' '.strtolower($a[2]),'win')){
     $to_return['platform'] = isset($this->windowses[strtolower($a[2])]) ?
$this->windowses[strtolower($a[2])]:'win-unknown';
    }else{
   $to_return['platform'] = strtolower($a[2]);
    }
    return $to_return;
   }
   if(preg_match('~Opera/([^.]+)\\.([\d.]+)([^; )]*)
\\(((Win|Windows|Mac_PowerPC|Mac_68000|Mac_68K|Mac_PPC|OS/2|SUNOS|sunos
4|sunos 5|i86|irix|irix 6|irix
5|hp-ux|aix|aix1|aix2|aix3|aix4|linux|unix_system_v|ncr|reliantunix|bsd)[^);
]*)~i', $s, $a)){
    $to_return = array('mozilla' => '', 'major' => $a[1], 'minor' => $a[2],
'build' => $a[3]);
    if (strpos(' '.strtolower($a[5]),'win')){
     $to_return['platform'] = isset($this->windowses[strtolower($a[4])]) ?
$this->windowses[strtolower($a[4])]:'win-unknown';
    }else{
   $to_return['platform'] = strtolower($a[4]);
    }
    return $to_return;
   }
   return $to_return;
  }
}
//  -=-=-=-=-=--==-=-=-=--==-=-=-=-=-=-=-=-=-=-=-=-class
ClientBrowser-=-=-==-=-

// -=--==-=-=-=-=-=--==-=-=-=-=--==-=-MAIN--==-=-=--==-=--==-=-=-=-//
 $client = new ClientBrowser($HTTP_USER_AGENT);
 $page_name = $HTTP_GET_VARS['page'];
 $screen_width = $HTTP_GET_VARS['scr_w']+0;
 $screen_height = $HTTP_GET_VARS['scr_h']+0;
 $screen_color = $HTTP_GET_VARS['scr_c']+0;
 if
(preg_match('/^([0-9\.]+)$/',$screen_width.''.$screen_height.''.$screen_colo
r)==0){
  exit;
 }
 if ($WEBSTATS_pages[$page_name]['collecting']==1){ // You must have the
$WEBSTATS_pages array in  the included file webstats.lib.php  . Collecting
shows if we collect info about this page
 db_connect();
  $res = db_exec('SELECT COUNT(*) FROM webPages WHERE
webpage_name="'.$page_name.'";');
  list($is_there)=db_fetch_array($res);
  if ($is_there==0){
   db_free_result($res);
   db_exec('INSERT INTO webPages  VALUES("","'.$page_name.'");');
  }else{
   db_free_result($res);
  }


  $res = db_exec('SELECT webpage_id FROM webPages WHERE
webpage_name="'.$page_name.'";');
  list($webpage_id) = db_fetch_array($res);
  db_free_result($res);


  $sql_to_exec = "SELECT COUNT(*) FROM webStats WHERE webpage_id=$webpage_id
AND os_name='".$client->platform.
       "' AND browser='".$client->browser."
".$client->major.".".$client->minor.
       "' AND screen_w=$screen_width AND screen_h=$screen_height AND
screen_c=$screen_color;";
  $res = db_exec($sql_to_exec);
  $is_there = 0;
  list($is_there) = db_fetch_array($res);
  db_free_result($res);


  if ($is_there==1){
   $update_sql = "UPDATE webStats set counter=counter+1 WHERE
webpage_id=$webpage_id AND os_name='".$client->platform.
       "' AND browser='".$client->browser."
".$client->major.".".$client->minor.
       "' AND screen_w=$screen_width AND screen_h=$screen_height AND
screen_c=$screen_color;";
    db_exec($update_sql);
  }else{
   $insert_sql = "INSERT INTO webStats (webpage_id, os_name, browser,
screen_w, screen_h, screen_c, counter) ".
         "VALUES($webpage_id,'".$client->platform."','".$client->browser."
".$client->major.".".$client->minor."',".
         "$screen_width, $screen_height, $screen_color, 1);";
   db_exec($insert_sql);
  }
 }
?>

Andrey Hristov
IcyGEN Corporation
http://www.icygen.com
99%

----- Original Message -----
From: "Gert Mellak" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Wednesday, August 22, 2001 6:49 PM
Subject: [PHP] php-counter


> hi!
>
> do you know an easy-to-use-php-counter, which uses mysql to store the
data?
>
> gert
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> To contact the list administrators, e-mail: [EMAIL PROTECTED]
>
>


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to