WHMCS Licensing Addon - Storing the local key in a text file

Please see my updated guide on How to setup and configure the WHMCS Licensing Addon

This post will attempt to explain how to use a text file to store the license key and local key when check license checks are performed preventing your whmcs licensing addon script from calling remotely too often and only when needed.

Need help getting the WHMCS licensing addon working? Read my other post

At the end of our check license script we have some code that looks like this which will return the state of the license.
$results = check_license($licensekey, $localkey);

if ($results["status"]=="Active") {
# Allow Script to Run
if ($results["localkey"]) {
# Save Updated Local Key to DB or File
$localkeydata = $results["localkey"];
} elseif ($results["status"]=="Invalid") {
# Show Invalid Message
} elseif ($results["status"]=="Expired") {
# Show Expired Message
} elseif ($results["status"]=="Suspended") {
# Show Suspended Message

Well to change this up I will remove the $licensekey and $localkey declarations above that code and replace the code shown above with the following code.
// End Check Function
# Get Variables from storage (retrieve from wherever it's stored - DB, file, etc...)
//I pull the license and local key from the text file.
$file = 'license.txt';
if (file_exists($file)) {
$lines = explode("\n", file_get_contents($myfile));;
$licensekey = $lines[0];
$localkey = $lines[1];;

else {
$licensekey = "":
$localkey = "";

$results = check_license($licensekey, $localkey);

if ($results["status"] == "Active") {
# Allow Script to Run
if ($results["localkey"]) {
# Save Updated Local Key to DB or File
$localkeydata = $results["localkey"];
# Show Invalid Message
$fp = fopen("license.txt", "w");

fwrite($fp, $licensekey);

fwrite($fp, $localkey);

} elseif ($results["status"] == "Invalid") {

echo 'License invalid';

} elseif ($results["status"] == "Expired") {

echo 'License expired';

} elseif ($results["status"] == "Suspended") {

echo 'License suspended';


You will also need to create a text file which is called license.txt and add the license key to the first line.

The code which I have written will open the text file and read the first line (license key) and if there is a second line it will read that as the local key. Leave the second line blank when you add the text file, the script adds this line automatically upon license validation. Every time the license is validated the license key (stays the same) and the local key is updated and rewritten to the text file. As long as the local key is valid there will be no need for your script to do the remote check.

Hope that helps, please feel free to ask any questions by sending me an email or commenting.


Posted: 2014-07-18T12:45:35+02:00

Hello Markus, firstly I would like to thank you for the guides you've written about the licence add-on. You have provided more information and assistance than WHMCS themselves are willing to, they've even referred me to your blog when I've requested assistance with this licence module. I'm a little disappointed with how complex the licence module has been to setup, and puzzled as to why WHMCS don't just provide a fully working sample code for the local key file - I've wasted several hours now trying to get it working. I wanted to ask you though, I can not get the above code to work at all. I really want to use a local key, rather than have the WHMCS server validate the licence on each page load. I've tried to copy your code as is above and created the required .txt file, however I cannot get it to work. I get syntax errors in dreamweaver when copying your code which I appear to have resolved by modifying a few lines, but then I cannot get the licence to validate or the local key to be saved, my WHMCS access logs report a connection from your script, but the licence key is not presented. I'm copying the script as is above, directly into the check_sample_code.php as you indicate above, the check_sample_code.php has had the required fields modified like WHMCS install path and secret key etc and works just fine without your script (i.e. just validating the licence key) Do you have any advise or suggestions? Is it possible you can quickly checkover your code to see if there are syntax errors or any errors that would cause this?

Markus Tenghamn
Posted: 2014-07-18T12:54:21+02:00

The guide is outdated which may be why you are having problems. I no longer use WHMCS but have a good understanding of how the licensing addon works. If you would like you can get in touch with me via the contact form and I will try to help and perhaps write a new guide which will be more relevant.

Leave a comment