Archive for the ‘Apache’ Category

Dating takes time, women are difficult but PHP?

Wednesday, March 18th, 2009

One of the big annoyances is having a blog or webpage in a certain language and that all the dates appear on your website in English. Of course you can choose just numbered formats, but sometimes the month or the day of the week in words adds that needed little extra polish.

Formatting a date in PHP

Normally a date is formatted like this with the date() function:

[sourcecode language=”php”]
< ?= date(‘d-m-Y’) ?>

Formatting a localised date in PHP

But to format a date in your preferred language you need to use the strftime function:

Format the time and/or date according to locale settings. Month and weekday names and other language-dependent strings respect the current locale set with setlocale().

So we also need the setlocale() function. This function let you set the language to use for the formatting. Your system or server should have them installed, otherwise it’s not possible! And most of them are not installed by default, although easily available.
What does the setlocale() function do:

Returns the new current locale, or FALSE if the locale functionality is not implemented on your platform, the specified locale does not exist or the category name is invalid.
An invalid category name also causes a warning message. Category/locale names can be found in RFC 1766 and ISO 639. Different systems have different naming schemes for locales.

Tough pages to consume to be honest, but let’s go on.

Locales settings on your server

You can find the installed localisations in this file on a Linux server:

[sourcecode language=”xml”]
vi var/lib/locales/supported.d/local

My server only had one line by default!
Simply add the one’s you’re missing. You can find all possible localisations in this file:

[sourcecode language=”xml”]

Don’t add them all: it will decrease performance.

So we’re nearly done:

[sourcecode language=”php”]
< ?
echo strftime(‘%s-$b-%y’,time())

You need to enter the correct encoding aswell, otherwise it won’t work. This is dependent of the server settings, so it will influence the portability of your code a bit.
Above code will give you the current date in Dutch, but we need another function to create localised dates from stored timestamps of the MySQL database.

Converting stored Timestamps to PHP understandable format

A MySQL timestamp value won’t work as a direct input to the strftime function. We need to convert it with another function: strtotime.

So we end up with something like this:

[sourcecode language=”php”]
< ?
echo strftime(‘%A %d $b %y’,strtotime($row->timestamp))

Quite a long road to take for a boring date!

Another php bug?

Saturday, April 12th, 2008

Tonight I had trouble with parsing url’s.

I wanted  to create some SEO-friendly url’s for a new site and the PATH_INFO server variable is a good base for it.

My url:


It’s easy to parse the url to segments with an explode on “/”

But I had a specific url that caused me troubles:


Strangely  the  trailing dot disappeared:




That’s weird. I never saw those differences before.

I tried it on an old server with php 5.1.2. And NO difference there.

Somehow a bug entered in php 5.2.5 that deletes a trailing dot in $_SERVER[‘PATH_INFO’].

Be aware!

Of course it can also be caused in interaction with the apache-server.

How to use Apache to compress (gzip) HTML, CSS and Javascript files

Tuesday, July 17th, 2007

In an earlier post we showed you how to use PHP to compress html files and save valuable bandwidth. Now we will choose a setup where apache will compress the files. This is a more efficient setup for your serverconfiguration.

Installing mod-deflate

When Apache 2 is installed, mod_deflate is automatically installed, but not always enabled. To enable mod_deflate on a debian or ubuntu distribution, we can do:
a2enmod deflate
On another system you have to edit Apache2’s configuration file manually. First locate then edit the config file. Add this to the LoadModule section:
LoadModule deflate_module /PATH_TO/mod_deflate.soThen restart Apache
apache2ctl restart

Enable the SetOutputFilter DEFLATE filter

Compression for APACHE 2 is implemented by the DEFLATE filter. To enable compression for documents simply put this filter in the appropriate Directory directives:

SetOutputFilter DEFLATE

But we don’t want to compress picture-files

But we do not want to compress everything! It doesn’t make any sense to compress files like gif, jpg or pdf’s. So we have to make an execption:

// Don't compress picture files
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ \ no-gzip dont-vary
// Don't compress compressed files
SetEnvIfNoCase Request_URI \.(?:exe|t?gz|zip|bz2|sit|rar)$ \no-gzip dont-vary
// Don't compress pdf's
SetEnvIfNoCase Request_URI \.pdf$ no-gzip dont-vary


Enabling compression is amazingly easy. Simply enable it in your apache directives. To sum it all up:

SetOutputFilter DEFLATE
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ \ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.(?:exe|t?gz|zip|bz2|sit|rar)$ \no-gzip dont-vary
SetEnvIfNoCase Request_URI \.pdf$ no-gzip dont-var

If you can’t edit your directives, you can alternatively use the .htaccess files in your webroot directory.
That’s all folks!

How to use gzip compression on your PHP websites

Wednesday, November 29th, 2006

Compressing weboutput can save valuable bandwidth. The other advantage is that clients will load your websites faster. Gzip compression is supported by all modern browsers, so why not use it. It will compress your pages between 60% and 80%. So you will lower your bandwidth-costs by an average of 70%. Also your server can push more requests in case you have limited connection speed. Of course it will not work for images.


Your (Apache)server can do the job, but only when it is configured for it. There was a Apache 1 Module called mod_gzip that compresses the HTML as it sends it out. For Apache 2 this module is called mod_deflate and it also compresses content before it is delivered to the client.


If you have no control over the Apache config files there is another way to solve the problem. Let PHP compress the content. If you have PHP > 4.3 you can use the zlib.output_compression.

It’s simple, change this setting to ON in your PHP ini file:
zlib.output_compression = On

That’s all. Every page will now send compressed content to every browser that understands the compression method.

NOTE: Don’t forget to disable gzip-compression in CMS-packages like Joomla in the administration section, otherwise you will spoil not only CPU resources but also you can run into trouble with the double compressed content that will show in some browsers only strange unreadable icons on the screen.

You can also adjust the compression level by setting this between 1 and 9:
zlib.output_compression_level = 6

Make your own choice between CPU time to compress on a higher level and the less data that will be sent.

Compression test

I ran a test, without gzip the content of my page is 20162 bytes. Now with different levels of compression, I came to the following results:

  1. 6779 bytes
  2. 6690 bytes
  3. 6591 bytes
  4. 6264 bytes
  5. 6147 bytes
  6. 6134 bytes
  7. 6127 bytes
  8. 6127 bytes
  9. 6125 bytes

I reduced the load of my webpage with 70%. The default value is 6, which is in my test the highest setting with some noticeable compression gains. So their is no real need for setting the level at all in the PHP ini:


Just delete the line, if it’s there, PHP info will show -1 which is fine, because the default compression level is 6.

Measuring CPU load is nearly impossible and should not be considered to be an issue until you run into problems with that. In most cases bandwidth is limited and CPU time is plentiful.

Alternative method 1

There was another way of reaching the same by puting this at the start of every PHP-script:
< ? ob_start("ob_gzhandler"); ?>

This still works in PHP 5, but according to the manual is not recommended.

Note: You cannot use both ob_gzhandler() and zlib.output_compression. Also note that using zlib.output_compression is preferred over ob_gzhandler().

Alternative method 2

If you have no access to your PHP.ini file you can set the option in the .htaccess file in the root directory of your website.

php_flag zlib.output_compression on

When CSS is embedded it’s automatically compressed as well. For enabling CSS files to be compressed, simply rename your style.css files to style.php and it’s done automatically. Of course not forget to refer from your html to the new name when linking.

When you don’t want to adjust the HTML, or for use with WordPress or Joomla that work with different templates that are automatically referred to from the code, the following solution will work and it’s simple again:

  1. Copy yourstylesheet.css to yourstylesheet.php
  2. Replace the content of yourstylesheet.css to

    @import url(yourstylesheet.php);


Edited 06-12-12:
For Mozilla/Firefox you need to add

< ? header("Content-type: text/css; charset: UTF-8"); ?>

to yourstylesheet.php at the start of the page. Otherwise the CSS is not recognised as CSS. In Opera/MSIE it was working correctly.

Your are browsing
the Archives of My Beloved PHP in the 'Apache' Category.