Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Jul 2002
    Posts
    100
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    577
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #3
    Regular Coder
    Join Date
    Jul 2002
    Posts
    100
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Nope. Changed it. But nothing happened

  • #4
    New to the CF scene
    Join Date
    Mar 2003
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    577
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #6
    Regular Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    577
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #7
    Regular Coder
    Join Date
    Jul 2002
    Posts
    100
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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...


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •