PHP

Posts about the server side scripting language PHP

Function To Delete Temporary Files

If you allow users to upload data to your site you might have a situation where a data directory might be full of temporary files. In the long term you will want to get rid of these files as they have served their purpose and are no longer needed.

Here is a function that can be used to delete any files in a directory that were created more than 20 minutes ago. It uses the glob() function to find all files of a particular type and then uses the filectime() function to figure out when the file was last modified (or created). It will then delete (unlink) any files that were created more than 20 minutes ago.

Day Of The Week Algorithm By Lewis Carroll In PHP

Lewis Carroll devised a mechanism to work out the day of the week given a particular date and was published in Science in 1887. Here is a PHP function that works out the day of the week given the date that uses the same mechanism that Lewis Carroll devised. The mechanism isn't very complicated, but rather than explain it twice I have just put a lot of comments in the code to indicate what is happening.

Convert Time Into Timestamp Or Timestamp Into Time In PHP

The easiest (and most reliable) way to store the time in a database table is with a timestamp. It is also the most convenient way of working out time scales as you don't have to do calculations in base 60. In MySQL this is accomplished by the UNIXTIME() function, which can be reversed by using another MySQL function called FROM_UNIXTIME().

However, you can sometimes be left with timestamps in your code and the task of trying to figure out what to do with them.

The first problem is trying to convert a timestamp into a date. So here is a PHP function that does this.

Banner Advert Rotation With PHP

A normal procedure with banner adverts is to rotate them randomly, but also to display some more than others. Lets say that you had 3 banner adds and that you wanted to display them at different rates. To do this you can use the rand() function to generate a random number that can then be used to see what banner advert will be displayed.

First we generate the random number, in our case it is between 1 and 100.

$number = rand(1, 100);

You can then use this number to figure out what banner ad to display. Because the random number is between 1 and 100 it is basically a percentage. So you can create a simple if statement that will determine what banner should be displayed.

Obfusticating PHP Code

You will sometimes want to make sure that your code is a better hidden from the end user. For example, you might want to make sure that your database password files are completely hidden from prying eyes so that even if your web server is hacked your database server isn't also compromised.

Take the following code, which prints out "Hello world".

echo "Hello world";

You can encode this into meaningless text by using the base64_encode() function.

$code = base64_encode('echo "Hello world";');

This turns the $code variable into the following.

ZWNobyAiSGVsbG8gd29ybGQiOw==

Which can be run again by using the bade64_decode() function in conjunction with the eval() function.

Create A Web Colour Pallette With PHP

Use the following bit of code to create a web safe colour table. In order to for the name of each colour to be displayed the background colour array is reversed and used to create the foreground colours. This makes white text appear on black backgrounds and visa-versa, the only problem is that in the middle of the table it will display grey on grey.

Mask Email With ASCII Character Codes In PHP

Hiding your email address in an image is the best way of encrypting your email, but if your server doesn't support the GD2 library, or if you don't want to use it, then you might want to look at a different way of doing this.

The easiest way to encrypt your email address is to turn every character into the ASCII code equivalent and use this to display the text in HTML by putting a &# in front of each character. Here is a function that takes a string and turns it into HTML encoded text.

function maskEmail($email) {
 $hiddenEmail = '';
 $length = strlen($email);    
 
 for ($i = 0;$i < $length; $i++) {
  $hiddenEmail .= "&#".ord($email[$i]).";";
 }
 
 return $hiddenEmail;
}

To use this just feed an email address into it.

Convert HTML To ASCII With PHP

The reverse of turning ASCII text into HTML is to convert HTML into ASCII. And to this end here is a little function that does this.

Turning ASCII Text Into HTML With PHP

Providing a text box for users to type information in is very common, but usually people want to include line breaks and links with the text and they expect the site to lay it out just as they had intended it. The following function will turn any ASCII text string into the approximate HTML equivalent.

PHP Function To Turn Integer To Roman Numerals

Use the following function to change any integer into a string containing the integer value as a Roman Numeral.