Microformat Abuse for improved CTR , 9.9 out of 10 based on 99 ratings

Monday, October 24, 2011

PHP image output and browser caching

Recently I was involved in a project that returned graphic thumbnails in response to a remote php script call. One of the optimisations done was to have browser caching of returned images to save on our bandwidth. I will share the code we used to do this.

The code is pretty simple, and borrows heavily from code pasted by mandor at mandor dot net for the PHP header function. We generalised it for different graphic file types and a second function so it works when PHP is an Apache module or cgi.

The first function displayGraphicFile is to return a graphic file. The function does assume the file exists.

// Return the requested graphic file to the browser // or a 304 code to use the cached browser copy function displayGraphicFile ($graphicFileName, $fileType='jpeg') {   $fileModTime = filemtime($graphicFileName);   // Getting headers sent by the client.   $headers = getRequestHeaders();   // Checking if the client is validating his cache and if it is current.   if (isset($headers['If-Modified-Since']) && (strtotime($headers['If-Modified-Since']) == $fileModTime)) {      // Client's cache IS current, so we just respond '304 Not Modified'.     header('Last-Modified: '.gmdate('D, d M Y H:i:s', $fileModTime).' GMT', true, 304);   } else {     // Image not cached or cache outdated, we respond '200 OK' and output the image.     header('Last-Modified: '.gmdate('D, d M Y H:i:s', $fileModTime).' GMT', true, 200);     header('Content-type: image/'.$fileType);     header('Content-transfer-encoding: binary');     header('Content-length: '.filesize($graphicFileName));     readfile($graphicFileName);   } } 

The second function to get the header request details. We specifically require the ‘If-Modified-Since’ header.

// return the browser request header // use built in apache ftn when PHP built as module, // or query $_SERVER when cgi function getRequestHeaders() {   if (function_exists("apache_request_headers")) {     if($headers = apache_request_headers()) {       return $headers;      }   }   $headers = array();   // Grab the IF_MODIFIED_SINCE header   if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {     $headers['If-Modified-Since'] = $_SERVER['HTTP_IF_MODIFIED_SINCE'];   }   return $headers; } 

Switching between PHP versions with Apache on Windows

I had occasion to set up a dev box to test a new web application against different php versions, so I thought I would share the steps. This setup is for one PHP version at a time, and I have documented for 2 versions, but more can be done if required. Also I have used PHP4 and 5, but it could be multiple versions of PHP5, such as a beta version of 5.3. This setup was done using Apache2 on Windows.

We will create the php4 part first and when it up and running we will do php5.

  • Have Apache installed and tested it is running correctly.
  • Create a directory such as C:\php
  • Unzip a version of php (example php4.4.9 the zipped binary version, not the one with the installer), into a subdirectory of c:\php.
  • Rename php.ini-recommended to php.ini
  • Copy php4apache2.dll from the sapi subdirectory to c:\php\php4
  • Open the httpd.conf file, and add the following at the end:
    AddType application/x-httpd-php .php # Choice between php versions   LoadModule php4_module "C:/php/php4/php4apache2.dll" # configure the path to php.ini PHPIniDir "C:/php/php4"  
  • (Optional) Search for DirectoryIndex. Add index.php after index.html, or before if you want index.php to be the default over index.html.
  • If the Apache service is running, stop it. In properties set it startup type to ‘Manual’. We are going to delete this later.
  • Open up a command prompt and navigate to the apache bin directory. [Default is C:\Program Files\Apache Group\Apache2\bin]
  • Type the following:
    apache.exe -k install -n Apache_php4 -D php4
  • If you refresh the services list, then there will be one named ‘Apache_php4′.
  • Start this new service. If all is well, then there will be 2 messages. 1 saying the service is starting and another it has started.
  • To test this, create a file in the Apache htdocs directory named test.php. In it have the line:
  • In a web browser type http://localhost/test.php. If this has worked you will get info about your php install and can continue with the 2nd php install. If not go back through the above steps.

Now the php5 part.

  • Unzip the other version of php (eg. 5.2.9) into a subdirectory of c:\php
  • Rename php.ini-recommended to php.ini
  • Open the httpd.conf file, and add the following at the end:
     LoadModule php5_module "C:/php/php5/php5apache2.dll" # configure the path to php.ini PHPIniDir "C:/php/php5" 
  • You will now do steps like above when you created and tested the apache service, but this time use php5
  • To delete the original apache service use something like:
    apache -k uninstall -n Apache2 

That is it, you will have 2 versions of PHP installed that you can switch between. Just stop 1 service and start the other. This could also be done using a batch file.

Related posts:

  1. XSS and SQL Injection PHP Code Scanner
  2. Introduction to PHP with Xdebug
  3. Windows within Kubuntu with qemu
  4. The Big List of PHP Frameworks
  5. PHP image output and browser caching