This script can be used for displaying a randomly selected quote from a given list of quotes. The quote changes every day. New quotes can be added.
See the quotes-example.php for a working example of the script. Make sure you chmod 777 quotes.txt.
quotes.php
<?php
/*
Function
* To display a randomly selected Quote for the day
How to use
* Create a file called 'quotes.txt' and chmod it 777
* Include the file [Eg: include 'quotes.php';]
* call the function showQuote where you want to show the quote [Eg: showQuote();]
*/
/*
* To add more insert
$quote[] = "Quote --Name"
*/
$quotes[] ="The value of a man resides in what he gives and not in what he is capable receiving. --Albert Einstein";
$quotes[] ="To cease smoking is the easiest thing I ever did. I ought to know, I've done it a thousand times.--Mark Twain";
$quotes[] ="Let us be thankful for the fools. But for them the rest of us could not succeed.--Mark Twain";
$quotes[] ="Suppose you were an idiot... And suppose you were a member of Congress... But I repeat myself.--Mark Twain";
$quotes[] ="There are several good protections against temptation, but the surest is cowardice.--Mark Twain";
$quotes[] ="The monotony and solitude of a quiet life stimulates the creative mind. --Albert Einstein";
$quotes[] ="I love to travel, But hate to arrive --Albert Einstein";
$quotes[] ="Do you believe in immortality? No, and one life is enough for me --Albert Einstein";
$file = "quotes.txt";
function showQuote(){
global $file,$quotes;
if(file_exists($file)){
$entry = file($file);
$entry = split("\|",$entry[0]);
}
// Check the files have been updated today. If not update it
if($entry[0] != date('j')){
$idx = _newQuote();
}else{
$idx = $entry[1];
}
$quote = split('--',$quotes[$idx]);
echo "<div class='quote'>$quote[0]<span class='author'>$quote[1]</span></div>";
}
function _newQuote(){
global $file, $quotes;
$randomid = mt_rand(0,count($quotes)-1);
$content = date('j') . "|$randomid";
// If $file doesnot exist attempt to create
if(!file_exists($file)) touch($file);
// Check whether the file is writable or not
if (is_writable($file)){
if (!$handle = @fopen($file, 'w'))echo "<b>Error</b>: Could not open the file<br>";
elseif (@fwrite($handle, $content) === FALSE)echo "<b>Error</b>: Could not write to the file<br>";
else{
fclose($handle);
return $randomid;
}
}else echo "<b>Error</b>: $file is not writable<br>";
}
?>
Usage Example
<?php
/*
This is an example on how to use the code.
Make sure that you have made the file quote.txt writable (777)
*/
/* Add this at the top of the page. */
include 'quotes.php';
?>
<!doctype html public "-//w3c//dtd html 4.1 transitional//en">
<html>
<head>
<title>Thought For The Day</title>
<meta name="Keywords" content="">
<meta name="Description" content="">
<meta name="MSSmarttagsPreventParsing" content="TRUE" />
<meta name="robots" content="index,follow" />
<script type="text/javascript" language="JavaScript">
<!--
if (top.location != location)top.location.href = document.location.href;
//--></script>
<style>
body, table{font:normal 12px Verdana, Arial, sans-serif;}
.quote{font:bold 12px Arial;}
.quote .author{font-weight:normal;font-style:italic;display:block;float:right;}
</style>
</head>