xsuchy09/visitor

Visitor saves info about visitors of web page into DB and using cookies to identify them.

1.0.4 2020-01-28 15:55 UTC

This package is auto-updated.

Last update: 2024-11-29 03:03:14 UTC


README

PHP library to save info about visitors of web page into DB. PHP 7.1 is required.

Authors:

Overview

Visitor saves info about visitor of web page into db and uses cookie to identify same visitor. Lifetime of that cookie is configurable - default is 10 years. It is using UtmCookie to save UTMs too and Hashids to get unique hash for every single visitor (for example in JavaScript it is not safe to use directly ID from DB).

SQL to create DB table is included (visitor.sql - for PostgreSQL). Column "visits_count" is counted with trigger (new visit is counted if it is more than 24 hours from last visit). You can change it as you want.

Installation (via composer)

Get composer and add this in your requires section of the composer.json:

{
    "require": {
        "xsuchy09/visitor": "*"
    }
}

and then

composer install

Usage

Basic Example

$pdo = $PDO = new PDO(sprintf('pgsql:host=%s;port=%d;dbname=%s;user=%s;password=%s', 'localhost', 5432, 'db_name', 'username', 'password'));
$visitor = new Visitor($pdo, 'HashidsKey'); // it needs pdo instance in constructor and key for Hashids (use your own for your security). You can use others optionally params as you need.
$visitor->addVisit(); // set last visit of of visitor if he is identified or just create the new one with first visit

More examples can be found in the examples/ directory. Since version 1.0.4 you can set cookie params in constructor (path, domain, secure, httpOnly, sameSite) and some getters were added.