How to Storing Visitor Log in the Database with PHP and MySQL

How to Storing Visitor Log in the Database with PHP and MySQL

How to Storing Visitor Log in the Database with PHP and MySQL

0 Sales


The Visitor Log is useful for tracking user activities on the web - based application. Whenever users enter the website, you may gather the user's Internet address, browser, referrer, as well as other information and store it in the database as a log.

Including the visitor's information, the logging system can retain and track the website's internal access information. The $_SERVER variable in PHP will be used to obtain the majority of the information. You may obtain visitor geolocation data by using a third-party API. In this article, we'll teach you how to use MySQL and PHP to retrieve user data (referrer, IP, geolocation, browser, and so on) and save logs in a server.


In this demo Website Visitor Tracking script, i will use PHP to log the user's behaviour in the MySQL database.


- Using the PHP $_SERVER variable, you may obtain the visitor's Internet address, referrer URL, current page URL, and browser information.

- PHP and MySQL are used to manage visitor logs in the system.


Create Database Table


A table in the database is required to hold the users logs. The SQL below creates a table called visitor logs in the MySQL database.

CREATE TABLE `visitor_logs` (
 `page_url` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
 `referrer_url` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
 `user_ip_address` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
 `user_agent` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
 `created` datetime NOT NULL DEFAULT current_timestamp(),
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;


Database Configuration (dbConfig.php)


The dbConfig.php tool is being used to configure PHP and MySQL to the database. As per your database credentials, enter the database host ($dbHost), username ($dbUsername), password ($dbPassword), and name ($dbName).

// Database configuration 
$dbHost     "localhost"; 
$dbUsername "root"; 
$dbPassword "root"; 
$dbName     "codexworld"; 
// Create database connection 
$db = new mysqli($dbHost$dbUsername$dbPassword$dbName); 
// Check connection 
if ($db->connect_error) { 
    die("Connection failed: " $db->connect_error); 


Visitor Log (log.php)


The majority of server and browser information is collected via $_SERVER, a big international object in PHP.


- Current Page URL - To obtain the current page URL, utilise the HTTPS, SERVER PORT, HTTP HOST, REQUEST URI, and QUERY STRING indices.

- Referrer URL – The HTTP REFERER parameter is used to specify the URL of the referrer. The URL of the page that directed the consumer to the webpage.

- IP Address – The REMOTE ADDR value is used to obtain the visitor's IP address.

- Browser Info — The HTTP USER AGENT value is used to obtain information about the user agent. The browser provides information about the current request header.

Using PHP and MySQL, insert visitor logs into the database.

- To enter log data into the MySQL database, use the prepared statement (prepare, bind, and execute).


// Include the database configuration file 
include_once 'dbConfig.php'; 
// Get current page URL 
$protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ? "https://" "http://"; 
// Get server related info 
$user_ip_address $_SERVER['REMOTE_ADDR']; 
$referrer_url = !empty($_SERVER['HTTP_REFERER'])?$_SERVER['HTTP_REFERER']:'/'; 
$user_agent $_SERVER['HTTP_USER_AGENT']; 
// Insert visitor log into database 
$sql "INSERT INTO visitor_logs (page_url, referrer_url, user_ip_address, user_agent, created) VALUES (?,?,?,?,NOW())"; 
$stmt $db->prepare($sql); 
$insert $stmt->execute(); 


Store Logs in Database


In order to save visitor logs on the database, also include Log script (log.php) in the web page.

// Include visitor log script 
include_once 'log.php'; 


Geolocation Tracking


If you wish to obtain data about the visitor's nationality and geolocation, you must utilise a third-party API. You may use PHP to acquire the geolocation from an IP address by utilising the IP Geolocation API.


The following data could be kept in the database relying on the geolocation data supplied by the API.


- Country Code — The country's short code.

- Country Name - The country's name.

- Latitude and longitude - The IP address's location.




This record tool will periodically retrieve visitor information and save logs in the database. You simply need to put this script on the web page that will keep the visitor's activity log. According to our sample code, the most important information is saved in the database log using PHP and MySQL. However, you may add any other information to the database as needed.


You can use it for personal or commercial projects. You can't resell it partially or in this form.


Create Date : Jan 18, 2022

Updated Date : Jan 23, 2022


Comments : 0

Downloads : 0