Page 1 of 1

Very simple script - but it isn't working. Help please!

Posted: Sun Dec 28, 2014 3:57 pm
by ally1205
Hi
The script I am attempting to use is a daily quote display script. It doesn't use mysql; it uses a simple text file list instead, which I like because it is easy to upload new lists of quotes for it to display. It uses a cron job to execute the main php script which takes one line (selected at random) from the quotes list file (quotesData.txt) and writes it into another text file (quoteToday.txt). A small script on my index.php is then supposed to display the content of the quoteToday.txt when someone goes to the URL. However the simple piece of scrip in my index.php file is not working. It worked ONCE, when I first installed the scripts, but never since. Here is my actual index.php content:
--------------------------

Code: Select all

<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
	
	<title></title>
</head>
<body>

<p></p>

<p>"; ?></p>

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

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

  echo "<p-->
<p>$string</p>

<p></p>
</body>
</html>
------------------------
I am obviously missing something, but what? I have to confess I am not familiar with PHP as I have only dabbled in PERL in the past. I set the permission of quoteToday.txt to 646. the script, for test purposes, is installed at: http://www.aalife.com/index.php
I can confirm that PHP is installed on my host's server (/usr/bin/php)<-Should this be stated in the above script? The script was found here: http://www.blazonry.com/php/quoteoftheday.php
However, the developer is long gone!
Thank you for your help.

Ally

Re: Very simple script - but it isn't working. Help please!

Posted: Sun Dec 28, 2014 6:22 pm
by Christopher
You PHP tags were mangled -- probably from your HTML editor. It should be something like:

Code: Select all

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<title></title>
</head>
<body>
<p></p>
<?php
  // display quote of the day
  $file = "quoteToday.txt";

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

  echo "<p>$string</p>";
?>
<p></p>
</body>
</html>
The simplest might be something like:

Code: Select all

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<title></title>
</head>
<body>
<p></p>
<p><?php readfile("quoteToday.txt"); ?></p>
<p></p>
</body>
</html>

Re: Very simple script - but it isn't working. Help please!

Posted: Sun Dec 28, 2014 8:20 pm
by ally1205
Hi Christopher,
That's fantastic! I've been puzzling over this for days! Thank you!! Your simplified version works perfectly. I wonder why the author made it any more complicated than that.

Anyway, that's half of the problem solved. The other issue is that Cron is not executing the other php script that changes the quote. So I guess that script has issues too. Would you (or someone else) be kind enough to have a look at this too? Here is what it contains:

Code: Select all

<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
	
	http://www.blazonry.com/php/quoteoftheday.php

   This file runs on the server. Save the file as "generateQuote.php".
   
   Set up a cron job to run this file. It randomly selects a quote and 
   saves it to a text file.
   
*****************************************************************************/

	$file = "quoteToday.txt";
	// read the file into an array
	$quoteArray = file("quoteData.txt");

	// set random seed
	//srand($seed);

	//get the random number
	$qrnd = rand(0,sizeof($quoteArray)-1);

	// pick quote name from random numbers
	$quote = "$quoteArray[$qrnd]";


	//write the string to the file

	// Note: mode 'w' opens for writing only; place the file pointer at 
	//   the beginning of the file and truncate the file to zero length.
	//   If the file does not exist, attempt to create it.
	
	$fh = fopen($file, "w");
	fputs($fh, $quote);
	fclose($fh);

	//uncomment the following line for debugging and testing
	echo ("wrote \"$quote\" <br />to the file $file");

?>
Cron fails to run the script and instead sends me an email containing the contents of the script.

Many thanks again,
Ally

Re: Very simple script - but it isn't working. Help please!

Posted: Mon Dec 29, 2014 3:01 pm
by Christopher
Again, looks like your HTML editor mangled the starting PHP tag. It should probably look like this:

Code: Select all

<?php
/*****************************************************************************

	http://www.blazonry.com/php/quoteoftheday.php

   This file runs on the server. Save the file as "generateQuote.php".
   
   Set up a cron job to run this file. It randomly selects a quote and 
   saves it to a text file.
   
*****************************************************************************/

	$file = "quoteToday.txt";
	// read the file into an array
	$quoteArray = file("quoteData.txt");

	// set random seed
	//srand($seed);

	//get the random number
	$qrnd = rand(0,sizeof($quoteArray)-1);

	// pick quote name from random numbers
	$quote = "$quoteArray[$qrnd]";


	//write the string to the file

	// Note: mode 'w' opens for writing only; place the file pointer at 
	//   the beginning of the file and truncate the file to zero length.
	//   If the file does not exist, attempt to create it.
	
	$fh = fopen($file, "w");
	fputs($fh, $quote);
	fclose($fh);

	//uncomment the following line for debugging and testing
	echo ("wrote \"$quote\" <br />to the file $file");
Which could be simplified to:

Code: Select all

<?php
/*****************************************************************************

	http://www.blazonry.com/php/quoteoftheday.php

   This file runs on the server. Save the file as "generateQuote.php".
   
   Set up a cron job to run this file. It randomly selects a quote and 
   saves it to a text file.
   
*****************************************************************************/

	$file = "quoteToday.txt";
	// read the file into an array
	$quoteArray = file("quoteData.txt");

	//get the random number
	$qrnd = rand(0,sizeof($quoteArray)-1);

	file_put_contents($file, $quoteArray[$qrnd]);

	//uncomment the following line for debugging and testing
	echo ("wrote \"{$quoteArray[$qrnd]}\" <br />to the file $file");

Re: Very simple script - but it isn't working. Help please!

Posted: Tue Dec 30, 2014 12:40 pm
by ally1205
Utterly Fantastic!!! Thanks a MILLION! At last this thing is working!

You've inspired me to re-educate myself! My past experience with PERL is pretty faded, and PHP seems far more popular now. I've just acquired "PHP and MySQL for Dynamic Web Sites (Visual QuickProject Guides)" by Ullman (2004) (Dated, yes, but I guess it'll mostly still apply, won't it? I guess I should at least learn the basics, because there are other things I need to build into my websites such as surveys, autoresponding, etc.

Thanks again, Christopher,
Ally