Getting a E-mail notification when someone visit your site

A few days earlier I was thinking to build a php script that will mail me when some one visits my site.  So last friday (friday is our holiday) I build this script which send me email notification about the visitor. (information like: ip, referrer, hostname,etc). I also wished to make this script into a complete stat script but I am too lazy. This script can be also integrated in WordPress blog sites. Just you have to include the index file in header of the theme. The script codes and file names are given below.

All the configuration settings are stored in config.php . Before using the script you have to edit this file with your own settings. Database for this script create automatically, you can see that if you go through the codes.

config.php

[php]
<?php
$mySQLserver="localhost"; //database host
$mySQLuser="root"; //database user name
$mySQLpass=""; //database password
$mySQLdb="testdb"; //database name
$toAddr="mailto@example.com"; // email address where you want to get emails
$fromAddr="mailfrom@example.com"; // from email address shows up on email header
$toSubj="IP log"; // subject of the email
?>
[/php]

model.php

[php]
<?php
include_once("config.php");
$con = mysql_connect($mySQLserver,$mySQLuser,$mySQLpass);
if (!$con)
{
echo(‘Could not connect to database: ‘ . mysql_error());
}

$db=mysql_select_db($mySQLdb, $con);
if(!$db)
{
mysql_query("CREATE DATABASE $mySQLdb",$con);
mysql_select_db($mySQLdb, $con);
}
//to module with wp

$sql="CREATE TABLE IF NOT EXISTS `ip_addr_store` (
`ip_addr` VARCHAR( 15 ) NOT NULL,
`time_stamp` VARCHAR( 20 ),
`user_agent` VARCHAR( 200 ) NOT NULL,
PRIMARY KEY ( `time_stamp` )
)";
mysql_query($sql);

function insertIP($ip)
{
$time=time();
$query="INSERT INTO `ip_addr_store` VALUES(‘".$ip."’,’".$time."’,’".$_SERVER[‘HTTP_USER_AGENT’]."’);";
$result = mysql_query($query);
return $result;
}

$visitor_res=mysql_query("SELECT * FROM `ip_addr_store`;");
function getDataRows($visitor_res)
{
if($visitor_res)
{
return mysql_fetch_row($visitor_res);
}
else
{
return ”;
}
}
function tableTruncate()
{
return mysql_query("TRUNCATE TABLE `ip_addr_store`;");
}
function mailIP($ip)
{
global $toAddr,$fromAddr,$toSubj;
$message="IP: $ip has logged into the site.nHostname: "
.gethostbyaddr($ip).
"nUser Agent: ".$_SERVER[‘HTTP_USER_AGENT’].
"nReferer: ".$_SERVER[‘HTTP_REFERER’].
"nX Forward: ".$_SERVER[‘HTTP_X_FORWARDED_FOR’];
$mail_hdr = "From: $fromAddr";
if(!mail($toAddr, $toSubj,$message,$mail_hdr))
{
echo "mail sending failed.";
}
}
?>
[/php]

The below file is used to show the visitor list in a tabular form.

userlist.php

[php]
<?php
include_once("model.php");
if($_GET["truncate"]=="1")
{
tableTruncate();
echo "Table truncated</br>";
echo ‘<a href="userlist.php">Back</a>’;
mysql_close($con);
die();
}
?>
<a href="userlist.php?truncate=1"><b>Truncate data</b></a>
<table border=1px>
<tr>
<th>IP</th><th>Date-Time</th><th>User Agent</th>
</tr>
<?php
while($row = getDataRows($visitor_res))
echo "<tr><td>".$row[0]."</td><td>".date(‘d-m-y at h:i:s a’,$row[1])."</td><td>".$row[2]."</td></tr>";
?>
</table>
<?php mysql_close($con); ?>
[/php]

index.php

[php]
<?php
session_start();
include_once("model.php");
$ip= $_SERVER[‘REMOTE_ADDR’];
echo "Lorem ipsum dolor si amet.<br/>"; // dummy text you can delete it.
if(!isset($_SESSION["ip"]))
{
$_SESSION["ip"]=$ip;
insertIP($ip);
mailIP($ip);
mysql_close($con);
}
?>
[/php]

Incoming search terms:

  • email notification on website visit
  • php code to send email notification when someone download file
  • php alert website visitor
  • notify-me php
  • notify script someone login to my site
  • notification of visit email
  • notification of site visitors

Tushar Roy

Software Engineer (Backend engineer), Android Developer,

6 thoughts on “Getting a E-mail notification when someone visit your site

  1. Being emailed with all the vital information each time a page is hit is extremely valuable for analyzing where you’re being contacted from, their navigation trail through your website, whether or not there’s frequent page reloading, and even spotting those who are likely up to no good.

  2. thanks for this awesome script. i’m using it to let me know if someone visits a directory that they’re not suppose to be looking at. 🙂

    i had some questions…

    1. it does send me an email, but upon visiting other directories where i have the script, it won’t send anymore. is it b/c the script already detected the ip address once already and will ignore future visits?

    2. where would i edit for the email to just let me know which directory/folder it came from?

    thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *


*