PHP scripts and tricks...

Static compile and install of apache + mod_ssl + php on FC4

Latest Compile with pdo drivers for mysql along with mod_security.

Remove the MySQL-shared rpm else openssl will not work.

# rpm -e MySQL-shared-5.0.20a-0.glibc23

Load your PHP pages faster with GZip compression

If you have heavy pages with lots of images and text, you can greatly improve its' performance by dynamically compressing the page with php.

Put the below code in the header of each php page.

<? ob_start("ob_gzhandler"); ?>

It automatically gzips the page for browsers that support it.

This could also be set in the php.ini file instead, so all php pages are automatically compressed without having to add the above code in each and every php page.

In php.ini add the below code:

output_handler = ob_gzhandler

How to fix _Can't open file: 'sessions.MYI'_ in Drupal

This seems to occur if mysql crashes for some reason and corrupts the sessions table for Drupal.
Here is a quick way to resolve this and I have used it in the past...

mysql> REPAIR TABLE sessions QUICK;

You can also use phpmyadmin to repair the table.

Related Reading: MySQL Database Repair

Compiling support for XSLT in PHP

XSLT, Extensible Stylesheet Language (XSL) Transformations is a language for transforming XML documents into other XML documents. It is a standard defined by The World Wide Web Consortium (W3C). Information about XSLT and related technologies can be found at

PHP XSLT extension only supports the Sablotron library from the Ginger Alliance. The extension does not come standard with Redhat Enterprise... and the compilation was a bit tricky. So if you are having difficulty, theses notes may help you some...

Listing image files in folders and subfolders with PHP

Here's a quick way to create a listing of images uploaded to a folder with php:

// file name: list_pics.php

global $startDir;

 * List Directories function, which transverses sub-folders
 * listing out the image files that exists within it.
function listDir( $path ) {
  global $startDir;
  $handle = opendir( $path );
  while (false !== ($file = readdir($handle))) {
    if( substr( $file, 0, 1 ) != '.' ) {
      if( is_dir( $path.'/'.$file ) ) {
        listDir( $path.'/'.$file );
      else {
        if( @getimagesize( $path.'/'.$file ) ) {

          // Uncomment if using with the below "pic.php" script to
          // encode the filename and protect from direct linking. 
          $url = 'http://domain.tld/images/pic.php?pic='
                 .urlencode( str_rot13( substr( $path, strlen( $startDir )+1 ).'/'.$file ) );

          substr( $path, strlen( $startDir )+1 ).'/'.$file;

          // You can customize the output to use img tag instead.
          echo "<a href='".$url."'>".$url."</a><br>";
  closedir( $handle );
} // End listDir function

$startDir = '.';
listDir( $startDir );


Generate a dynamic playlist of mp3 files in a folder with PHP

Below is a simple php script to generate a playlist of the mp3 files placed in a folder. I have tested it to work with XMMS media player on FC4 and should work with others too. Substitute the path and the url of the music folder.

// music_playlist.php

$folder_path = './music_folder';
$folder_url = 'http://domain.tld/music_folder/';

if ($handle = opendir($folder_path)) {
   while (false !== ($file = readdir($handle))) {
       if ($file != "." && $file != "..") {
           $song_list .= $folder_url.$file."\n";
else {
  echo "Music folder does not exist!";

// send header of audio/x-mpegurl content type
header('Content-type: audio/x-mpegurl');
if (preg_match("/MSIE 5.5/", $HTTP_USER_AGENT)) {
       header("Content-Disposition: filename=\"my_playlist.m3u\"");
else {
       header("Content-Disposition: inline; filename=\"my_playlist.m3u\"");  

echo "$song_list";


Related Reading: Listen to mp3 songs using XMMS in Redhat/Fedora

Sending form data to email via php

The below is a simple script that handles sending form data to email using php:


   // checks to see if the page that called this script was sent from the same host
      if (strpos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST'])>7 ||
         !strpos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST']))
         die("Bad referer");

      // begin building the message body
      $msg="Values submitted by the user:\n";
      foreach($_POST as $key => $val){
         if (is_array($val)){
            $msg.="Item: $key\n";
            foreach($val as $v){

PHP Accelerator

PHP Accelerator is an easily installed PHP Zend engine extension that provides a PHP cache, and is capable of delivering a substantial acceleration of PHP scripts without requiring any script changes, loss of dynamic content, or other application compromises.

  1. Installation:
    • Download PHPAccelerator.
    • Unpackage it and move contents to "/usr/local/phpaccelerator".
    • Include the below line to php.ini under dynamic extension section:

Get PHPwiki up and running with mysql

Ran into some difficulty installing the wiki with mysql backend as the documentation wasn't upto-date.

Below are some notes and customization done during the process:

  1. Download the latest tarball from SourceForge
  2. Extract all files.
  3. Copy 'config/config-dist.ini' to 'config/config.ini' and edit the settings.
  4. Make sure to set the value of 'DEBUG' to either 1 or 0 and do not comment it out. The install process complains if the DEBUG value is not found.
  5. Upload the "passencrypt.php" to the web-server and access it via browser to encrypt password.

Top 10 ways to crash PHP

Programming errors are of many kinds but the consequences can range from harmless to downright dangerous.
Top 10 ways to crash PHP
is an interesting article that deals with such a topic for PHP.

Syndicate content