It is recommended to check the end user’s key daily. This is important for handing refunds and dealing with malicious users who break your service agreement.
If your app is a web app, or will be installed on a webserver, or is part of a website, such as a plugin or theme, you should set up a simple cron job to do run daily.
NOTE: If your app is a stand-alone program with a clearly defined startup or launch event like a piece of windows/mac software or a video game, you should check the key during startup instead. See our full documentation to checking keys at startup to learn how to do that.
Getting The Key #
When your app first activates a key, it’s best practice to save the key from the user input into a secure local file or database. From this data you can preform key checks any time you need.
Creating A Pseudo Cron #
Most apps will be installed across a variety of dedicated servers and shared hosting environments, many of which have strict limitations on cron job creation. Since you will not be able to reliably create a server cron job programmatically, the best solution is to create a pseudo cron.
Create a file that is installed with your app with similar code to the below example. Note that we use PHP in these examples but this can be accomplished in any language.
$last_run_file = 'last_run.txt';
$last_run = file_get_contents($last_run_file);
// Check if the script has been run today
if (date('Y-m-d') !== $last_run) {
// Run the daily tasks
include('key_check.php');
// Update the last run date
file_put_contents($last_run_file, date('Y-m-d'));
}
This file will save the last time it ran to a local file, and if it’s been more than 24 hours since it last ran it will run now and save the new time. If it’s been less than 24 hours it will do nothing.
The file key_check.php is where your actual key check logic should be. Be sure to include this file in a major part of your app that will run often, like so:
// footer.php (or another frequently accessed file)
include('pseudo_cron.php');
WordPress Plugins And Themes #
If your app is a WordPress plugin or theme you’re in luck as they already have a pre-built pseudo cron called Wp-Cron. You can use WordPress hooks to create a cron job.
WordPress, Using PHP: #
function my_plugin_activate() {
if (!wp_next_scheduled('my_plugin_daily_license_check')) {
wp_schedule_event(time(), 'daily', 'my_plugin_daily_license_check');
}
}
register_activation_hook(__FILE__, 'my_plugin_activate');
It’s also good practice to always clean up your cron jobs on an uninstall, so use something similar to this example.
function my_plugin_deactivate() {
$timestamp = wp_next_scheduled('my_plugin_daily_license_check');
wp_unschedule_event($timestamp, 'my_plugin_daily_license_check');
}
register_deactivation_hook(__FILE__, 'my_plugin_deactivate');