View Full Version : What's wrong with this piece of code?

03-07-2003, 01:12 PM
I have been trying to create a random Font Of The Day part on my site. I have managed to come up with this piece of code and have been told by others that it should work.

$filename = "/path/to/fotd.txt";
$contents = file($filename);

if ($contents['1'] <= (time()+86400)) {
$handle = fopen ($filename, "w");
$contents = fwrite ($handle, $number);
$contents = fwrite ($handle, time());
fclose ($handle);
} else {
$number = $contents['0'];

The fotd.txt has just 2 lines in and is laid out like this


1st line is fontid
2nd line is Unix Timestamp

The problem is, if you refresh the page the FOTD changes. I only require it to change every 24 hours. Can anyone help - or am I barking up the wrong tree?

Check it out here (http://www.font-factory.com)

03-07-2003, 01:38 PM
have you tried it without quoting the index value of $contents ?


as $contents['1'] would reference an associative array with index named '1' as a string ('0'=>1,'1'=>7849877436) as opposed to (1,784734796)

03-07-2003, 02:13 PM
Nope. Changed it. But nothing happened :(

03-10-2003, 05:34 PM
Is this the exact same code as you are using?
If so,
$filename = "/path/to/fotd.txt";

This must be changed to the actual server path of the script! it may look something like this when you are done:

$filename = "/home/domain/public_html/fotd.txt";

This will work if you do this (if not already done)

Must be YOUR server path

03-10-2003, 06:24 PM
if that doesn't work, maybe try just checking the last modified time - somewhat like

$filename = "/path/to/fotd.php";
if (filemtime($filename) < (time()+86400))
// old - so update fotd.php
$rndno = rand(1,12000);
$fp = fopen($filename,'w');
fwrite($fp,'<?php $number = '.$rndno.'; ?>');
echo $number;

note: I used a php file with a variable $number and random value which can just be included. Also note: rand() doesn't need salting since php4.2.0

03-10-2003, 06:28 PM
actually - thinking about this

a much better approach would be to write the php included file through running a cron job every 24 hours

03-11-2003, 10:48 AM
I have to say thanks for all your help with this dilema I was in. It has now been sorted out.

Anyone in need of the final coding - just ask and I'll post it.

Thanks everyone... :)

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum