Visitor counter

Hello,

What is the basic logic of the visitor counter?
Registered member and guest visitor distinction
When a registered member enters the site, he is registered as a guest. Then when he logs in he is registered as a registered member. How should this distinction be made?
I don’t want the counter to increment every time the page is refreshed. But I want the counter to increment when logged in at different times during the day
Now it occurred to me. I guess it can be checked by creating a session with a random key for each session.

Anytime you do the ‘login’ routine… log the user IP/username…etc…

If you want to count ‘visits’ outside of a login process (such as still logged in but comes back several hours later)

1.) Probably shouldn’t allow that length of still being authorized
2.) You could use a cookie or session value to check the time of last login/visitor timestamp…

I created a shape in my head, I’m trying to make it, I’ll ask for your comment when it’s finished
Thank you

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
}
Sponsor our Newsletter | Privacy Policy | Terms of Service