Very Simple PHP Visit Counter

26th May 2008

To create a simple PHP visit counter you will need to create a plain blank text file called counter.txt.

Put the following two functions into a file and include it at the top of any page that you want to have counted.

List All Files In A Directory By Creation Date

25th May 2008

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.

Display A String By A Date Value With PHP

23rd May 2008

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.

HTML Meta Refresh

22nd May 2008

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.

 

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

Generate A Random Colour With PHP

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.

philipnorton42 Wed, 05/21/2008 - 08:35

Blocking Multiple IP Addresses With PHP

20th May 2008

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.

Function To Darken A Colour With PHP

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

philipnorton42 Sun, 05/18/2008 - 15:32

Split An Array Into Smaller Parts In PHP

17th May 2008

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.