Outils pour utilisateurs

Outils du site


php:snippets

php snippets

Here's a bunch of php code examples and snippets.

MD5 in base64

The standard MD5 function of php provides a hex (base 16) output, but you cant get the base64 version like this:

echo base64_encode(pack('H*',hash ('md5',$data)));

Force gzip encoding

Here is a solution to force gzip (compression) of data on a per-page basis. This can be interesting if you don't want to enable gzip compression on your whole server/directory, if you can't use .htaccess files, or if you webserver does not support gzip compression. The other advantage is that you can add HTTP headers even if you have started sending data to the client.

Put at the beginning of your script:

if(!ob_start("ob_gzhandler")) ob_start();

If the client does not support gz compression, it will serve non-compressed data. You do not even need to enable mod_gzip on the server. Note that you don't have to call explicitely ob_end_flush(): This will be done automatically by php.

gz compression may cause problems in IE6, but IE7/8/9 and other browsers are ok.

You can compress html, plain text, jSon and other (This can be used in webservices called by Ajax code, which is very efficient because they typically return very redundant data). I do not recommend compressing PNG, JPEG or images.

php/snippets.txt · Dernière modification : 2014/07/12 11:26 de 127.0.0.1