Simple PHP caching improved

There are many tools around that will help cache your web pages to increase speed and performance (most notably Memcached) however these usually require installation on your web server, thus rendering them useless for people who use shared hosting and don't have access to the server.

Using some simple PHP code we can quite effectively cache a webpage, useful for sites that are database driven and have a large amount of queries or high volumes of traffic.

First we need to setup the folder where the cached files are stored, making sure it has read/write permissions (CHMOD to 0777 - see your FTP client documentation on how to do this).

Next create a file called cache.php which contains the following code:

Read more »

Flintstone - A key/value database store using flat files for PHP

XEWeb are proud to announce the release of Flintstone, a key/value database store using flat files for PHP.

Some of the features include:

  • Memory efficient
  • File locking
  • Caching
  • Gzip compression
  • Easy to use

All it requires is PHP 5 and read/write file permissions.

Take a look at example usage below:

<?php

try {

    // Load flintstone
    $db = new Flintstone(array('dir' => '/path/to/database/dir/'));

    // Set keys
    $db->load('users')->set('bob', array('email' => 'bob@site.com', 'password' => '123456'));
    $db->load('users')->set('joe', array('email' => 'joe@site.com', 'password' => 'test'));
    $db->load('settings')->set('site_offline', 1);
    $db->load('settings')->set('site_back', '3 days');

    // Retrieve keys
    $user = $db->load('users')->get('bob');
    echo 'Bob, your email is ' . $user['email'];

    $offline = $db->load('settings')->get('site_offline');
    if ($offline == 1) {
        echo 'Sorry, the website is offline<br>';
        echo 'We will be back in ' . $db->load('settings')->get('site_back');
    }

    // Delete a key
    $db->load('users')->delete('joe');

    // Flush database
    $db->load('users')->flush();
}
catch (Exception $e) {
    echo 'Exception: ' . $e->getMessage();
}

For more information and to download visit this page.

Generate a random string A-Z, 0-9 in PHP

Often I find that generating a random string can be tedious, there seem to be so many ways to do this.

I wrote this simple function to do the work, and it can be easily modified.

<?php
/*
 * Create a random string
 * @author	XEWeb <>
 * @param $length the length of the string to create
 * @return $str the string
 */
function randomString($length = 6) {
	$str = "";
	$characters = array_merge(range('A','Z'), range('a','z'), range('0','9'));
	$max = count($characters) - 1;
	for ($i = 0; $i < $length; $i++) {
		$rand = mt_rand(0, $max);
		$str .= $characters[$rand];
	}
	return $str;
}

You can remove any of the ranges in $characters (for example if you didn't want uppercase letters delete the range('A','Z')) or put in your own array of characters.

Bash commands to detect script injections and malware

Not so long ago this site and other domains hosted on my server were injected with malware PHP scripts that caused all sorts of damage, including amending javascript files to display ads to people who visited my sites.

These 2 bash commands saved my life and I would like to share them with the world.

The first one will find any javascript file that contains the string "eval(unescape" which is the most common way of injecting malicious code. The second is a similar method for PHP files.

find . -name "*.js" | xargs grep -l "eval(unescape"
find . -name "*.php" | xargs grep -l "eval(base64_decode"

Seek and destroy!

Thumbnail opacity hover with CSS and jQuery

A nice effect when hovering over thumbnails is to change the opacity, so that it has that fade in and out effect.

<ul class="thumbnails">
 <li><a href="#"></a></li>
 <li><a href="#"></a></li>
 <li><a href="#"></a></li>
 <li><a href="#"></a></li>
 <li><a href="#"></a></li>
</ul>

Take the above example, a simple list of images. Let's apply the basic styling for it...

ul.thumbnails { list-style: none; margin: 0; padding: 0; }
ul.thumbnails li { float: left; margin: 10px; width: 50px; height: 50px; }
ul.thumbnails li img { filter: alpha(opacity=60); /* IE only */ opacity: .60; /* All other browsers */ }

As you can see all of the thumbnails will have a default opacity of 60%. Now comes the jQuery script to add the fade effect...

$(document).ready(function(){
	$('ul.thumbnails li').hover(function(){
		$('img', this).stop().animate({opacity: 1});
	}, function() {
		$('img', this).stop().animate({opacity: 0.6});
	});
});

And that's all there is to it!