PHP Logo

List All Files In A Directory By Creation Date

25th May 2008 - 2 minutes read time

The following function takes a path as an argument and produces an array of files ordered by their timestamp. The array values are the filename and the array keys are the timestamps. In order to prevent two files that have the same timestamp overwriting each other when the array keys are created with a random number. If two files where created at the same time the least that will happen is that they will swap places every time the array is created as the random number will be different.

JavaScript Logo

Assign Or Get Class Name Attribute With JavaScript

24th May 2008 - 2 minutes read time

To get the class of an element with JavaScript you use the className property of the element object. Take the following section of HTML code.

<div id="adiv" class="theClass">some text</div>

Use the following bit of code to print off the class name of the div element in a message box.

PHP Logo

Display A String By A Date Value With PHP

23rd May 2008 - 2 minutes read time

Printing off a random quote on a page is useful (or at least interesting), but it is nice to rotate them slower than every page view.

A better solution is to use a time based value to work out which quote to display. In this way the quote is changed every hour/day/week or whatever time period you have selected.

Create a file called quote.txt in the same directory as the script and put a single quote on each line.

  1. quote 1
  2. quote 2
  3. quote 3

The following function will take a time part as a single parameter and return a quote.

HTML Logo

HTML Meta Refresh

22nd May 2008 - 1 minute read time

To get a webpage to refresh every few seconds you can use a meta tag with the attribute http-equiv and a value of refresh. The number of seconds to delay can be put into the content attribute. This meta tag (as will all meta tags) goes into the head section of the document.

Here is an example that refreshes the page every 2 seconds.

<meta http-equiv="refresh" content="2" />

It is also possible to make the browser refresh to another page by including the string:

url=url or filename

Within the content attribute. Here is an example that redirects the page to google.com after a 5 second delay.

PHP Logo

Generate A Random Colour With PHP

21st May 2008 - 1 minute read time

Here is a short bit of code to generate a random hexadecimal colour using PHP. Essentially you just create a random number between 0 (000000) and 10,000,000 (ffffff) and turn this into a hexadecimal number using the PHP function dechex.

  1. $colour = rand(0, 10000000);
  2. $colour = dechex($colour);

This can also be accomplished on a single line.

PHP Logo

Blocking Multiple IP Addresses With PHP

20th May 2008 - 2 minutes read time

It is sometimes necessary to block people from using your site, dependent on their IP address. A users IP address can be detected by PHP using the $_SERVER superglobal and the parameter REMOTE_ADDR.

The code includes two ways to load the list of IP addresses. The first is by hard coding it into an array, and the second is by the use of a plain text file called "blocked_ips.txt". The format of this file is simply a list of IP addresses, with one address on each line. Through the use of the file() function this file is loaded as an array into of addresses.

Apache HTTP Logo

Redirect One Directory To Another With .htaccess

19th May 2008 - 1 minute read time

To stop access to a directory (and anything in that directory) all you need is a simple RewriteRule.

  1. RewriteEngine on
  2. RewriteBase /
  3. RewriteRule ^exampledirectory/(.*)$ / [R=301,L]

In this example, if this .htaccess file resides in the root directory of the site and you try to access anything within /exampledirectory you will be redirected back to the root folder. To redirect to another folder (like anotherdirectory) on your web server use the following rule.

PHP Logo

Function To Darken A Colour With PHP

18th May 2008 - 1 minute read time

The following function will reduce a hexadecimal colour string by a set value. It can take three and six digit colour values.

PHP Logo

Split An Array Into Smaller Parts In PHP

17th May 2008 - 2 minutes read time

Splitting an array into sections might be useful for setting up a calendar or pagination on a site. Either way there are numerous ways to do this but the following seems to be the quickest and most reliable method.

PHP Logo

Robust Domain Whois Query In PHP

16th May 2008 - 4 minutes read time

A whois query will tell you some information about a domain name. Although not available as a default on Windows systems you can type:

whois google.com

On most Linux installs and see some information about the google.com domain. What information you see depends on the domain you are looking at and the rules that the Top Level Domain (TLD) employs. For more information on whois you can take a look at the Wikipedia entry on the subject.