<?php 
//session_start();
require('visitors_connections.php');

// Script Online Users and Visitors - coursesweb.net/php-mysql/

//if(!isset($_SESSION)) session_start(); // start Session, if not already started

// get the user name if it is logged, or the visitors IP (and add the identifier)

//$vst_id = '-vst-'; // an identifier to know that it is a visitor, not logged user

//$uvon =  $_SERVER['SERVER_ADDR'];

$rgxvst = '/^([0-9\.]*)'. $vst_id. '/i'; // regexp to recognize the rows with visitors

$dt = time(); // current timestamp

$timeon = 120; // number of secconds to keep a user online

$nrvst = 0; // to store the number of visitors

//$nrusr = 0; // to store the number of usersrs

//$usron = ''; // to store the name of logged users

$visitor_hour = time();

date_default_timezone_set('Asia/Kolkata');

$visitor_ip = GetHostByName($_SERVER['REMOTE_ADDR']);

$visitor_browser = getBrowserType();

$visitor_minute = date("i");

$visitor_day = date("d");

$visitor_date = date("Y-m-d H:i:s");

$visitor_month = date("m");

$visitor_year = date("Y");

$visitor_refferer = $_SERVER['HTTP_REFERER'];

$visitorpage = selfURL();

$pathname = explode('index.php', $visitorpage);

$visitor_page = $pathname[0];

//echo $visitor_page;



$visitor_platform = getPlatform();

$visitor_device = getDevice();


//$visitor_id = 0;

//$sql_max_id = "select max(visitor_id) as visitor from tbl_visitors_2";

//$statement_max_id = $connect->prepare($sql_max_id);

//$statement_max_id->execute();

//$row_max_id = $statement_max_id->fetch();

//$visitor_id = $row_max_id['visitor'] + 1;

// Define and execute the Delete, Insert/Update, and Select queries

$sqldel = "DELETE FROM tbl_visitors_2 WHERE `visitor_time`<". ($dt - $timeon);

$sqliu = "INSERT INTO tbl_visitors_2 (visitor_ip,visitor_time, visitor_browser, visitor_hour, visitor_minute, visitor_date, visitor_day, visitor_month, visitor_year, visitor_refferer, visitor_page, visitor_platform ,visitor_device, category_id, status) VALUES ('$visitor_ip',$dt, '$visitor_browser', '$visitor_hour', '$visitor_minute', '$visitor_date', '$visitor_day', '$visitor_month', '$visitor_year', '$visitor_refferer', '$visitor_page' ,'$visitor_platform','$visitor_device', 2, 'ACTIVE') ON DUPLICATE KEY UPDATE `visitor_time`=$dt";

//echo $sqliu;
//exit;

//return;

//$sqlsel = "SELECT * FROM `userson`";

$statement = $connect->prepare($sqliu);

$statement->execute();

$statement = $connect->prepare($sqldel);

$statement->execute();
$_SESSION["visitor_page"][$visitor_ip] = $visitor_page;
$_SESSION["visitor_ip"][$visitor_page] = $visitor_ip;
$_SESSION["visitor_browser"][$visitor_page] = $visitor_browser;
$_SESSION["visitor_page"][$visitor_page] = $visitor_page;
$_SESSION["visitor_device"][$visitor_page] = $visitor_device;

// Execute each query

//if(!$conn->query($sqldel)) echo 'Error: '. $conn->error;

//if(!$conn->query($sqliu)) echo 'Error: '. $conn->error;

//$result = $conn->query($sqlsel)

//echo 1;

?>