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.

Leave a Reply

Scroll To Top