PHP Logo

Display A String By A Date Value With PHP

23rd May 2008 - 3 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.

quote 1
quote 2
quote 3

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

Read the full article

HTML Logo

HTML Meta Refresh

22nd May 2008 - 2 minutes 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.

<meta http-equiv="refresh" content="5;url=http://www.google.com" />

 

Read the full article

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.

$colour = rand(0, 10000000);
$colour = dechex($colour);

This can also be accomplished on a single line.

$colour = dechex(rand(0, 10000000));

Read the full article

PHP Logo

Blocking Multiple IP Addresses With PHP

20th May 2008 - 3 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.

Read the full article

Apache HTTP Logo

Redirect One Directory To Another With .htaccess

19th May 2008 - 2 minutes read time

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

RewriteEngine on
RewriteBase /
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.

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

Read the full article

PHP Logo

Function To Darken A Colour With PHP

18th May 2008 - 2 minutes read time

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

function ColorDarken($color, $dif=20)
{
 $color = str_replace('#','', $color);
 $rgb = '';
 if (strlen($color) != 6) {
  // reduce the default amount a little
  $dif = ($dif==20)?$dif/10:$dif;
  for ($x = 0; $x < 3; $x++) {
   $c = hexdec(substr($color,(1*$x),1)) - $dif;
   $c = ($c < 0) ? 0 : dechex($c);
   $rgb .= $c;
  }
 } else {
  for ($x = 0; $x < 3; $x++) {
   $c = hexdec(substr($color, (2*$x),2)) - $dif;
   $c = ($c < 0) ? 0 : dechex($c);
   $rgb .= (strlen($c) < 2) ? '0'.$c : $c;
  }
 }
 return '#'.$rgb;
}

Here are some examples of use.

echo ColorDarken('#123456'); // #002042
echo ColorDarken('#666'); // #444
echo ColorDarken('#ffffff'); // #ebebeb
echo ColorDarken('#ffffff',1); // #eeeeee

Read the full article

PHP Logo

Split An Array Into Smaller Parts In PHP

17th May 2008 - 3 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.


function sectionArray($array, $step)
{
 $sectioned = array();
 
 $k = 0;
 for ( $i=0;$i < count($array); $i++ ) {
  if ( !($i % $step) ) {
   $k++;
  }
  $sectioned[$k][] = $array[$i];
 }
 return $sectioned;
}

Run the function by passing it an array, in this case I am going to split the alphabet into 5 arrays of 5 letters.


$array = range('a','z'); // create an array from a to z
 
echo '<pre>'.print_r(ArraySplitIntoParts_Shorter($array,5),true).'</pre>';

This produces the following output.

Read the full article

PHP Logo

Robust Domain Whois Query In PHP

16th May 2008 - 5 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.

In order to get this information in PHP you will need to ask the appropriate TLD whois server for information about the domain. But where do you get a list of the servers? Well the root zone database has a comprehensive list of domains, and by clicking on the TLD links you can see whois information at the bottom of the page.

Read the full article

PHP Logo

Finding The Current File Or Directory With PHP

15th May 2008 - 5 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

Read the full article

PHP Logo

Rot13 Function In PHP

14th May 2008 - 3 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.

Read the full article