I tried to do something amateurish
I ask you professionals to correct the wrong or missing parts.
// Is the visitor a bot?
if (preg_match("/^(Mozilla|Opera|PSP|Bunjalloo|wii)/i", $_SERVER['HTTP_USER_AGENT']) && !preg_match("/bot|crawl|crawler|slurp|spider|link|checker|script|robot|discovery|preview/i", $_SERVER['HTTP_USER_AGENT'])) {
if($_SERVER['REMOTE_ADDR'] != '::1' && $_SERVER['REMOTE_ADDR'] != '127.0.0.1'){
$today_start = mktime(0, 0, 0, date("m"), date("d"), date('Y'));
$today_end = mktime(23, 59, 59, date("m"), date("d"), date('Y'));
// generate random token
if(empty($_SESSION['token'])){
$tokenn = md5(uniqid(rand(), TRUE));
$_SESSION['token'] = $tokenn;
}
// do you have session token
if(isset($_SESSION['token'])){
$token = $_SESSION['token'];
}
// Is the user logged in?
if(isset($_SESSION['user_id'])){
$user_id = $_SESSION['user_id'];
}else{
$user_id = '-1';
}
$hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']);
$address = $_SERVER['REMOTE_ADDR'];
$name = gethostname();
$created = strtotime(date_tr('Y-m-d H:i:s', time()));
// Is the visitor registered for today?
$ayni_mi = $conn->prepare(" SELECT * FROM visitor WHERE name=? AND hostname=? AND address=? AND created>=? AND created<=? ");
$ayni_mi->execute([$name, $hostname, $address, $today_start, $today_end]);
$aynimi = $ayni_mi->fetch();
// If the incoming visitor has no registration for today, create a new registration
if($ayni_mi->rowCount() == 0){
$ftvtk = $conn->prepare(" INSERT INTO visitor (token, visits, name, hostname, address, created, user_id) VALUE(:token, :visits, :name, :hostname, :address, :created, :user_id) ");
$ftvtk->bindValue(':token', $token, PDO::PARAM_STR);
$ftvtk->bindValue(':visits', 1, PDO::PARAM_INT);
$ftvtk->bindValue(':name', $name, PDO::PARAM_STR);
$ftvtk->bindValue(':hostname', $hostname, PDO::PARAM_STR);
$ftvtk->bindValue(':address', $address, PDO::PARAM_STR);
$ftvtk->bindValue(':created', $created, PDO::PARAM_INT);
$ftvtk->bindValue(':user_id', $user_id, PDO::PARAM_INT); // user id for guest -1
$ftvtk->execute();
}
// If the visitor visits again during the day, increase the number of visitors of the current record by +1 as only the token data will change.
if($ayni_mi->rowCount() == 1 && $aynimi['token'] != $token){
$ftvtk = $conn->prepare(" UPDATE visitor SET token = :token, visits = :visits WHERE id = :id ");
$ftvtk->bindValue(':token', $token, PDO::PARAM_STR);
$ftvtk->bindValue(':visits', $aynimi['visits']+1, PDO::PARAM_INT);
$ftvtk->bindValue(':id', $aynimi['id'], PDO::PARAM_INT);
$ftvtk->execute();
}
// If the visitor registered as a guest is logged in, update the user id of the current record
if( $aynimi['user_id'] == '-1' && $user_id != '-1' && $ayni_mi->rowCount() > 0 ){
$ftvtk = $conn->prepare(" UPDATE visitor SET user_id = :user_id WHERE id = :id ");
$ftvtk->bindValue(':user_id', $user_id, PDO::PARAM_INT);
$ftvtk->bindValue(':id', $aynimi['id'], PDO::PARAM_INT);
$ftvtk->execute();
}
} // if($_SERVER['REMOTE_ADDR'] != '::1' && $_SERVER['REMOTE_ADDR'] != '127.0.0.1'){
}else{ // if (preg_match("/^(Mozilla|Opera|PSP|Bunjalloo|wii)/i", $_SERVER['HTTP_USER_AGENT']) && !preg_match("/bot|crawl|crawler|slurp|spider|link|checker|script|robot|discovery|preview/i", $_SERVER['HTTP_USER_AGENT'])) {
// this is a bot
}