Easy PHP Random Quote Script On Your Website

You can lively up your website by putting a random quote php script which displays random joke, fact, anecdote, tip or anything else you want. All you need is only 1 .txt file and 1 line of php code. No database or php knowledge required! Sites with random quotes are more interesting to your visitors because every time they visit your site they see a catchy text block. Sometimes they even reload page to see some more jokes or facts. This increases page impressions if you want that to be. So let's begin building.
  • At first you need to create .txt file. To do that open Notepad from your computer's start menu. Write or paste as many jokes, quotes or anything else as you want. IMPORTANT! Don't press enter unless you want to start writing new piece of text. That's because pressing Enter key puts an invisible sign in the .txt document and php script recognize it as new information block. Once you've created your .txt file save it with a name of text.txt on your desktop.
  • Now you only need to paste this php code

    <?php $file = file("text.txt"); $line = rand(0, sizeof($file)-1); echo $file[$line]; ?>

    inside your page anywhere between body tag
  • That's it! Save your webpage and be sure it ends up with .php extension. If your page was .htm or .html change it to .php. Don't worry it won't alter your previous content. The php extension only says to server that this page contains php code.

Notes: In order the script to work both files (text.txt and youpage.php) must be at the same location. Also you must have a working web server and php enabled if you test this on your computer. Most hostings will have no problems displaying this script, however free hostings (like geocities) doesn't provide the ability to run php scripts that read from external files. You can modify your text.txt file at any time and add new quotes.

No comments: