Question

i have a php based website which. I use a switch case to include different pages and navigate. i have employed a method so that my index page includes a navigation bar and a footer

my problem is that each time i navigate from one page to another everything is loaded again and makes the website heavy.

<?php include('models/header.php'); ?>
<div id="content">
<center>

<div id="switch" align="center">

<?php
switch($view)
{
case 'Index':
    include('pages/index.php');
    break;

case 'Services':
    include('pages/Services.php');
    break;

case 'About':
    include('pages/about.php');
    break;

case 'Contact':
    include('pages/contact.php');
    break;

case 'Download':
    include('pages/download.php');
    break;

default:
    include('pages/error.php');
}
?>
</div>
</div>
</center>
<br>
<?php include('models/footer.php'); ?>
</div>

is there a way i can set it u so that these elements get preloaded once and stay in the cache so that they dont need to be loaded everytime i navigate to a new page...?

Was it helpful?

Solution

Given your code, you actually don't need to cache anything, doing so could lead to more overhead that it is actually needed.

Cached or not cached, you will still need to access a file, which your gain will be the opcode generation. But PHP still needs to access filesystem, except if you use a memcached solution with RAMFS, you won't note a real change.

However, you really need to cache your code, for obvious reasons, you should take a look at APC, which is an opcode cache for PHP.

Basically, it'll cache the calls you make to your included file and cache the PHP interpreter result.

Finally, I actually advise you to give a read to Best Practices for Speeding Up Your Web Site which will help you enhance user experience in a probably more notable way.

OTHER TIPS

The elements in the page (such as images) will be fully reloading if the browser chooses so. If your elements are PHP files, they will generally be reloaded completely as PHP pages often change.

You could set headers in PHP that will tell the browser to cache the page for a certain amount of time, though. See http://php.net/manual/en/function.header.php for more info.

Theres a lot of different ways to do this. I would recommend using Smarty.

switch(strtolower($view)) {
     case "download":
            $smarty->assign("download_var", $downloadvar);
            $smarty->display("Download.tpl");
            break;
     .....
}

UPDATE

I guess this a bit vague still. Smarty actually has a compiled templates directory that it keeps handy. You can configure smarty to cache in a lot of different ways but the basic idea is that you have a flat file thats precompiled and stored based on session ID.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top