HTML5 offline article

Home >> HTML5 offline >>

  HTML5 offline application dynamic manifest file in PHP.

This Article will explain you, how to create a dynamic manifest file in PHP. Let's take an example, you have some static resources as well as dynamic resources.

Static resources means, which will not get change frequently like JS, CSS, Images etc...

Dynamic resources means, which may get changed frequently like a product image. An admin might change them.

For explanation purposes, I consider static or dynamic resources. It might be possible that you have all static resources no dynamic resources, then you can consider only static resources.

Let's follow the below steps to create the dynamic manifest file.

Step 1: - First, you need database configuration.

Step 2:- You may have some static resources (JS, CSS, Images) as well as some dynamic images. For dynamic resources you will execute some query which will fetch the image name from the database. The same thing I did in source code using $dynamic_image_array array which will consist only dynamic images.

Step 3:- For  static resources (JS, CSS, Images) you can create a static $cache array which will consist only static resources.

Step 4:- If you run this code and see the last of manifest file you will get one #Hash with some random string. This hash will change every time the cache manifest changes even in the slightest bit. This allows the browser or your users to understand that the cache manifest has changed and that it needs to reload it.

<?php

require_once('db_config.php'); // Database configuration.

/**
 * Function to generate random string.
 */
function getRandomString($length = 10)
{
    $random_string = md5(uniqid(mt_rand(), true));
    return $random_string;
}

/**
 * Mainfest file path.
 */
$mainfest_full_url = "/public_html/cache_file/cache.appcache";


/**
 * Dynamic images for mainfest file. 
 */
$dynamic_image_array = array();
$select_image_sql = "SELECT image as dynamic_image FROM table-name";
$select_image_sql_exe = mysql_query($select_image_sql);
while ($dynamic_images = mysql_fetch_assoc($select_image_sql_exe)) {
    array_push($dynamic_image_array, "/public_html/upload_images/" . $dynamic_images['dynamic_image']);
}

/**
 * Mainfest file for statics resources . 
 */
$hashes = getRandomString();
$network = array("NETWORK:", "*");

/**
 * Files that are static like css, js and static images will be included in the CACHE section. 
 */
$cache = array(
    "CACHE:",
    "#css", "/public_html/gallery/css/abc.css", "/public_html/gallery/css/def.css",
    "#js", "/public_html/gallery/js/abc.js", "/public_html/gallery/js/def.js",
    "#dynamicimages"
);

$complete_cache_array = array_filter(array_merge($cache, $dynamic_image_array));

$ignore = array("error_log", "html");
$dir = new RecursiveDirectoryIterator(".");
foreach (new RecursiveIteratorIterator($dir) as $file) {
    if ($file->IsFile() && $file != "manifest.php" && substr($file->getFilename(), 0, 1) != ".") {
        if (preg_match('/.php$/', $file)) {
            foreach ($ignore as $item):
                if (strpos($file, $item)): $allow = FALSE;
                    break;
                else: $allow = TRUE;
                endif;
            endforeach;
            //if($allow): array_push($network,"\n". $file); endif;
        }
        else {
            foreach ($ignore as $item):
                if (strpos($file, $item)): $allow = FALSE;
                    break;
                else: $allow = TRUE;
                endif;
            endforeach;
            //if($allow): array_push($complete_cache_array,"\n" . $file); endif;
        }
    }
}

$mainfet_file = fopen($mainfest_full_url, 'w');
fwrite($mainfet_file, 'CACHE MANIFEST');
foreach ($complete_cache_array as $file): fwrite($mainfet_file, $file);
endforeach; // CACHE ARRAY.
foreach ($network as $file): fwrite($mainfet_file, $file);
endforeach; // NETWORK ARRAY.

fwrite($mainfet_file, "#Hash:" . base64_encode($hashes));
fclose($mainfet_file);

?>

 

  Download code

Write your comment now