Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions


WDVL Newsletter

Active Server Pages
JSP/Java Servlets
Microsoft SQL Server
Daily Backup
Dedicated Servers
Streaming Audio/Video
24-hour Support    

jobs.webdeveloper.com

Hiermenus


e-commerce
Partner With Us















Developer Channel
FlashKit.com
JavaScript.com
JavaScriptSource
Developer Jobs
ScriptSearch
StreamingMediaWorld
Web Developer's Journal
Web Developer's Virtual Library
WebDeveloper.com
Webreference
Web Hosts
XMLfiles.com

internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


The parse_it function

July 28, 2000

The parse_it function scans the string to see if any tag occurs, and if it does, breaks down the tagname in $fun and its optional parameters into the associated array $arglist.


<?php

function parse_it ($str) {
    global $loaded;

    if (eregi ("<[Mm][Yy]-([A-Za-z0-9]*) ([^>]*)", $str, $regs)) {
        $tag = $regs[1];
        if (!$loaded[$tag]) {
            include "res/$tag/$tag.php";
            $loaded[$tag] = 1;
        }

        $fun = "handle_$tag";
        $list = explode (" ", strtolower ($regs[2]));
        $cache_file = "cache/$tag";

        for ($i = 0; $i < count ($list); $i++) {
            if ($argname = strtok ($list[$i], "=")) {
                $arglist[$argname] = strtok ("=");
                if ($argname != "cache") {
                    $cache_file .= "_" . $argname . "=" . $arglist[$argname];
                }
            }
        }

        $buf = "<!-- $tag start here //-->\n";
        $buf .= $fun ($arglist);
        $buf .= "\n<!-- $tag ends here //-->\n";
        return $buf;
    } else {
        return $str;
    }
}

?>

This function calls the function $tag directly in the file included from res/$tag/$tag.php and uses the output to return to the parse main loop.

As you notice, the variable $cache_file is also being built pointing to cache/$tag... where ... is a string combining all parameters to form a unique cache entry for this tag. To actually use the cache, we must add some code between the end of the for loop and the creation of $buf:


<?php

$read_cache = 0;
$write_cache = 0;
if (!(isset ($arglist["cache"]) && ($arglist["cache"] < 10))) {
    $write_cache = 1;
    if (file_exists ($cache_file)) {
        if (!isset ($arglist["cache"])) {
            if ((filemtime ($cache_file) + $default_cache_time) > date ("U")) {
                $read_cache = 1;
                $write_cache = 0;
            }    
        } else {
            if ((filemtime ($cache_file) + $arglist["cache"]) > date ("U")) {
                $read_cache = 1;
                $write_cache = 0;
            }
        }
    }
}

?>

Security
Building your website with cached dynamic modules
$buf


Up to => Home / Authoring / Languages / PHP / Dynamic_Modules




Jupiter Online Media: internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and Jupiter Online Media

Jupitermedia Corporate Info


Legal Notices, Licensing, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers