Advertisement

Creating a Simple Licence for Your Application (php)

When you create an app you may only think about selling it to people, but what if you want to rent your application? how can you control your application that people are borrowing. You need a system to control your project that is a 'licence'. 

I will share a simple concept of how an application license is created. 



We have 3 objects here viz:

1. Application
2. Api
3. Database

The explanation of the diagram above is that an application will send a request to the Api (application program interface) and then will send it to the database to confirm whether the device has been registered or not. If the device has been registered in the database then the application can be opened if not then the application will give a warning to the client. Next, I will show you how to create a license with the PHP and SQL programming languages.

1. First create a database with the name 'db_licensi' then create a table with the name 'dc_licensi'
do like the structure below:

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
START TRANSACTION;
SET time_zone = "+00:00";

CREATE TABLE `dc_licensi` (
	`id` int(15) NOT NULL,
	`ip_address` varchar(126) NOT NULL,
	`deskrispi` varchar(256) NOT NULL,
	`expire` date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;



INSERT INTO `dc_licensi` (`id`, `ip_address`, `deskrispi`, `expire`) VALUES
(1, '192.168.1.213', 'tester', '2022-12-30');


ALTER TABLE `dc_licensi`
ADD PRIMARY KEY (`id`);

ALTER TABLE `dc_licensi`
MODIFY `id` int(15) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;
COMMIT;

# Field Type Sort Not Null Extra
1 id int(15) AUTO_INCREMENT
2 ip_address varchar(126) utf8mb4_general_ci
3 deskrispi varchar(256) utf8mb4_general_ci
4 expire date

2. Then we will create a license API that will confirm the application client device. save it with the name apilicensi.php
<?php

$host = "localhost"; /* Host name */
$user = "root"; /* User */
$password = ""; /* Password */
$dbname = "db_licensi"; /* Database name */

$con = mysqli_connect($host, $user, $password,$dbname);
// Check connection
if (!$con) {
 die("Connection failed: " . mysqli_connect_error());
}

   // $allcount_query);

$allcount_fetch = mysqli_fetch_array( $allcount_result = mysqli_query($con,"SELECT * FROM dc_licensi WHERE ip_address='".$_GET['ip']."'"));

if ($allcount_fetch) {
	echo json_encode(array('status'=>true));
}else{
	echo json_encode(array('status'=>false));
}

3. Then we will make an example of using the license api like this. save it with the name index.php

<?php

$myip = $_SERVER['SERVER_ADDR'];


$context = stream_context_create([
	'http'=>[
		'method'=>"GET",
		'header'=>"Accept-language: en\r\n" .
		"Cookie: foo=bar\r\n"
	],
	"ssl"=>[
		"verify_peer"=>false,
		"verify_peer_name"=>false,
	]
]);



$getURL = file_get_contents('http://192.168.1.213/[dir]/apilicensi.php?ip='.$myip, false, $context);
$data = json_decode($getURL);

if ($data->status==false) {
	echo "<script>alert('Access Denied')</script>";
}else{
	echo "It Work !";
}

Don't forget to register your IP address first in the database, here my IP is http://192.168.1.213/


then try running index.php

Post a Comment

0 Comments