Hi, good peoples after a long time I am back with a new tutorial. This tutorial is basically created for those people who are not strong in PHP and maybe they have a little bit of knowledge of PHP. Today I am going to show how to track Visitor IP, Visiting Time and Visited Page using PHP. Before we start we want to go back because I have already written a tutorial to User Real IP Address In PHP so we know how to get user IP address when they visit a webpage. Now we are going to track them and save them. Before starting we know the basic example of the code how can we use that and why we use that. For clear answer if you are a CPA marketer and if you have a campaign and user have to submit IP address if the visited page then you have to validate that. So you need to list their IP address. This is a simple example, there is a lot of ways to use this. Better you know why you reading this article ? I have talked too much, let's start the main thing ?
Ok, we will save visitor information in a specific directory for an example we have crossed visitors as the directory name. This is is the php code to create the directory if directory not exists.
$dir = 'visitors';
if(!is_dir($dir)){
mkdir($dir,0777);
}
Here we take a variable $dir for the directory name and we have checked the directory is exists or not by using php built-in function is_dir then we created the directory by using another built-in function mkdir(direcorty_name,directory_permission); we used 0777 permission for the directory.
Now we are going to save visitor information using this code-
<?php
$dir = 'visitors';
if(!is_dir($dir)){ mkdir($dir,0777);
}
$file = date('Y-m-d').'.txt';
$ip = $_SERVER['REMOTE_ADDR'];
$filename = $dir.'/'.$file;
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$info = '
===========================
IP: '.$ip .'
Time: '.date('d/m/Y H:i:s a').'
Filename: '.basename($_SERVER['PHP_SELF']).'
URL: '.$url.'
';
file_put_contents($filename, $info, FILE_APPEND);
?>
This is the code to save visitor information. The file will save as current date name .txt file in visitor directory. we have gathered all information in $info variable. and then we save visitor info using file_put_contents() php built-in function. We also use FILE_APPEND to not override previous visitor info and append information is the same file.
So this is the easy process to track visitors which page visited or not. You can use this code top of the page or bottom of the page wish is your own. I hope you understand this one. If you like this tutorial please share it and encourage us to make new articles for you. You can also subscribe to our newsletter to get our regular updated. If you have any problem to understanding this tutorial please comment us below. I will back with new tutorials soon. Till then be happy, have a good time.