January 3, 2024 5:01 am

PHP Server and Browser Cache

I am writing this tutorial to increase speed of your web applications with server and browser cache. With caching your application become super lets come to the details of the procedure of cache.

Cache

[wpdm_file id=19]DEMO

Basics

You should have writing permission for the caching folder (eg. chmod 666 or 777).
You should add attached value (for example cookie or session value ) for the logged users.
The caching object should be added right after the initialization of cookie session.

Sample

<?php
$folder=$_SERVER['DOCUMENT_ROOT']."/cache";  
// in this case i created the folder "cache" in root

$cache=new CACHE($folder);
$cache->startCache();
// after that.. the rest of the page/scripts

//showing the content 
? >
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
...........................
</head>
<body>
..........................
</body>
</html>

1. Server Side Caching

<?php

$folder=$_SERVER['DOCUMENT_ROOT']."/cache";
$cache=new CACHE($folder);
$cache->setCacheType(1); //server side caching
$cache->startCache();

?>

2. Client Side (Browser) Caching

<?php

$folder=$_SERVER['DOCUMENT_ROOT']."/cache";
$cache=new CACHE($folder);
$cache->setCacheType(2); //client side caching
$cache->startCache();

?>

3. Both (Server and Client) Side Caching

<?php

$folder=$_SERVER['DOCUMENT_ROOT']."/cache_";
$cache=new CACHE($folder);
$cache->setCacheType(3); //both side caching
$cache->startCache();

?>

4. Logged User Both Side Cache

<?php

$folder=$_SERVER['DOCUMENT_ROOT']."/cache";
$cache=new CACHE($folder,2, $_COOKIE['user']); // $_COOKIE['user'] is just an example
$cache->startCache();

?>

cache.php file default settings

private $root=""; // path to the local folder, YOU NEED WRITING PERMISSIONS (chmod 666 or 777)
private $server__cache_time=300; // how long a file will remain cached on browser ( seconds )
private $client__cache_time=150; //how long a file will remain cached on browser ( seconds )
private $cache_type=2; // 0 = no cache , 1 = server side cache, 2 = client side (browser) cache, 3 = both side cache
private $attached_value=""; //useful to COOKIE or SESSION values (for example it may be used for logged users )
private $page;
public $file_name; // the cache file name on server
public $full_file_name; //full file path and name on local server
public $need_update=false; //if true then the content will be saved locally in the path (full_file_name)
public $file_extension="html"; // useful to check the cache content faster

Cache type table

Type Action
0 No cache
1 Server side cache
2 Client side cache
3 Both sides cache

Author Huzoor Bux

I am Huzoor Bux from Karachi (Pakistan). I have been working as a PHP Developer from last 5+ years, and its my passion to learn new things and implement them as a practice. Basically I am a PHP developer but now days exploring more in HTML5, CSS and jQuery libraries.


Tutorial Categories:

4 responses to “PHP Server and Browser Cache”

  1. sempi says:

    memcached?

  2. Saddam Khan says:

    Thanks…. Was looking for it

  3. Loganatan says:

    Good, useful.

  4. Jonas Maro says:

    Hi, I am trying to suscribe to your feed to be able to download your PHP Cache Code, I think it’s interesting, but I can’t. When I click on Suscribe after inserting my email, a spinning icon appears and it doesn’t stop. Could you help me? Thanks in advance 😉

Leave a Reply

Your email address will not be published. Required fields are marked *