Create User Log

Today, I tried to make user log with PHP. User log is used to save information about visitor, like IP address and web browser. User log saved in html file. So you don’t need databases like MySQL. In this user log script, we need date() function (to set time) and variable like $REMOTE_ADDR to get IP address from user and $HTTP_USER_AGENT to know web browser in user. Usually this script must be place in index of web site (index.php). Then you need to created log.html file to save user log.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Welcome to My Page!</title>
</head>
<body>
<?php
//to get time and save in $time variable
$time = date("F jS Y, h:iA");
 
//to get IP Address and save in $ip variable
$ip = $_SERVER['REMOTE_ADDR'];
 
//to get hostname and save in $hostname
$hostname = gethostbyaddr($_SERVER['REMOTE_ADDR'])
 
//to get web browser and save in $browser variable
$browser = $_SERVER['HTTP_USER_AGENT'];
 
//open log.html
$fp = fopen("log.html", "a");
//then input log on it
fputs($fp, "<strong>Time : </strong> $time <br />
<strong>IP Address :</strong>$ip <br />
<strong>Hostname (if any):</strong> $hostname <br />
<strong>Browser : </strong> $browser<br />
<br />");
//close log.html
fclose($fp);
?>
<a href="log.html">See User Log</a>
 
</body>
</html>

Save it to index.php. Try to open it from your localhost, for example http://localhost/your_root_website/index.php.

Then click on “See User Log” link and get your IP logged on it.
Nice right?



Related Post:

Post a Comment

Your email is never published nor shared. You're allow to say what you want...

Blogroll Link Update