• About Us
  • Blog
  • Basket
  • Account
  • Sign In


Published on Tuesday, April 9, 2013

Automatic PHP Updates With Cron

This guide shows how 51Degrees.mobi Premium Device Data can be automatically updated in a PHP environment. If you are a Lite Data user you still need to manually update your data file, or upgrade to Premium Data to keep your Device Data up to date automatically.

51Degrees.mobi Detector for PHP ensures that the Premium Data file is kept up to date by offering an update feature that will download and apply any new data released by 51Degrees.mobi.

Unfortunately, a vanilla PHP install will only respond to web requests, so cannot be made to update by itself regularly. Instead, updates can be triggered by requesting '51DUpdate.php'. Visiting this page will cause the PHP runtime to check for a newer version of 51Degrees Device Data and install it. Before continuing any further with this guide, check that you are already able to get data updates in our user guide.

To automate this we can use a cron job, although any process that can request the webpage will have the same effect.

A cron job is a basic program used for scheduling tasks and is available in just about any Unix or Unix-style system. To work, it first needs a crontab; a file that tells the scheduler what task needs doing and how often.

Cron tabs work by specifying the time with parts seperated by a space, and then the task:

'minute hour day month day-of-week command-line-to-execute'

The asterisk indicates that the cron expression will match for all values of the field; e.g., using an asterisk in the 4th field (month) would indicate every month.

For example, our cron tab looks like this:

* * * * 4 /usr/bin/wget http://YourWebsite.com/51Degrees/51DUpdate.php

Note that several *'s are used to specify that the job should happen only every thursday, as our Premium Device Data is released on wednesday every week. Also note that a full path is used for wget. Some servers implement tighter security so a cron job needs to be explicitly told exactly what command to run. Wget is used even though it isn't a browser, as it will create a web request which is sufficient to trigger the update.

This line should be saved as an ASCII text file with a .txt extension. In this example it'll be called 'crontab.txt'. You should also use a simple text editor, as something like Microsoft Word may add extra artifacts to the file that will not work with cron.

The cronjob now needs to be added to the schedule. To do this, use an FTP client(or some other method) to copy the cronjob over to your server.

Once the file has been copied across, you need to open a terminal window and use the following command to add the file to the scheduler:

crontab crontab.txt

No output signifies a success and if an error was present, check that the crontab file is properly formatted.

51DUpdate.php will be polled according to your schedule. You can check when your data was published by using the following code:



echo $_51d_meta_data;


Note that this provides the date that the data was published, not the date it was downloaded. The data is published weekly, but if you find that you're not receiving updates see the update section of the user guide.

For more information about the crontab process, you can visit LinuxManPages.com

Comments (0)

Author: Anonym

Categories: Development

Tags: PHP



.NET 4G 51Degrees 5G Acer Adform Adtech Advertising Afilias Alcatel Amazon AMP Analysis Analytics Android Apache API Apple Asian Market ASP.NET Asus Blackberry Browser C C# Centro Chrome chromium Client hints Cloud CMS CPU CSS3 Data Data Blog Data File Daydream Design Detection Developers Device Device Data Device Detection Device Intelligence Device Models Device property DeviceAtlas Disney dmexco DotNetNuke Download ebay Ericsson Event Facebook Firefox Foundation Framework Galaxy git repositories Google Google Analytics Google Daydream GPU GSMA Guess HAProxy Hash Trie HTC HTML5 HTTP HTTP Headers Huawei Infinix Ingeniux Internet usage iOS iOS 13 ipad iPadOS iPhone iPhone 11 Java Javascript Kentico LG Liferay LTE m.dot Memory Memory leak Meta Data Microsoft Mobile Mobile Analysis Mobile Analytics Mobile Devices Mobile Marketing Mixer Motorola Mozilla MWC MWC 2017 MWC16 Native Apps NET New Release News Nexus NFC NGINX Nokia OnePlus 5 Opera Operating System Oppo Optimisation OS Patent Performance PHP Press Release Price Band programmatic Publishers Python Redirection Research Responsive Images Responsive web design RESS Review RTB RWD Safari Samsung Scala ScientiaMobile SEO Server Server-side optimisation Seznam.cz Sitecore Smart TV Smartphone Smartwatches Sony Swedish Beers Tablet Tencent Testing Tips Tutorial Umbraco Update User Agent User-Agent Valgrind Varnish Varnish Cache Video Vodafone VoLTE Web Web Apps Web content management Webtrekk White Paper Widgets WiFi Windows WURFL Xiaomi Xperia ZTE