Category Archives: PHP

Scheduling task on web server to database

Although it sounds simple, but please trust me that it always troubled me.

OS level Cron is a good choice but it does not have any protection against multiple execution. Let’s imagine you have created an event that executes every 10 seconds, but the logic inside the event (i.e. curl) can take longer than 10 seconds, so it can pile-up. In the worst case, when an event contains a follow up action of ‘insert’ query, it can cause duplication.

MySQL Event Scheduler that available on version 5.1.6 is at the database layer implementation but it has some way to avoid deadlock. https://www.percona.com/blog/2015/02/25/using-mysql-event-scheduler-and-how-to-prevent-contention/

Database layer of scheduler is useful if you just wanto modify the data without other ‘external’ action such sending email or calling another php script. It is also useful for database hosted on AWS RDS cloud.

Gearman is another totally different tool but kind of related. http://gearman.org/

Gearman provides a generic application framework to farm out work to other machines or processes that are better suited to do the work. It allows you to do work in parallel, to load balance processing, and to call functions between languages. It can be used in a variety of applications, from high-availability web sites to the transport of database replication events.

Imagine you crawling multiple website base on today captured records. Gearman help you distribute task evenly across available client, changing a ‘serial’ process into ‘parallel’. The speed it give you help to avoid the above mentioned deadlock situation. However, it still not protect against multiple execution.

For now, I find this article very helpful: http://bencane.com/2015/09/22/preventing-duplicate-cron-job-executions/

Font Awesome to PNG converter using PHP

Font Awesome gives you scalable vector icons that can instantly be customized — size, color, drop shadow, and anything that can be done with the power of CSS. It’s free and open source, available at http://fortawesome.github.io/Font-Awesome/ It’s very ready to use in HTML: I have created this open source at Github: https://github.com/exiang/php-font-awesome-to-png that helps you mass extract the icons into PNG images. Then, you can use it for your native mobile app development like ios, android and windows phone.
  1. First, download the zip file and extract it to htdocs\php-font-awesome-to-png-master
  2. Make sure your web server have php5 and gd module installed.
  3. We need to download fontawesome-webfont.ttf separately from https://github.com/FortAwesome/Font-Awesome/tree/master/fonts. Drop it into the same directory as the php code.
  4. You may run the php code from browser or console.
Modify the generate.php By default, php-font-awesome-to-png generate png images at the following size as this code shows: It’s a PHP array, just modify at your will to add-in or deduct any size. As for colors, it is define as PHP array too: Please note not to use 255,0,255 as it is reserved color for background trimming. Modify the icons.data.php icons.data.php is a mapping between the character code used by fontawesome into the outputted png file name. Edit it if necessary, for example when fontawesome added new icon. You will need to refer to fontawesome css file. http://netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css For example, glass icon in css is: so the mapping in our php code would be like: Now, we are done 😉 Happy using the converter and please help the project by contribute your latest up to date icons.data.php that sync with fontawesome. TQ.  

Using S3 for web storage with PHP

Amazon S3 is a reliable and affordable cloud storage. When you host a file up there, it is automatically scale-able. Not to confuse with your traditional hosting ftp diskspace, s3 is meant to store static files like images, html, js, css and etc; but not dynamically executed server side code like PHP. In this tutorial, I will teach you how to create a web enabled s3 bucket and how to use PHP code to upload your content onto it. An S3 bucket is a grouping or set of discrete storage container where you can define specific rules and condition. For example, you can create a bucketA which is private while another bucketB which is publicly accessible from the web. You can also create another bucketC which only serve to log access data generated from the bucketB. Be notice that each bucket name have to be globally unique and follow the naming convention here. Your bucket name will also be part of your url if you choose to open the bucket to public. You can have unlimited folders and files inside a bucket. Create a web access bucket s31 Login to your AWS web console, goto S3 section. Click “Create Bucket” button found on the top right of page. s32 On the right is your bucket properties. We will need to make changes to Permissions and Static Web Hosting section.   s34 For static web hosting section, note down your end point url. Then click the Enable Website hosting radio button. Do not forget to insert your index document name too. In this case, i just use index.htm. If this file is missing, it will show a 404 File not found HTTP error page. Unlike a convention HTTP server, it wont display a list of other files found in this directory.   s33 For Permission section, Click on the Edit Bucket Policy button. Then, insert the code as shown. Remember to change your resource name to match your bucket name. This move is to give right to the public to have read access on your bucket and every files/folders in it. Ok, now you have successfully created your bucket and may test it with the endpoint url. Download Amazon SDK for PHP Next, we will download Amazon SDK for PHP. As amazon update their SDK frequently, the following information might be out-dated. However, the concept would still be the same. Visit http://aws.amazon.com/sdkforphp/ for the download file link. Click ‘AWS SDK for PHP‘. Notice the file downloaded is aws.phar. PHAR is a very nice feature from PHP 5.2 and newer for easy distribution and installation. Writing PHP code to upload file to s3  

Grabbing JPJ latest Plate No

Unlike many of Malaysia government website, JPJ is serious about protecting their latest car plate info found in their website. The plate info can be found at url http://www.jpj.gov.my/transaksi-percuma-no.-pendaftaran-terkini

Portal Rasmi Jabatan Pengangkutan Jalan Malaysia

If you look into the source code, you would find that this info is actually in an external page included thru iframe. The exact url would be: http://blacklist.jpj.gov.my/malay/plateno.shtml Which will give you this error page when u try to open it separately in browser,

Error

I figured out the protection method should be simple, most probably using http referrer checking. so by using a simple code as below, i can bypass the simple security checking easily and acquired the real content. Note: this piece of code is just a snapshot part of the complete one. So, checking http referrer is a very simple protection method that will not works against advance grabbing technique where the referrer url can be fake.