Resources: http://seegatesite.com/my-way-create-license-key-for-php-application/

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 Concept:

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.

[cc lang=”php” tab_size=”2″ lines=”40″]
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”)));
}
[/cc]

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

[cc lang=”php”]

Index Page

Welcome to web application

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.

 

[/cc]

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
[cc lang=”php”]

Activator

Please activate your application before use

* Please be sure you re connected to the Internet




[/cc]

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

[cc lang=”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.’);
}
[/cc]
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

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.

Send a Message