PHP Logo

Finding The Current File Or Directory With PHP

15th May 2008 - 3 minutes read time

Having a header file that prints out a standard menu on a site is a good idea and saves you time in the long run as you only have to edit one file to change an item on the menu. However, what if you only want to display a menu or sub-menu when a particular page is loaded? This is a common problem, and finding out what page you are on is something that all PHP programmer come across at some point or another.

The PHP $_SERVER superglobal array has three items of interest which can be used to find out the current page. These are PHP_SELF,REQUEST_URI and SCRIPT_NAME and they all appear to have the same values but there are some subtle and important differences. Here are some examples of their values (on the right) with the original URL (on the left).

PHP_SELF

PHP Logo

Rot13 Function In PHP

14th May 2008 - 2 minutes read time

Rot13 (which stands for "rotate by 13 places") is a name given to a simple encoding algorithm (or substitution cipher) that is used to mask text. It works by making each letter 13 spaces further along in the alphabet so that a becomes n and b becomes o. For the letter n the alphabet starts again from the beginning.

The cipher can be used both ways so that any string encoded with the function can then be easily decoded with the same function. For this reason it is a very poor mechanism of encoding, but can be used if you want to mask some text but are not concerned about people reading it. It is commonly used on forums in order to hide spoilers and solutions from readers who don't want to see them.

Here is the function.

PHP Logo

Function To Delete Temporary Files

13th May 2008 - 2 minutes read time

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.

PHP Logo

Day Of The Week Algorithm By Lewis Carroll In PHP

12th May 2008 - 4 minutes read time

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.

Script.aculo.us - Web 2.0 Javascript

11th May 2008 - 2 minutes read time

Script.aculo.us is a JavaScript framework that allows you to add effects and advanced JavaScript features to any web page. Although Script.aculo.us can be a little slower than Mootools on occasion it makes up for this by having more stuff built into the library components. For example, the slider control is available of both frameworks, but the Script.aculo.us version comes with some handy features (like filling one part of the slider) where the Mootools version doesn't.

PHP Logo

Convert Time Into Timestamp Or Timestamp Into Time In PHP

10th May 2008 - 2 minutes read time

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.

Linux Logo

Delete File By inode Reference

9th May 2008 - 1 minute read time

If you want to delete a file that you can't type in the name of either because the name is long and complicated, or because it is difficult to type in without causing a syntax error then here is the solution.

You first need to find the inode reference of the file. This can be done by using the command ls -li. The start of each line has a number that is specific to that file. You could use the command ls -i , but the output is a little confusing.

To delete the file use the find command with the flag -inum, followed by a pipe into the rm (remove file) command like the following.

find . -inum 916618 | xargs rm

The xargs bit is used to pass a list of the files found from the find command to the rm command.

JavaScript Logo

Linked HTML Drop Down Menus With JavaScript

8th May 2008 - 2 minutes read time

A good usability feature of web forms is to fill the contents of one drop down menu, depending on what has been selected in a previous menu. This can be done quite easily with JavaScript.

Take the following form, in this instance I have used some server variables used in mod_rewrite, but the idea is valid.

WordPress Logo

Fixing Wordpress Scheduled Posts

7th May 2008 - 4 minutes read time

Wordpress has a neat little feature that allows you to write a post and then schedule it to display at some point in the future. This seems good, but it invariably doesn't work on some server platforms and rather than publishing a post Wordpress just counts the amount of time passed since it was supposed to go live. The basic solution to this is to go into the post and click on publish, which can be a pain if you are taking a couple of days off from blogging and want to leave it running.

The problem lies with the functions that convert a scheduled post into a live post which are kept in the file wp-cron.php in the root Wordpress directory. For some reason the Wordpress developers decided to call the scheduling functions using the fsockopen() function available in PHP. This essentially opens a browser session to the wp-cron.php file, just as you would if you browsed to the location using your web browser.

JavaScript Logo

Check Or Uncheck All Items In A Checklist With JavaScript

6th May 2008 - 2 minutes read time

If you have lots of check boxes in a row a handy little usability trick is to allow a user to click on a button and check all of the checkboxes at once. The following function will either check or uncheck all of the check boxes in your form.