Active Visitor Information and Counter with PHP

Without further ado, I would like to give a short summary and share the code.
The information about a visitor to your site, namely:
- - Real IP ,
- - Masked IP,
- - Active Visitor Count,
- - Yesterday's Unique Total ,
- - Today's Unique Total,
- - Overall Unique Total ,
- - Browser Used,
- - Operating System
I will also be sharing a few functions that give us access to information like this; I hope you find them useful.
First, let's take a look at our database code
// For the Online Table CREATE TABLE `online` ( `ip` VARCHAR(50) NOT NULL, `time` VARCHAR(50) NOT NULL, `OS` VARCHAR(75) NOT NULL, `browser` VARCHAR(255) NOT NULL , `useragent` VARCHAR(255) NOT NULL ) COLLATE='utf8_turkish_ci' ENGINE=MyISAM ; // For the Ziyaret (Visit) Table CREATE TABLE `ziyaret` ( `id` INT(50) NOT NULL AUTO_INCREMENT, `ip` VARCHAR(75) NOT NULL, `tarih` VARCHAR(20) NOT NULL, `sondk` VARCHAR(20) NOT NULL, `OS` VARCHAR(75) NOT NULL, `browser` VARCHAR(255) NOT NULL, `useragent` VARCHAR(255) NOT NULL, PRIMARY KEY (`id`) ) COLLATE='utf8_turkish_ci' ENGINE=MyISAM AUTO_INCREMENT=1 ;
Now, the function we will use to detect the browser,
function browser_al($useragent = '')
{
if ($useragent == '')
{
$useragent = $_SERVER['HTTP_USER_AGENT'];
}
$browser_name = '';
$browsers = array();
$browsers[] = array('name' => 'AmigaVoyager', 'useragent' => 'AmigaVoyager');
$browsers[] = array('name' => 'Apple Generic', 'useragent' => 'CFNetwork');
$browsers[] = array('name' => 'Camino', 'useragent' => 'Camino');
$browsers[] = array('name' => 'Ensemble2', 'useragent' => 'Ensemble2');
$browsers[] = array('name' => 'Epiphany', 'useragent' => 'Epiphany');
$browsers[] = array('name' => 'Firefox', 'useragent' => 'Firefox');
$browsers[] = array('name' => 'Firefox', 'useragent' => 'Firefox/1.0');
$browsers[] = array('name' => 'Firefox', 'useragent' => 'Firefox/1.5');
$browsers[] = array('name' => 'Firefox', 'useragent' => 'BonEcho');
$browsers[] = array('name' => 'Firefox', 'useragent' => 'Firefox/2');
$browsers[] = array('name' => 'Firefox', 'useragent' => 'Firefox/3');
$browsers[] = array('name' => 'Firefox', 'useragent' => 'GranParadiso');
$browsers[] = array('name' => 'Firefox', 'useragent' => 'Phoenix');
$browsers[] = array('name' => 'Firefox', 'useragent' => 'Firebird');
$browsers[] = array('name' => 'Galeon', 'useragent' => 'Galeon');
$browsers[] = array('name' => 'Google Chrome', 'useragent' => 'chrome');
$browsers[] = array('name' => 'Google Chrome', 'useragent' => 'Chrome');
$browsers[] = array('name' => 'iCab', 'useragent' => 'iCab');
$browsers[] = array('name' => 'Iceweasel', 'useragent' => 'Iceweasel');
$browsers[] = array('name' => 'Internet Explorer', 'useragent' => 'MSIE');
$browsers[] = array('name' => 'Internet Explorer', 'useragent' => 'MSIE5.');
$browsers[] = array('name' => 'Internet Explorer', 'useragent' => 'MSIE6.');
$browsers[] = array('name' => 'Internet Explorer', 'useragent' => 'MSIE7.');
$browsers[] = array('name' => 'Internet Explorer', 'useragent' => 'MSIE8.');
$browsers[] = array('name' => 'Internet Explorer', 'useragent' => 'MSIE9.');
$browsers[] = array('name' => 'Internet Explorer', 'useragent' => 'MSIE10.');
$browsers[] = array('name' => 'Internet Explorer', 'useragent' => 'MSIE11.');
$browsers[] = array('name' => 'Internet Explorer', 'useragent' => 'MSIE.');
$browsers[] = array('name' => 'Internet Explorer Mobile', 'useragent' => 'IEMobile.');
$browsers[] = array('name' => 'K-Meleon', 'useragent' => 'K-Meleon');
$browsers[] = array('name' => 'Konqueror', 'useragent' => 'Konqueror');
$browsers[] = array('name' => 'Lynx', 'useragent' => 'Lynx');
$browsers[] = array('name' => 'Mozilla Minefield', 'useragent' => 'Minefield');
$browsers[] = array('name' => 'Netscape', 'useragent' => 'Netscape');
$browsers[] = array('name' => 'NewsCopier', 'useragent' => 'NewsCopier');
$browsers[] = array('name' => 'Opera', 'useragent' => 'Opera');
$browsers[] = array('name' => 'Pocket Internet Explorer', 'useragent' => 'MicrosoftPocketInternetExplorer');
$browsers[] = array('name' => 'Pocket Internet Explorer', 'useragent' => 'MSPIE');
$browsers[] = array('name' => 'Radianrss', 'useragent' => 'radianrss');
$browsers[] = array('name' => 'RssReader', 'useragent' => 'rssreader.com');
$browsers[] = array('name' => 'Apple Safari', 'useragent' => 'AppleSyndication');
$browsers[] = array('name' => 'Mobile Safari', 'useragent' => 'Mobile Safari');
$browsers[] = array('name' => 'SeaMonkey', 'useragent' => 'SeaMonkey');
$browsers[] = array('name' => 'Swiftweasel', 'useragent' => 'Swiftweasel');
$browsers[] = array('name' => 'Thunderbird', 'useragent' => 'Thunderbird');
$browsers[] = array('name' => 'Vienna', 'useragent' => 'Vienna');
foreach ($browsers as $browser)
{
if (stripos(strtolower($useragent), strtolower($browser['useragent'])) !== FALSE)
{
$browser_name = $browser['name'];
}
}
unset($browsers, $browser, $useragent);
if ($browser_name)
{
return $browser_name;
}
else
{
return false;
}
}
Right after that, the function we will use to detect the operating system,
function os_al($useragent1 = '')
{
if ($useragent1 == '')
{
$useragent1 = $_SERVER['HTTP_USER_AGENT'];
}
$os_name = '';
$oses = array();
$oses[] = array('name' => 'Windows 95', 'useragent1' => 'Windows 95');
$oses[] = array('name' => 'Windows 98', 'useragent1' => 'Windows 98');
$oses[] = array('name' => 'Windows NT', 'useragent1' => 'Windows NT');
$oses[] = array('name' => 'Windows 2000', 'useragent1' => 'Windows NT 5.0');
$oses[] = array('name' => 'Windows XP', 'useragent1' => 'Windows NT 5.1');
$oses[] = array('name' => 'Windows Vista', 'useragent1' => 'Windows NT 6.0');
$oses[] = array('name' => 'Windows 7', 'useragent1' => 'Windows NT 6.1');
$oses[] = array('name' => 'Windows 8', 'useragent1' => 'Windows NT 6.2');
$oses[] = array('name' => 'Windows 8.1', 'useragent1' => 'Windows NT 6.3');
$oses[] = array('name' => 'Windows Phone ', 'useragent1' => 'Windows Phone 8.0');
$oses[] = array('name' => 'Symbian ', 'useragent1' => 'SymbOS');
$oses[] = array('name' => '”Mac”', 'useragent1' => '”Mac”');
$oses[] = array('name' => '”Linux”', 'useragent1' => '”Linux”');
$oses[] = array('name' => 'Android', 'useragent1' => 'Android');
$oses[] = array('name' => 'Mac OS X', 'useragent1' => 'iPhone');
$oses[] = array('name' => 'Mac OS X', 'useragent1' => 'Mac OS X');
foreach ($oses as $os)
{
if (stripos(strtolower($useragent1), strtolower($os['useragent1'])) !== FALSE)
{
$os_name = $os['name'];
}
}
unset($oses, $os, $useragent1);
if ($os_name)
{
return $os_name;
}
else
{
return false;
}
}
Next up is detecting the user's real IP; the function I use is below, but you can also use the functions here.
function GetIP(){
if (!empty ($_SERVER['HTTP_CLIENT_IP']))
{
$ipp = $_SERVER['HTTP_CLIENT_IP'];
}
else if (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
{
$ipp = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
else if (!empty($_SERVER['REMOTE_ADDR']))
{
$ipp = $_SERVER['REMOTE_ADDR'];
}
return $ipp;
}
Now let's find the masked IP,
echo 'Masked IP :'.$_SERVER['REMOTE_ADDR'];
I assigned the functions to variables so that we can use them easily
$tarayıcı= browser_al(); $os = os_al(); $ip = GetIP();
With the functions out of the way, let's find the number of active people on our site,
@$past = time()-150;
mysql_query("DELETE FROM online WHERE time < $past");
@$result = mysql_query("SELECT time FROM online WHERE ip='$ip'");
@$time = time();
if($row = mysql_fetch_array($result)){
mysql_query("UPDATE online SET time='$time',ip='$ip' WHERE ip='$ip'");
}else{
mysql_query("INSERT INTO online (ip,time,browser,OS,useragent) VALUES ('$ip','$time','$tarayıcı','$os','$genel')") or die(mysql_error());
}
@$result = mysql_query("SELECT ip FROM online");
@$aktifkac = mysql_num_rows($result);
echo 'Active Visitors : ' .$aktifkac. '<br />';
Now let's find yesterday's visitor count,
@$baslat =date(Y."-".m."-".d);
@$year =substr($baslat, 0,4);
@$month =substr($baslat, 5, 2);
@$day =substr($baslat, 8, 2);
@$bitis =date("Y-m-d", mktime(0, 0, 0, $month, $day-1, $year));
@$sorgula = mysql_query("select tarih from ziyaret where tarih='$bitis'");
@$dunku = mysql_num_rows($sorgula);
echo 'Visitors Yesterday : '.$dunku.'<br />;
Let's find today's visitor count,
@$bugun = date("Y-m-d");
@$sorgu = mysql_query("select tarih from ziyaret where tarih='$bugun'");
@$bugunku = mysql_num_rows($sorgu);
echo 'Visitors Today : '.$bugunku.'<br />;
Now let's find the total number of unique visitors,
@$ipsi = GetIP();
@$tarih = date("Y-m-d ");
@$genel = $_SERVER['HTTP_USER_AGENT'];
@$tarayıcı= browser_al();
@$os = os_al();
@$sondk = date("H:i:s");
mysql_query("UPDATE ziyaret SET sondk='$sondk' WHERE tarih='$bugun' and ip='$ipsi'");
@$ipkontrol = mysql_query("select * from ziyaret where ip='$ipsi' order by id desc");
@$yaz = mysql_fetch_assoc($ipkontrol);
@$vip = $yaz['ip'];
@$vos = $yaz['OS'];
@$vtarayıcı =$yaz['browser'];
@$vgenel =$yaz['useragent'];
@$vtarih= $yaz['tarih'];
@$vsondk= $yaz['sondk'];
@$bak = mysql_num_rows($ipkontrol);
if($bak>0){ //if-
if($vtarih<$tarih){
$kayit_1 = mysql_query("insert into ziyaret (ip,OS,browser,useragent,tarih,sondk) values ('$ipsi','$os','$tarayıcı','$genel','$tarih','$sondk')");
}
}
else{
@$kayit_2 = mysql_query("insert into ziyaret (ip,OS,browser,useragent,tarih,sondk) values ('$ipsi','$os','$tarayıcı','$genel','$tarih','$sondk')");
}
@$toplamne = mysql_query("select * from ziyaret");
@$toplamziyaret = mysql_num_rows($toplamne);
echo 'Total Unique Visitors : '.$toplamziyaret.'<br />;
echo 'Masked IP :'.$_SERVER['REMOTE_ADDR']; echo 'Real IP :'. GetIP(); echo 'Browser Used :'.browser_al(); echo 'Operating System :'.os_al();
Well friends, there is one thing I want to point out: some variable definitions may differ, because I put this together from functions I have used in many projects; I hope it works without any problems.
Hoping you get to use it in great projects. Take care...
PHP,Maskeli IP,Gerçek IP,Tarayıcı Tespit,İşletim Sistemi Tespit,Tekil Ziyaretçi Bilgileri,Aktif Ziyaretçi Bilgileri,Ziyaretçi Sayacı