Web development related posts

Good practices for sending emails from your web server

On Forexagone.com, we like to do everything ourselves, meaning we don’t like to use many third-party services to handle our work, so we are sure that everything is tailored to our needs, well integrated and working all together. For sending our emails, we decided to do it ourselves as well and not use a service like mailchimp or such. I learned much throughout my research and work on implementing the necessary technical stuff, so I thought it’s a good idea to write my learning on my blog, so it will be like a reminder for me, and it could help others who want to do the same.

Nota Bene: this post is not about the email marketing strategy itself, it is only about the technical setup for sending emails from your server and doing it right.

Like me, you have your own reasons for sending emails from your own server and not using a third-party service. If you decide to do so and go on reading this article, you would like to know the benefits of implementing the things I describe. There are many benefits.

  • it lowers the chances that your emails land in the spambox of your subscribers (heck it is important!)
  • it makes you comply with the legislation (can vary from the country but better too much than not enough)
  • it makes your subscribers happy
  • it shows a professional attitude

My list of good practices might not be complete and I excuse in advance, but they are already pretty important to carry out.

Like it? Share it!

Weird 302 redirect at GoDaddy

It took me some time to realise what was going on, but I finally found out a weird bug (or feature?) on my GoDaddy web hosting this afternoon. Which made me switch to Namecheap later on.

This is what happens, totally randomly when a client is accessing a file. Let’s say you try to access the following URL: mysite.com/blog. These are the consecutive HTTP requests that will occur:

  1. GET mysite.com/blog
    302 redirect mysite.com/AbCdE/blog
  2. GET mysite.com/AbCdE/blog
    302 redirect mysite.com/blog
  3. GET mysite.com/blog
    200 OK

Where AbCdE is always a random string made of 5 characters. That’s right, randomly, requests happen to be redirected twice through a random directory that doesn’t exist before the server finally delivers the resource. What’s the matter, you will say? Well, this has absolutely no reason to happen, and even if a visitor wouldn’t notice it by loading the URL in his browser, this can have an impact in other fields.

First, Google tends to index the temporary URLs that doesn’t exist, and this leads to undesired pages to be indexed. Secondly, as this is an unexpected behaviour, this can fuck your scripts in some situations. It especially happened to me, when apps that are connected to my website couldn’t manage redirections that don’t output more than 255 characters for example. Because of this unwanted random 302 redirect, I couldn’t guarantee the availability of my app anymore.

I am sorry, but I am not responsible, as a developer, for this, and I don’t have to adjust my code to my web hosting in this situation. It seems to be a recurring problem at GoDaddy’s, you can check it out on Google, many people are talking about it, and GoDaddy doesn’t seem to be fixing the issue…

But I really wonder where this bug (or feature) comes from. I bet maybe for a feature, where by differing the actual delivery of the resource, it’s freeing some load on the server when too many connections are coming in at the same time.

Anyways, I am now hosting my site on Namecheap.

Like it? Share it!

Lazy loading for Javascript

In a previous post, I was discussing how to lazy load a Facebook widget, so that it loads only when really needed. I have put my thoughts further, and come up with a general way to lazy load any kind of JavaScript snippet.

I always take for example the Facebook widgets, which are always very heavy and load many files (include JS, CSS and images).If that widget is not in the viewport when your visitor loads your page at the beginning, and if it’s visible only after he scrolls, then you can tweak the performances of your page by lazy loading that widget. Note that it works for any kind of JavaScript snippet that would have a visible effect only after the user scrolls your page down.

In this post, I’ll introduce my jQuery plugin and show you a demo of how it works, but I’m not going to go very deep into the interior mechanisms (for that, see the Google Code page).

jQuery lazy loading JS

read more… »

Like it? Share it!

Deferred lazy loading of Facebook widgets

Facebook Like BoxIf you use social widgets on your web pages, you must be aware of the extra kilobytes of data your visitors are going to load for this, and how it’s going to affect the loading time of your pages. It can be difficult to figure the weight of those widgets, but sometimes they’re worth considering them.

You can use Firefox and the plugin YSlow to measure how those social widgets overweight your pages (see total size of the page, loading time, and number of external JS/CSS/image files loaded).

In this post, I will consider the case of the Facebook Like Box, which is a quite fat widget actually (almost 200kB of data amongst 6 external JS scripts, 4 CSS sheets, 2 CSS image sprites and as much images as there are fans of your page) and how I can manage to load it ONLY IF NEEDED. read more… »

Like it? Share it!

Give feedback to your users (PHP CLI coding)

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: read more… »

Like it? Share it!

Forex for beginners

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 read more… »

Like it? Share it!

Rotating your log files with logrotate

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 :-)

Like it? Share it!

Proposal: best practices for writing PHP

My motivation in writing this proposal comes from years of PHP practice, when I had to use other people’s code and their code looks like garbage so much that I wanted to throw it to the their face rather than using it. I can’t expect everybody to write clear code, but if I could turn the actual world in a world of beauty and smartness, I’d like people to follow the guidelines in this paper. Note that I didn’t pretend to write complete PHP good practices, as I’m just covering the writing aspect of PHP coding, with concerns such as readability and logic in the flow of code.

Good practices for PHP writing

read more… »

Like it? Share it!

strtolower and UTF-8

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. read more… »

Like it? Share it!

5 things to know before starting to design a WordPress theme

With hindsight, there are things that are definitively not obvious to me when I started to design my first WordPress theme, things which are not very well explained in the official documentation. To begin, be aware that WordPress theming is everything but beautiful coding: no OOP, no MVC, no respect of coding good practices, nor even of common sense sometimes… I’ve learnt these things at my expense, so I’m now going to warn you from the beginning so that you don’t get the cooling effect that I got.

read more… »

Like it? Share it!