How to Store Visitor Activity Log in the MySql Database Using PHP

Store Visitor Activity Log in the MySql Database Using PHP

In this article, we will learn about the website visitor activity tracking script. User activity on the online application / Website can be tracked using the Visitor Log. You’ll collect the visitor’s IP address, referrer, browser, and other information when they visit the website and store it in a database log.

The country, latitude, and longitude of the visitor can also be stored in the database for geolocation tracking. Along with the visitor’s information, the logging system frequently stores and tracks the website’s interior access information. The $_SERVER variable in PHP will be used to achieve the majority of the knowledge. To get the visitors’ geolocation data, you’ll use a third-party API.

Are you want to get implementation help, or modify or extend the functionality of this script?

A Tutorialswebsite Expert can do it for you.

Using PHP and MySQL, we’ll show you how to collect visitor information (IP, referrer, browser, geolocation, and so on) and store logs in a database. We’ll use PHP to log the visitor’s activity in the MySQL database.

Store Visitor Activity Log in the MySql Database

Step-1: Create Database Tablel

To store the user activity information, a table should be made in the database. The following SQL query creates a visitor_activity_logs table with some essential fields in the MySQL database.

Step-2: Create Database Configuration (dbconfig.php)

The dbconfig.php file is used to connect and select the database. Specify the database hostname ($dbHost),DB username ($dbUsername), DB password ($dbPassword), and DB name ($dbName) as per your MySQL database credentials.

Step-3: Create Visitor Activity Log file(user_activity_log.php)

The most important part is gathering information from your visitor browser. PHP has a global variable called $_SERVER that contains several environment variables, including visitor information.

Simply execute the following code to collect all of the information you require:

In the above code:

Current Page URL – HTTPS, SERVER_PORT, HTTP_HOST, REQUEST_URI, and QUERY_STRING indices are used to get the current page URL.

Referrer URL – HTTP_REFERER key is used. The page URL that referred the user-agent to the current page.

IP Address – REMOTE_ADDR key is used to get the visitor’s IP address.

Browser Info – HTTP_USER_AGENT key is used to get the user agent details. The browser details with the current request header.

Step-4: Store Activity Logs in Database

To store the visitor activity log in the database, we have to create a webpage named index.php and Include the Log script (user_activity_log.php) in the web page.

index.php file

Also Read: How to Get Current Visitor Location using HTML5 Geolocation API

Are you want to get implementation help, or modify or extend the functionality of this script?

A Tutorialswebsite Expert can do it for you.

Wrapping Words

I hope you found this guide helpful and learned How to Store the Visitor Activity Log in the MySql Database Using PHP. If you did, please consider sharing this post on social media and Keep learning!. You can also extend the functionality as per your requirement.

Related posts