web blazonry web development scripts and tutorials Get best price on Samsung Galaxy S5, Copper Gold 16GB (Verizon Wireless)
   PHP       Name Generators       Perl       CSS       Javascript       Java       MySql       How Tos       Resources   




PHP Name Generators
  Wu-Name
  U.S. Name
  Baby Name

Bookmark and Share




PHP Script: Quote of the Day

This free PHP script for "Quote of the Day" automatically updates your web site with a randomly selected quote. It is easy to set up and customize, and does not require a MySQL database. This tutorial explains how it is programmed and provides download for all source code.

The nature of the content and the frequency of change are flexible. "Quote of the Day" can be used for "joke of the hour," "thought of the week," "blog of the month," "product of the fortnight," "web site of the season," etc.

See the examples of the script in use for a funny quote of the day and a blog of the day.


Tutorial for Quote of the Day PHP Script

My first thought was to use PHP and MySQL. A PHP date function would key off the current date and select the same quote throughout the day. The next day, it would randomly select a new quote. I thought about using MySQL to store the quotes and author data and use PHP to dynamically display the day's quote.

However, this approach has too much overhead. Sometimes we are so in love with the idea of dynamic content that we forget we can use a solution with less overhead.

The problem with dynamic content in this case is that every visitor to the page would run the date function, "hit" the database to get the quote, then programmatically insert the quote into the HTML page.

This sequence would occur every time the page was generated. That is alot of overhead for a small piece of content many times per day. It's even worse when you use the same quote on different pages of a site (for example, the home page and an internal page on the same site).

Instead of MySQL, I decided to use a simple text file to store the data. Instead of programatically identifying today's quote every page view, I have the server do the work one time per day with a cron job.

The cron job runs a PHP script that randomly selects a new quote and writes the quote into a text file. The script on the PHP web page that site visitors see reads the file and inserts the contents into the page.

Although I am reading and inserting the contents of a text file every time the page is hit, the file with a single quote is relatively small. There are server load and memory issues with file operations on text files, but I'll save that discussion for another tutorial. This solution is also simple to set up and maintain.


Source Code Files for Quote of the Day PHP Script

There are 4 files for "Quote of the Day"

  • data file with all the quotes (quoteData.txt)
  • current day's quote (quoteToday.txt)
  • web page that displays the quote
  • PHP script the cron job runs daily (quoteGenerate.php)


Data File With the Quotes (quoteData.txt)

The file that contains all the quotes is a simple text file. One line for each quote and related data such as author.. The file can include HTML tags for formatting if you wish. Just be sure each line (that is, each quote) has the formatting you want.

Here is sample quoteData.txt file. This file has 10 clichés that provide you data to work with.


Current Day's Quote (quoteToday.txt)

This file contains only one quote, the quote displayed that day. This file is updated daily by your server. Create the file and make it writable. Change permissions (CHMOD) to "646" so the server can save the changes. (646 permissions look like -rw-r--rw-).

Here is sample quoteToday.txt file (today's quote) to work with. The file has one line, the content that is displayed that day.


Web Page That Displays the Quote

Your web page where you display the quote contains the PHP code that opens, reads and inserts the contents of the days quote.

Add the following to your web page where you want to display the quote:

<?php
  // display quote of the day
  $file = "todaysQuote.txt";

  $fh = fopen($file, "r");
  $string = fread($fh, filesize($file));
  fclose($fh);

  echo "<p>$string</p>";
?>

Name the file whatever you want.


File the Cron Job Runs (quoteGenerate.php)

Here is quoteGenerate.php to download. Save the file with a "PHP" extension. Upload the file to your web server and set up your cron job. For help with setting up cron jobs, see wikipedia.


Fixing Common Problems

Upload all 4 files to your web server.

Check that paths to all files are correct. For simplicity, I assume they are all in the same directory. During debug and testing on localhost, check your Apache error logs to see if you have a problem with your path, (See the blazonry home page for a tip with more details about checking your error logs to figure out problems with your code.)

quoteToday.txt must be writable on the server by "other" (CHMOD to "646")

You can quickly see only the contents of your today's quote file (quoteToday.txt) by displaying it in your browser. (For example, http://localhost/quoteToday.txt)



 

Newest Pages
Test Google Ads Ver. 2
Free Linux Admin Books
Free Linux Books for Programmers
Free Books for Linux on the Desktop
Free PHP Books
Free JavaScript Books
Free Java Books - Advanced
Free Java Books - Basic
Free Perl Books
Free Python Books
Quote of the Day (PHP)
Debugging Part 2
How to Test Google Ads
Most Popular Pages
Baby Name Generator
U.S. Name Generator
Wu Name Generator
Popup Windows (JavaScript)
Upload and Resize an Image (PHP)
How To Install Apache + PHP + MySQL
Intro to Web Databases (PHP, MySQL)

Least Popular Pages
iNews Applet (Java)
Java Resources
Site Monitor (Perl)
PHP Resources
 
 

  privacy policy     ||     © 1997-2016. astonishinc.com   All Rights Reserved.