Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-07-2003, 12:12 PM   PM User | #1
Golden_Eagle
Regular Coder

 
Join Date: Jul 2002
Posts: 100
Thanks: 0
Thanked 0 Times in 0 Posts
Golden_Eagle is an unknown quantity at this point
What's wrong with this piece of code?

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.

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

if ($contents['1'] <= (time()+86400)) { 
   $number=rand(1,12000); 
   $fileshowid=$number; 
   $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

1
1046960000

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
Golden_Eagle is offline   Reply With Quote
Old 03-07-2003, 12:38 PM   PM User | #2
Ökii
Regular Coder

 
Join Date: Jun 2002
Location: UK
Posts: 577
Thanks: 0
Thanked 0 Times in 0 Posts
Ökii is an unknown quantity at this point
have you tried it without quoting the index value of $contents ?

$contents[1]

as $contents['1'] would reference an associative array with index named '1' as a string ('0'=>1,'1'=>7849877436) as opposed to (1,784734796)
__________________
Ökii - formerly pootergeist
teckis - take your time and it'll save you time.
Ökii is offline   Reply With Quote
Old 03-07-2003, 01:13 PM   PM User | #3
Golden_Eagle
Regular Coder

 
Join Date: Jul 2002
Posts: 100
Thanks: 0
Thanked 0 Times in 0 Posts
Golden_Eagle is an unknown quantity at this point
Nope. Changed it. But nothing happened
Golden_Eagle is offline   Reply With Quote
Old 03-10-2003, 04:34 PM   PM User | #4
stylempire
New to the CF scene

 
Join Date: Mar 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
stylempire is an unknown quantity at this point
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
__________________
http://www.gofreelancers.com
Work For Programmers.
Need Script Help? Hire The Best Coders On The Web!! FREE Signup
stylempire is offline   Reply With Quote
Old 03-10-2003, 05:24 PM   PM User | #5
Ökii
Regular Coder

 
Join Date: Jun 2002
Location: UK
Posts: 577
Thanks: 0
Thanked 0 Times in 0 Posts
Ökii is an unknown quantity at this point
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.'; ?>');
fclose($fp);
}
include_once($filename);
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
__________________
Ökii - formerly pootergeist
teckis - take your time and it'll save you time.
Ökii is offline   Reply With Quote
Old 03-10-2003, 05:28 PM   PM User | #6
Ökii
Regular Coder

 
Join Date: Jun 2002
Location: UK
Posts: 577
Thanks: 0
Thanked 0 Times in 0 Posts
Ökii is an unknown quantity at this point
actually - thinking about this

a much better approach would be to write the php included file through running a cron job every 24 hours
__________________
Ökii - formerly pootergeist
teckis - take your time and it'll save you time.
Ökii is offline   Reply With Quote
Old 03-11-2003, 09:48 AM   PM User | #7
Golden_Eagle
Regular Coder

 
Join Date: Jul 2002
Posts: 100
Thanks: 0
Thanked 0 Times in 0 Posts
Golden_Eagle is an unknown quantity at this point
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...
Golden_Eagle is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:22 AM.


Advertisement
Log in to turn off these ads.