Give feedback to your users (PHP CLI coding)

Posted on July 17, 2011
by

Terminal PHP CLI

Whereas PHP’s main usage is to generate web pages, it can also be used as a scripting language in your shell (just like bash or python). To that purpose, you can use the CLI version of PHP (CLI standing for Command Line Interface).

The purpose of a CLI script is different, it’s not to generate a web page. Instead, it can be an automatisation task for your website or a maintenance task for your server. Therefore, of course, when you write a CLI script, you don’t output HTML, but instead you output short messages that will tell the user if the script went well or not.

This script can be run manually by you or a coworker, or a cron job can run it periodically. In all the cases, giving a feedback about the success or failure of your script is important. It’s not difficult, simply print a message and append a new line character to it (\n, not the HTML breaking line tag <br/>). I find my custom (and very simple) function println() (yes, inspired from Java :)) quite convenient for this. See code below:

Forex for beginners

Posted on May 19, 2011
by

Today, I’m taking the time to talk about my new project, Forexagone.com. It’s a website (in french only so far) whose aim is to teach Forex to very beginners, in a way it has not been done before. We really make Forex easy to learn for beginners, who can really start from the very beginning and end by reaching a good level in Forex trading. Our core business is divided into 3 main sectors.

Forex

Rotating your log files with logrotate

Posted on November 2, 2010
by

If you’re like me and you’re used to log what’s going on your web application, you might want to know how to rotate your log files just like Linux commonly does for its system logs. No need to code anything for that purpose, you can simply use the same software as your system: logrotate.

Attention: I wrote this post based on my personal server which is a Linux Debian 5.0 Lenny and logrotate 3.7.1, but it’s very likely that it works more or less the same way for any Linux server and version of logrotate.

When installing logrotate, it’s likely that it’s already designed to rotate some system logs (syslog, kern.log …), and in the case of a web server, it also rotates Apache or MySQL (access.log, error.log, mysql.log…). Having a quick look at the manual (#man logrotate), you’ll see you can easily tell logrotate to handle your own log files. Simply open /etc/logrotate.conf (or touch a new file in /etc/logrotate.d/ if it exists for your version), and add something like the following:
"/home/mysite/log/visitors.log" "/home/mysite/log/downloads.log" {
rotate 5
mail anon@ymous.com
size 100k
postrotate
/usr/bin/killall -HUP httpd
endscript
nocompress
}

This example section will handle both visitors.log and downloads.log (you can add as many file locations as you wish). The options are:
  • rotate 5: it means it will rotate 5 times before deleting data
  • mail anon@ymous.com: will send an email with the content of the latest log file before deleting it
  • size 100k: maximum size for a log file before rotating it
  • postrotate […] endscript: enclosed is a piece of script you want to be run after rotating logs
  • nocompress: add this line to prevent logrotate from compressing rotated log files

These are the main options for logrotate, but of course many others can be found, please read the manual for more.

And there you are, your custom log files are being rotated without having to code a custom PHP script 🙂

strtolower and UTF-8

Posted on September 27, 2010
by
Charset issues is something that always made me go mad. And since I’m french and I’ve designed many french websites, it’s something I couldn’t escape, thanks to all these special chars we have in our language 🙂

Well, today an issue came up with the strtolower function. Look at what follows:

$t1 = 'Expérience';
$t2 = strtolower($t1);
echo $t2; // echos 'expience'

See? It drops the two letters “ér”. No matter why and how it processes (for more details about UTF-8/ISO issues, please use google), what matters is that it totally screwed up my beautiful string.

On PHP.net, you can read this function uses the charset defined in the current locale. It means that whatever the encodage of your string is (UTF-8, ISO, …), even if you work with UTF-8 all along (database tables, database connection, page chars …), it will use the current locale charset anyway.

To this, I can see two options.