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

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);
 
        fclose($fp);
    } 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.

About Markus Tenghamn

avatar

Markus Tenghamn is a programmer and entrepreneur currently studying computer science at Mälardalens University (MDH), Sweden, in addition to being a big part of several new and innovative startups. Markus has pursued web development for over 10 years and started his first ecommerce website at the age of 17. At the moment Markus is responsible for developing MDH Schedule iPhone app (unofficial) and sync server which let's students see their course schedules right on their phone. In addition to other projects, he is a volunteer for SFSFUM (Meeting for student unions in Sweden) where he developed an app for iPhone, Android and Windows Phone 7 which gives participants maps with direction and event information. He has also been a finalist for the Rising Star scholarship awarded by Östsvenska Handelskammaren for course projects at MDH.


Markus's latest startup is called Anveto AB (Anveto.com), an innovative analytics and marketing platform which will bring truly unique and new ideas to companies looking to increase revenue.


In his spare time Markus prefers to write about web development, app development and startups on his blog at MarkusTenghamn.com. When he is not doing that he loves to read about AI, chaos theory, space and quantum mechanics.

2 comments

  1. avatar

    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?

    • avatar

      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 Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" cssfile="">

Scroll To Top
OR
SUBSCRIBE!
I will send you a quick email whenever I post something interesting! you can opt out at any time by clicking a link and I never give your information to third parties.
WANT MORE WEB DEV POSTS?
WANT MORE FREEBIES? SUBSCRIBE!
WANT MORE GAMING POSTS?
WANT MORE IOS DEV POSTS?
WANT MORE ANDROID DEV POSTS?
STAY UPDATED!
GET THE LATEST POSTS VIA EMAIL!
GET MY LATEST REVIEWS!