Is $expire a global value?

Posted on 2005-05-16
Last Modified: 2011-09-20

I have no PHP experience, but am familiar with perl and some other languages.  I'm using a PHP script from the Open Source community.  It is a front end to a MySQL database.

One of the functions selects distinct hosts from the database.  It then populates the webpage, but also writes them to a text file. Then on subsequent calls, it checks that text file, and if it exists, and is recent, is uses that list to populate the web form, instead of doing the SQL Select (to save overhead).

Nice idea.

But I'm jsut trying to understand when it decides to use the text file.  The code says:

      if (file_exists($file) && filemtime($file) > (time() - $expire))

Can someone explain that line to me.  It's checking the date stamp of the file, and comparing it to the current time minux $expire.  I can't find $expire defined anywhere.  Is that a global PHP thing?

Question by:shanepresley
    LVL 7

    Accepted Solution

    If the text file holding our cached data exists and the time the file was last modified is greater than the time on the server minute the time to allow the textfile to live then refresh the text file.

    I would do :
           echo "The cached data lives for : ". $expire . ' time units.<br>';
          if (file_exists($file) && filemtime($file) > (time() - $expire)) {  }

    Check that and see. Most likely it's defined in another file. Search for expire. It's either in there, or the programmer mistakenly left it out.

    LVL 1

    Author Comment

    Correct!! Thank you.  It was defined in another file (an include)

    common_inc.php:$expire          = 86400 ;       // Cache life of one day.

    So if the file exists, and is newer than one day..use the cache.

    Works for me!

    LVL 7

    Expert Comment

    You can actually tune this to your needs... More or less time as necessary. If your data only changes once a week, set it to a week, and then do manual refreshes as necessary.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
    Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to count occurrences of each item in an array.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now