October 5, 2022

In this lesson, I will show you how to create your own log file of each page, the language of the user and the exact date and time of the visit.

A log file is a way to track the user experience,the language of the visitors and helps you know more about the topics your visitors want the most.

How to create a PHP file ?

You can create a php file using any text editor app on your mobile or with a sample notepad on your computer, and name it with a php extension.

A php extension is any file withe a file name ending with .php.

Save the file and choose the all files options on your computer then upload it to your website using any FTP application.

You can upload the PHP file on the FTP section on your Cpanel by a sample drag and drop option.

Here is a list of PHP & HTML editors that can help you edit and create php files easily :

You will not need any personal data like IP address for your business, useful data is what you will need like language, referring page , preferred time of visit…

PHP will help you track the data that will help you grow your business and give the best user experience for your customers.

Php offers you alot of variables that helpsĀ  you get the data that suits you :

The language variables

$_SERVER["HTTP_ACCEPT_LANGUAGE"];

It shows you multiple informations about the language of the browser

So we will narrow it down the main language of the user.

The code will be like :

$lang=explode(",",$_SERVER["HTTP_ACCEPT_LANGUAGE"])[0];

The URL variables

$current=$_SERVER[‘REQUEST_URI’];

This variable will return the url of page you are opening right now.

Adding this variable to your log file will show you the most visited websites.

language : en-US
current URL :/the-easiest-way-to-create-a-log-file-for-your-website/
<?php 
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2); 
$current=$_SERVER['REQUEST_URI']; 
$time = date("Y-m-d H:i:s"); 
$myfile = fopen("log.txt", "a") or die("Unable to open file!"); 
$txt = "$time>".$lang.">".$current."\n";
fwrite($myfile, $txt);
fclose($myfile);
?>


The exact url if the log file is :

https://promoteyourproject.com/log.txt

Leave a Reply

Your email address will not be published.