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.
<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.
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.
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.
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.
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.
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.
The following function will reduce a hexadecimal colour string by a set value. It can take three and six digit colour values.
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.
A whois query will tell you some information about a domain name. Although not available as a default on Windows systems you can type:
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.