License Your Scripts
Posted by Pete | Posted in PHP Tutorials | Posted on 02-11-2009-05-2008
0
After a week off in the lake district im now back and ready for some coding. So heres a license system which will allow you to license off your coding work to only users you choose.
So we must understand what will happen

Now we know what needs to happen lets start making it. First you need to create a Database with username and password on the licensing server website then you need to login to phpMyAdmin and select the database you have just created. Now click SQL and enter the following into the big box that appears.
CREATE TABLE `licenses` ( `id` int(50) NOT NULL AUTO_INCREMENT, `domain` varchar(200) NOT NULL, `ip` varchar(50) NOT NULL, `status` varchar(20) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM;
This will create a table with the 4 fields id, domain, ip and status. This is where we will store our licenses.
Now we have the database sorted lets create the licensing server part.
server.php
<?php /** * To begin with we need to setup the configuration so we are able to connect to the database. */ $conn = mysql_connect("DB_HOST","DB_USER","DB_PASS"); // Replace DB_HOST with localtion of database usually localhost and DB_USER and DB_PASS with your database details if (!$conn) die ("Could not connect MySQL"); // Check if could connect to mysql db. mysql_select_db("DB_NAME",$conn) or die ("Could not open database"); // Replace DB_NAME with database name and open the database. If not die :). /** * First we will retrieve the variables from the POST sent to us. * And we will make sure they are safe to be used. */ foreach ($_POST as $key => $value) // Go through all items in the $_POST array { // Carry out the foreach on the $field assigning the key and value of the array to $key and $value. $key = strip_tags($key); // Remove any tags from the key $value = strip_tags($value); // Remove any tags from the value $_POST[$key] = htmlentities($value, ENT_QUOTES); // Convert all applicable characters to HTML entities and assign them back to $_POST array. } /** * Now we need to check if the information passed to use is genuine */ $retrieve_q = mysql_query("SELECT * FROM `licenses` WHERE `domain` = '".$_POST['domain']."' && `ip` = '".$_POST['ip']."' && `status` = 'active'"); // Select from the licenses table where the domain, ip match the details passed from the local server and check if the license status is active. if(!mysql_num_rows($retrieve_q)){ // Check if any rows match the criteria above. echo "invalid"; // If not echo invalid. } ?>
and finally the part which goes on the clients website
local.php
<?php /** * First we must retrieve the domain name and the server ip. */ $domain = str_ireplace("www.", "", $_SERVER['HTTP_HOST']); // Remove www. if it has it $ip = $_SERVER['SERVER_ADDR']; // Get server ip /** * Now we will create the function which will connect to the license server and retrieve the status. */ function license_check($url, $curl_data) // Start license_check function { $options = array(CURLOPT_RETURNTRANSFER => true, // return web page CURLOPT_USERAGENT => $domain . "(" . $ip . ") LICENSE CHECK", // who am i CURLOPT_CONNECTTIMEOUT => 60, // timeout on connect CURLOPT_TIMEOUT => 60, // timeout on response CURLOPT_POST => 1, // i am sending post data CURLOPT_POSTFIELDS => $curl_data, // Post data ); $ch = curl_init($url); // Start the connection to $url curl_setopt_array($ch, $options); // Set the options above into the curl $content = curl_exec($ch); // Execute the connection with the options and retrieve the page curl_close($ch); // Close Connecion return $content; // Return $content } /** * Now to use the function we just created and check the license if valid. */ $license = license_check("URL TO SERVER.PHP", "domain=" . $domain . "&ip=" . $ip); // Run the function connecting to the location of the server.php and with the domain and ip post variables $license = trim($license); if (empty($license)) // Check if $license returned valid or not. { echo "You have a VALID License. :)"; // If it did echo valid }else{ die("Unable to find a valid license for this domain and server ip."); // If not show stop anything else running and show error message. } ?>
And there we have it now if you upload local.php to your website and enter in your website’s domain and ip into the database and set the status to active. then run local.php you will get a success message.
TIPS
One of the big problems your probably thinking now is well how do I stop people editing local.php I too have thought this and the way to resolve the problem is put the code of local.php in with functions and classes that are vital for your website to run and would not work without them. Then use a encryptor such as ioncube to encrypt the file. Ioncube is the best source encryptor around.
So thats it if you have any further question or would like to leave feed back please leave a comment.


