Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9198341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:15:39+00:00 2026-06-17T22:15:39+00:00

<?php // cache – will work online – not locally // location and prefix

  • 0
<?php
// cache - will work online - not locally
// location and prefix for cache files
define('CACHE_PATH', "siteCache/"); 

// how long to keep the cache files (hours)
define('CACHE_TIME', 12); 
// return location and name for cache file 
function cache_file() 
{ 
    return CACHE_PATH . md5($_SERVER['REQUEST_URI']); 
}

// display cached file if present and not expired 
function cache_display() 
{ 
    $file = cache_file(); 
    // check that cache file exists and is not too old 
    if(!file_exists($file)) return; 
    if(filemtime($file) < time() - CACHE_TIME * 3600) return; 
    // if so, display cache file and stop processing
    readfile($file);
    exit; 
} 

// write to cache file 
function cache_page($content) 
{ 
    if(false !== ($f = @fopen(cache_file(), 'w'))) { 
        fwrite($f, $content);
        fclose($f); 
    } 
    return $content; 
} 

// execution stops here if valid cache file found
cache_display(); 

// enable output buffering and create cache file 
ob_start('cache_page');
?>

This is the cache code that I am using in a dynamic website in db file. And every page contains this code at top.

<?php session_start();
include("db.php"); ?>

Pages are being cached and its working but on form submission, on user login, on variable passing through pages, nothing is happening. Old pages are being displayed. How do I use this caching code so that it may work but site remain functional as well.

I wonder how wordpress plugins do it. Wp Super Cache and W3T Cache cache everything, yet blog remains functional. Should I selectively use it at parts of website.

Like this:

<?php
// TOP of your script
$cachefile = 'cache/'.basename($_SERVER['SCRIPT_URI']);
$cachetime = 120 * 60; // 2 hours

// Serve from the cache if it is younger than $cachetime
if (file_exists($cachefile) && (time() - $cachetime < filemtime($cachefile))) {
    include($cachefile);
    echo "<!-- Cached ".date('jS F Y H:i', filemtime($cachefile))." -->";
    exit;
}

ob_start(); // start the output buffer
// Your normal PHP script and HTML content here
// BOTTOM of your script
$fp = fopen($cachefile, 'w'); // open the cache file for writing
fwrite($fp, ob_get_contents()); // save the contents of output buffer to the file
fclose($fp); // close the file
ob_end_flush(); // Send the output to the browser
?>

But it will not work as well, because its about pageURL (whole page caching), not the selective content from page.

Please advise. Is there any easy script do do this. Pear::Cache_Lite seems good but its looks difficult to implement.

Update: I have used Cache_Lite. Its the same. Caches everything or included php file. There are few configuration options to play with. But if used as a whole, it will also ignore get, post, session data updates…and will show previous cached pages unless they are deleted.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T22:15:40+00:00Added an answer on June 17, 2026 at 10:15 pm

    I think you could separate display from logic.

    I mean, change the action attribute of the form and point it to a php that does not have cache logic (and you must check the referer or other parameter, using tokens or sessions, or something, to avoid security issues like CSRF).

    Other thing that I want to point out is you should look to cache only the most visited pages (i.e. the homepage), generally you don’t have a “one size fits all” with caching, and it is better not to worry about pages that don’t have speed/load issues. Or it may be better to cache the data if your speed issues comes from a database query (you should profile your application before implementing caching).

    Other approach that migth work is checking the request method and disable the cache if it is post (given that all your forms use the POST method) using $_SERVER['REQUEST_METHOD'] == 'POST'.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a cache library that stores cached data in normal php files, with
Here is my try: @header(Content-type: text/html; charset=utf-8); @header(Location:/index.php); @header(Cache-Control: no-cache, must-revalidate); // HTTP/1.1 @header(Expires:
Simple question. Do browsers cache PHP generated CSS and script files automatically, just like
If PHP does cache a permanent redirect: For how long? Can this period be
I followed this tutorial http://stevejenkins.com/blog/2011/08/how-to-install-apc-alternative-php-cache-on-centos-5-6/ to install apc on my centos vps hosting but
How to clear browser cache with php?
I need the browser to cache a large, mostly static .php file. I open
Hi I need to clear IE8 browser cache before loading my cd-based php application.
I implemented heartbeat as the following $.ajax({ cache:false, timeout:8000, type:POST, url:someurl.php, data:allFormValues, error:function(){ alert(some
I have this jQuery code: $(document).ready(function() { $.ajax({ url: pages/+page+.php, cache: false }).done(function( html

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.