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
$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
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.


