Background Tracker – How I made it myself

1435783446000 php Sysaxiom StackoverFlow

by Start Bootstrap


Last Night i made a task by myself for one of my work, To track the details of users who attempts to login the admin panel. I plan to get the basic details such as Operating System, Ip Address, Browser Details and trigger to the List of Admin who are listed.

Step 1 : 

To get the basic details of the Users’s which i listed above, i  used the following script (Which gained me several rep in Stackoverflow in the beginning days)

function GetDetails()
{
$u_agent = $_SERVER['HTTP_USER_AGENT'];
$bname = 'Unknown';
$platform = 'Unknown';
$version= "";

//First get the platform?
if (preg_match('/linux/i', $u_agent)) {
$platform = 'linux';
}
elseif (preg_match('/macintosh|mac os x/i', $u_agent)) {
$platform = 'mac';
}
elseif (preg_match('/windows|win32/i', $u_agent)) {
$platform = 'windows';
}

// Next get the name of the useragent yes seperately and for good reason
if(preg_match('/MSIE/i',$u_agent) && !preg_match('/Opera/i',$u_agent))
{
$bname = 'Internet Explorer';
$ub = "MSIE";
}
elseif(preg_match('/Firefox/i',$u_agent))
{
$bname = 'Mozilla Firefox';
$ub = "Firefox";
}
elseif(preg_match('/Chrome/i',$u_agent))
{
$bname = 'Google Chrome';
$ub = "Chrome";
}
elseif(preg_match('/Safari/i',$u_agent))
{
$bname = 'Apple Safari';
$ub = "Safari";
}
elseif(preg_match('/Opera/i',$u_agent))
{
$bname = 'Opera';
$ub = "Opera";
}
elseif(preg_match('/Netscape/i',$u_agent))
{
$bname = 'Netscape';
$ub = "Netscape";
}

// finally get the correct version number
$known = array('Version', $ub, 'other');
$pattern = '#(?' . join('|', $known) .
')[/ ]+(?[0-9.|a-zA-Z.]*)#';
if (!preg_match_all($pattern, $u_agent, $matches)) {
// we have no matching number just continue
}

// see how many we have
$i = count($matches['browser']);
if ($i != 1) {
//we will have two since we are not using 'other' argument yet
//see if version is before or after the name
if (strripos($u_agent,"Version") < strripos($u_agent,$ub)){
$version= $matches['version'][0];
}
else {
$version= $matches['version'][1];
}
}
else {
$version= $matches['version'][0];
}

// check if we have a number
if ($version==null || $version=="") {$version="?";}

return array(
'userAgent' => $u_agent,
'name' => $bname,
'version' => $version,
'platform' => $platform,
'pattern' => $pattern
);
}

It can be invoked easily by function GetDetails(),

To make it simple i am assigning it inside an array i.e.,

$Fetch = function GetDetails();

Now I can fetch those details by

$Fetch['name'];
$Fetch['version'];
$Fetch['platform'];
$_SERVER['REMOTE_ADDR'];

Note : 

The naming conventions can be changed according to the need.

Step 2 : 

To construct the Email that is to be fired

Constructing the Body

$Sub = "User : ".get_current_user().' accessed the Admin Panel.
Here are the Details 
';
$Sub .= 'Browser Name : '.$Fetch['name'].'
';
$Sub .= "Browser Version : ".$Fetch['version'].'
';
$Sub .= "User Operating System : ".$Fetch['platform'].'
';
$Sub .= "IP Address : ".$_SERVER['REMOTE_ADDR'].'
';

Constructing the Headers

$headers= "From: Kohlipe Travelers \r\n";
$headers.= "Reply-To: The Reply To Name \r\n";
$headers.= "X-Mailer: PHP/" . phpversion()."\r\n";
$headers.= "MIME-Version: 1.0" . "\r\n";
$headers.= "Content-type: text/html; charset=iso-8859-1\r\n";

Then to fire we can the below static way

mail('yourmail@yourdomain.com', 'Subject Regarding Accesss', $Sub, $headers, 'sysaxiom.com');

or to achieve the list of subscribed admin,

Step 3 :

Establish the DB Connection

$mysqli = new mysqli("localhost", "root", "pass", "dbname");
if (mysqli_connect_errno()) {
echo mysqli_connect_error();
exit();
}

Now i can use the variable $mysqli for all my DB Activities,

Following up,

Now getting all the lists and firing using the simple foreach loop

$query = "SELECT * FROM email_notify";

if ($results = $mysqli->query($query))
{
while ($row = $results->fetch_assoc())
{
mail($row['email'], 'Subject Regarding Access', $Sub, $headers, 'sysaxiom.com');
}
$results->free();
}

 

And now i have iterated all the lists inside the foreach loop which will fire itself the mail who are listed in the email_notify table.

The same above function can be used for the Illegal Access, in addition the subject and important priority can be changed accordingly.

And it was done ????