Hello Friends, Today we are going to learn how to make web application installation detector in PHP. I came up with this idea because many of the times we develop for client but even after delivery we don’t get payment. Or sometime we develop for free or in partnership then might be possible your application get copied and distributed without your knowledge. So in these situations it can be very helpful to track where your application is running and performing some actions i.e suspending, deleting/terminating. Now without wasting any time lets code but before coding we need database and a table in it to store installation data.
1 2 3 4 5 6 7 8 9 |
CREATE TABLE `installations` ( `id` int(11) NOT NULL, `application` varchar(100) NOT NULL, `client_name` varchar(100) NOT NULL, `client_addr` varchar(100) NOT NULL, `client_mac` varchar(100) NOT NULL, `last_used` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(), `status` varchar(50) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; |
There will be two part of this code one for server side which will reside at our end and another for client side which will be in the actual application which we are delivering/distributing. Make sure you host server/our side code publicly available on internet. You can use third party hosting.
Server Side Code
config.php
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
<?php define("DATABASE_NAME", "applications");//database name define("T_INSTALLATIONS", "installations");//table name function getConnection() { $servername = "localhost"; //database host $username = "root"; // database username $password = ""; //database password // Create connection $conn = new mysqli($servername, $username, $password, DATABASE_NAME); // Check connection if (mysqli_connect_error()) { die("Database connection failed: " . mysqli_connect_error()); } else { return $conn; } } function isInsallationDataExists($data) { $conn = getConnection(); $application = mysqli_real_escape_string($conn, trim($data['application'])); $client_addr = mysqli_real_escape_string($conn, trim($data['client_addr'])); $client_mac = mysqli_real_escape_string($conn, trim($data['client_mac'])); if($client_mac <> "") { $result = $conn->query("SELECT id FROM " . T_INSTALLATIONS . " WHERE client_mac = '{$client_mac}' AND application = '{$application}'"); return ($result->num_rows > 0 ? true : false); } $results = $conn->query("SELECT id FROM " . T_INSTALLATIONS . " WHERE client_addr = '{$client_addr}' AND application = '{$application}'"); if ($results->num_rows > 0) { return true; } return false; } function addInstallationData($data) { $conn = getConnection(); $application = mysqli_real_escape_string($conn, trim($data['application'])); $client_name = mysqli_real_escape_string($conn, trim($data['client_name'])); $client_addr = mysqli_real_escape_string($conn, trim($data['client_addr'])); $client_mac = mysqli_real_escape_string($conn, trim($data['client_mac'])); $last_used = mysqli_real_escape_string($conn, trim($data['last_used'])); $status = mysqli_real_escape_string($conn, trim($data['status'])); if ($application == '' || $client_name == '' || $client_addr == '' || isInsallationDataExists($data)) { return; } return $conn->query("INSERT INTO " . T_INSTALLATIONS . " (application, client_name, client_addr, client_mac, last_used, status) VALUES('{$application}', '{$client_name}', '{$client_addr}', '{$client_mac}', '{$last_used}', '{$status}')"); } function updateInstallationData($data) { $conn = getConnection(); $application = mysqli_real_escape_string($conn, trim($data['application'])); $client_addr = mysqli_real_escape_string($conn, trim($data['client_addr'])); $client_mac = mysqli_real_escape_string($conn, trim($data['client_mac'])); $last_used = mysqli_real_escape_string($conn, trim($data['last_used'])); $status = mysqli_real_escape_string($conn, trim($data['status'])); if($client_mac <> "") { return $conn->query("UPDATE " . T_INSTALLATIONS . " SET last_used = '{$last_used}', status = '{$status}' WHERE client_mac = '{$client_mac}' AND application = '{$application}'"); } return $conn->query("UPDATE " . T_INSTALLATIONS . " SET last_used = '{$last_used}', status = '{$status}' WHERE client_addr = '{$client_addr}' AND application = '{$application}'"); } function getInstallationsData($columns = array(), $filters = array(), $limit = -1, $offset = 0, $order_by = 'id', $order = 'ASC') { $conn = getConnection(); $data = array(); $sql = "SELECT * FROM " . T_INSTALLATIONS . " WHERE 1"; if (!empty($columns) && is_array($columns)) { $sql = "SELECT `" . implode("`,`", $columns) . "` FROM " . T_INSTALLATIONS . " WHERE 1"; } if (isset($filters['application']) && trim($filters['application']) <> "") { $application = mysqli_real_escape_string($conn, $filters['application']); $sql .= " AND application LIKE '%{$application}%'"; } if (isset($filters['client_name']) && trim($filters['client_name']) <> "") { $client_name = mysqli_real_escape_string($conn, $filters['client_name']); $sql .= " AND client_name = '{$client_name}'"; } if (isset($filters['client_addr']) && trim($filters['client_addr']) <> "") { $client_addr = mysqli_real_escape_string($conn, $filters['client_addr']); $sql .= " AND client_addr = '{$client_addr}'"; } if (isset($filters['client_mac']) && trim($filters['client_mac']) <> "") { $client_mac = mysqli_real_escape_string($conn, $filters['client_mac']); $sql .= " AND client_mac = '{$client_mac}'"; } if (isset($filters['status']) && trim($filters['status']) <> "") { $status = mysqli_real_escape_string($conn, $filters['status']); $sql .= " AND status = '{$status}'"; } if (isset($filters['q']) && trim($filters['q']) <> "") { $q = mysqli_real_escape_string($conn, $filters['q']); $sql .= " AND (application LIKE '%{$q}%' OR client_name LIKE '%{$q}%' OR client_addr LIKE '%{$q}%')"; } $sql .= " ORDER BY {$order_by} {$order}"; if ($limit != -1 && is_numeric($offset) && is_numeric($limit)) { $sql .= " LIMIT {$offset}, {$limit}"; } $results = $conn->query($sql); while ($row = $results->fetch_assoc()) { $data[] = $row; } return $data; } function getInstallationData($id) { $conn = getConnection(); $data = null; $id = mysqli_real_escape_string($conn, trim($id)); $results = $conn->query("SELECT * FROM " . T_INSTALLATIONS . " WHERE id = '{$id}'"); while ($row = $results->fetch_assoc()) { $data = $row; } return $data; } |
Here in the above code database connection information needs to be changed accordingly i.e database name “applications” to your database name, username to your username and password to your password etc.
requests.php
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 |
<?php require 'config.php'; if (isset($_REQUEST['application']) && isset($_REQUEST['client_name']) && isset($_REQUEST['client_addr'])) { $data['application'] = filter_var(trim($_REQUEST['application']), FILTER_SANITIZE_STRING); $data['client_name'] = filter_var(trim($_REQUEST['client_name']), FILTER_SANITIZE_STRING); $data['client_addr'] = filter_var(trim($_REQUEST['client_addr']), FILTER_SANITIZE_STRING); $data['client_mac'] = isset($_REQUEST['client_mac']) ? filter_var(trim($_REQUEST['client_mac']), FILTER_SANITIZE_STRING) : ""; $data['last_used'] = date("Y-m-d H:i:s"); $data['status'] = isset($_REQUEST['status']) ? filter_var(trim($_REQUEST['status']), FILTER_SANITIZE_STRING) : "active"; if (isInsallationDataExists($data)) { $filters = array("client_addr" => $data['client_addr']); if ($data['client_mac'] <> "") { $filters = array("client_mac" => $data['client_mac']); } $datas = getInstallationsData(array("status"), $filters); foreach ($datas as $d) { $status = $d['status']; } if(in_array($status, array("suspended", "deleted"))) { $data['status'] = $status; } updateInstallationData($data); } else { $status = $data['status']; addInstallationData($data); } echo $status; } |
index.php
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
<?php include_once 'config.php'; ?> <?php if(isset($_REQUEST['action']) && isset($_REQUEST['id']) && trim($_REQUEST['id']) <> "") { $data = getInstallationData(filter_var(trim($_REQUEST['id']), FILTER_SANITIZE_NUMBER_INT)); if(trim($_REQUEST['action']) == "suspend") { $data['status'] = "suspend"; } else if(trim($_REQUEST['action']) == "delete") { $data['status'] = "delete"; } else if(trim($_REQUEST['action']) == "activate") { $data['status'] = "active"; } if(updateInstallationData($data)) { header("location: index.php?msg=Updated Successfully"); exit(); } } $installations = getInstallationsData(); ?> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Application Installations</title> </head> <body> <h2>Application Installations</h2> <?= isset($_GET['msg']) ? '<p>' . $_GET['msg'] . '</p>' : '' ?> <table border="1" width="100%"> <thead> <tr> <td>Id</td> <td>Application</td> <td>Client Name</td> <td>Client Addr</td> <td>Client MAC</td> <td>Last Used</td> <td>Status</td> <td>Action</td> </tr> </thead> <tbody> <?php foreach ($installations as $i) { ?> <tr> <td><?= $i['id'] ?></td> <td><?= $i['application'] ?></td> <td><?= $i['client_name'] ?></td> <td><?= $i['client_addr'] ?></td> <td><?= $i['client_mac'] ?></td> <td><?= $i['last_used'] ?></td> <td><?= $i['status'] ?></td> <td> <a href="index.php?action=activate&id=<?= $i['id'] ?>">Activate</a> <a href="index.php?action=suspend&id=<?= $i['id'] ?>" onclick="return confirm('Sure? You want to suspend.')">Suspend</a> <a href="index.php?action=delete&id=<?= $i['id'] ?>" style="color:red;" onclick="return confirm('Sure? You want to terminate.')">Terminate</a> </td> </tr> <?php } ?> </tbody> </table> </body> </html> |
This code is for showing list of all the installations and performing actions like activate, suspend, terminate.
Screenshot
Client/Application Side Code
index.php
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 |
<?php function getMac() { ob_start(); system('arp -an'); $subject = ob_get_contents(); ob_clean(); preg_match("/([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})/", $subject, $matches); return isset($matches[0]) ? $matches[0] : ''; } $host = "https://zatackcoder.com/demo/";//"http://127.0.0.1/";//"http://locahost/"; $params[0] = "client_name=" . urlencode($_SERVER['SERVER_NAME']); $params[1] = "client_addr=" . urlencode($_SERVER['SERVER_ADDR']); if ($_SERVER['SERVER_NAME'] == 'localhost' || $_SERVER['SERVER_ADDR'] == '127.0.0.1') { $params[2] = "client_mac=" . getMac(); //get mac address in case of application running on localhost } $response = file_get_contents($host . "installation-detector-host/requests.php?application=test&" . implode("&", $params) . "&status=active"); if (strtolower($response) == "suspend") { file_get_contents($host . "installation-detector-host/requests.php?application=test&" . implode("&", $params) . "&status=suspended"); die("suspended"); //can be replace with your code and logic } else if (strtolower($response) == "delete") { file_get_contents($host . "installation-detector-host/requests.php?application=test&" . implode("&", $params) . "&status=deleted"); unlink("important-file-to-run.php"); //can be replace with your code and logic } ?> |
Here in the above code https://zatackcoder.com/demo/Â should be replaced with your domain or ip address and then code can be pasted at the top of every page or you can create header.php and include it in every page of the application. You can also see getMac() function in above code which is to get MAC address of the system if your application is running in localhost. It detect multiple copies of your application on different system running localhost. You can identify multiple copies of your application using MAC address.
Note: if you don’t want to perform any action and the application will be hosted on any third party hosting i.e godaddy, hostgator, bigrock etc. then below one line code is enough to track installations.
1 |
<?php file_get_contents("http://your-domain-or-ip/installation-detector-host/requests.php?application=test&client_name=" . urlencode($_SERVER['SERVER_NAME']) . "&client_addr=" . urlencode($_SERVER['SERVER_ADDR']) . "&status=active"); ?> |
Screenshots
NetBeans Project Download
Application Installation Detector NetBeans Projects
Thanks friends
Please don’t forget to share if you find this helpful
Comments