My Way Create License Key For PHP Application

Seegatesite – Creating PHP license key for secure applications from software piracy. What do you feel if your paid application is used by people who do not have the right to use the application? of course, you are very disappointed and angry. I have experienced this some time ago. My POS Applications that I created in a few months, has been spread by people who do not have a right of usage because the application is not protected. So, an idea occurred to create software license in the form of a license key whenever the user doing the new installation a PHP application on the server locally or online. Here’s a simple way I protect the PHP application with a license key.

Basic concepts

My Way Create Simple Php Application License Key

The simple idea is in the header of the script will check if the license key is existing or not? if existing, whether the license key is valid or not. If invalid , the system will redirect users to the activation page to register first.

This activation code is checking the hard drive ID, so 1 license key is valid only for 1 hard drive. This method is effective for the offline application (intranet).

Parameter

To create a license key code, I use some parameters as follows

  • Secret code : a key which is only owned by us as a developer.
  • Username : username of the person who has the right to use the application.
  • Password : the owner password who has the right to use the application.
  • Unique code : In this tutorial, used the hard drive ID as the unique code.

How to check hard drive ID using PHP

To detect the hard drive id in PHP there are 2 ways I do. That is to detect the hard drive id for the windows operating system and detect hard drive id Linux operating systems from PHP server.

if(strtoupper(PHP_OS) == strtoupper("LINUX"))
{
	$ds=shell_exec('udevadm info --query=all --name=/dev/sda | grep ID_SERIAL_SHORT');
	$serialx = explode("=", $ds);
	$serial = $serialx[1];
}
else
{
	function GetVolumeLabel($drive) {
		if (preg_match('#Volume Serial Number is (.*)\n#i', shell_exec('dir '.$drive.':'), $m)) {
		$volname = ' ('.$m[1].')'; } else { $volname = ''; }
		return $volname;
	}
	$serial = str_replace("(","",str_replace(")","",GetVolumeLabel("c")));
}

Tutorial creates a simple license key on PHP application

Work Flow Tutorial Create License Key Php By Seegatesite

There are 2 sides in making the license key, i.e. from the client side (web application) and the server side (web activator).

Client Side (app folder)

1. Make a folder with name “app“.

2. Create a PHP file with name index.php and copy the following code

<!DOCTYPE html>
<html>
<head>
 <title>Index Page</title>
</head>
<body>
 <?php
 $secret_code = 'secret';
 if(strtoupper(PHP_OS) == strtoupper("LINUX"))
 {
 $ds=shell_exec('udevadm info --query=all --name=/dev/sda | grep ID_SERIAL_SHORT');
 $serialx = explode("=", $ds);
 $serial = $serialx[1];
 $licensi = md5('username'.'password'.trim($serial).$secret_code);
 }
 else
 {
 function GetVolumeLabel($drive) 
 {
 if (preg_match('#Volume Serial Number is (.*)\n#i', shell_exec('dir '.$drive.':'), $m)) 
 {
 $volname = ' ('.$m[1].')'; 
 } 
 else 
 { 
 $volname = ''; 
 }
 return $volname;
 }
 $serial = str_replace("(","",str_replace(")","",GetVolumeLabel("c")));
 $licensi = md5('username'.'password'.trim($serial).$secret_code);
 }
 $lisfile = $licensi.'.key';

 if(!file_exists(__DIR__.'/'.$lisfile))
 {
 header('Location: activator.html');
 }
 ?>

 <h1>Welcome to web application</h1>
 <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
 tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
 quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
 consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
 cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
 proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</body>
</html>

Explanation :

When the user open index.php page , the system will check if there is a license key file or not ? If no, then the user will be directed towards the activator page.

3. Create an HTML page with name activator.html and copy the following code

<!DOCTYPE html>
<html>
<head>
 <title>Activator</title>
 <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
 <div class="container">
 <div class="row">
 <div class="col-xs-12"><h2>Please activate your application before use</h2>
 </div>

 <div class="tab-pane fade active in" role="tabpanel" id="home" aria-labelledby="home-tab">
 <h3>* Please be sure you re connected to the Internet</h3>
 <div>
 <div class="form-group">
 <label for="exampleInputEmail1">Username</label>
 <input type="text" class="form-control" autofocus id="txtuser" placeholder="Username">
 </div>
 <div class="form-group">
 <label for="exampleInputPassword1">Password</label>
 <input type="password" class="form-control" id="txtpass" placeholder="Password">
 </div>
 <div class="form-group">
 <label for="exampleInputFile">Web Activator</label>
 <input type="text" value="" placeholder="URL WEB SERVER" class="form-control" id="txtweb">
 </div>
 <button type="submit" id="btnonline" class="btn btn-default">Submit</button>
 </div>
 </div>

 <div class="col-xs-12" id="result"></div>

 </div>

 <script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
 <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

 <script type="text/javascript">
 $(document).on("click","#btnonline",function(){
 var nama = $("#txtuser").val();
 var pass = $("#txtpass").val();
 var url = $("#txtweb").val();
 var value = {
 nama:nama,
 password:pass,
 url:url,
 method : "online",
 };
 $.ajax(
 {
 url : "c_activator.php",
 type: "POST",
 data : value,
 success: function(data, textStatus, jqXHR)
 {
 var data = jQuery.parseJSON(data);
 if(data.value == true)
 {
 $("#result").html("<h2>Application active!!, Thank you</h2><p>You can login from <a href='index.php'>this link</a>");
 $("#xtabbs").html("");
 }else
 {
 $("#result").html("Sorry, failed to activate your application");
 }

 },
 error: function(jqXHR, textStatus, errorThrown)
 {
 $("#result").html("error");
 
 }
 });
 })
 
 </script>
 </body>
 </html>

4. Create a PHP file with the name c_activator.php

<?php
if( isset( $_SERVER['HTTP_X_REQUESTED_WITH'] ) && ( $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' ) )
{
	$method=$_POST['method'];
	if($method == 'online')
	{
		$nama=$_POST['nama'];
		$password=$_POST['password'];
		if(strtoupper(PHP_OS) == strtoupper("LINUX"))
		{
			$ds=shell_exec('udevadm info --query=all --name=/dev/sda | grep ID_SERIAL_SHORT');
			$serialx = explode("=", $ds);
			$serial = $serialx[1];
		}
		else
		{
			function GetVolumeLabel($drive) {
				if (preg_match('#Volume Serial Number is (.*)\n#i', shell_exec('dir '.$drive.':'), $m)) {
					$volname = ' ('.$m[1].')';
				} else {
					$volname = '';
				}
				return $volname;
			}
			$serial = str_replace("(","",str_replace(")","",GetVolumeLabel("c")));
		}
		$url=$_POST['url'];
		$ch = curl_init();  

		curl_setopt($ch,CURLOPT_URL,$url.'/index.php?nama='.$nama.'&password='.$password.'&serial='.trim($serial));
		curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
		$output=curl_exec($ch);

		curl_close($ch);

		$array= array();
		if($output == 'error')
		{
			$array['value'] = false;

		}else
		{
			$content='empty';
			$fp = fopen($output.'.key',"wb");
			fwrite($fp,$content);
			fclose($fp);
			$array['value'] = true;
		}
		echo json_encode($array);
	}
	

} else {
	exit('No direct access allowed.');
}

Explanation :

The system will send the parameters needed by the server to establish the license key. The system will get the license key code on the server using  curl command.

After the license codes obtained from the server, the system will create a new file with the .key extension -> license_code.key

Build Secure License Key With Php

SERVER SIDE (webkey folder)

1. Create a folder with the name “webkey”

2. Create a file with the name index.php in the webkey folder and copy the following code

<?php 
$secret_key = 'secret'; 
$nama = trim($_GET['nama']); 
$password = trim($_GET['password']); 
$serial = trim($_GET['serial']); 
if(!isset($_GET['nama']) OR !isset($_GET['nama']) OR !isset($_GET['serial']) )
{ 
$licensi = 'error'; 
}
else 
{ 
if($nama == 'username') 
{ 
       if($password == 'password') 
       { 
           $as = true; 
       }else{ 
           $as = false; 
       } 
}
else
{ 
   $as = false; 
} 
if($as == true) 
{ 
$licensi = md5($nama.$password.$serial.$secret_key); 
}
else 
{ 
$licensi = 'error'; 
} 
} 
echo $licensi; 
?>

Explanation :

The server will accept the parameters sent by the client, then check each parameter are valid or not. The server will return the license code to the client, and the code will be processed by the client by creating a new file with .key extension.

Up here, you’ve finished making the simple license key using PHP. If you re running the code will be as the following video

Note, if you don’t encrypt your php files, all the above code would be futile anyway. Make sure you perform encryption on your php code using a reliable encryption. For a free encryption, I suggest you use encryption php file from fopo.com.ar

Tutorial creates a license key for PHP application is suitable for intranet applications or offline application. If you used online applications such as WordPress themes, plugins, and some tools supporting online applications, how to create the license key? I will discuss on the other occasions.

Please download the full code above at the following link

If there are suggestions and questions please leave a message in the comments below, I will be happy to assist you. So my quick tutorial how to create a license key for a PHP application may help you.

The following two tabs change content below.
This site is a personal Blog of Sigit Prasetya Nugroho, a Desktop developer and freelance web developer working in PHP, MySQL, WordPress.

Leave a Comment

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.

%d bloggers like this: