Session variables to cache HTML

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Echilon
Forum Newbie
Posts: 12
Joined: Fri Nov 25, 2005 4:11 am
Location: England
Contact:

Session variables to cache HTML

Post by Echilon »

I have a script which uses a large amount of IF statements to check the contents of variables and generate HTML. These variables are loaded from a database, and don't change during the session. I'm wondering whether it would be considered bad practice to generate the HTML at the start of the session, then cache say 20Kb or so in a session variable.

It's actually for a dynamic navigation bar. The bar would only need to be generated once, then just echoed from the saved SESSION variable. Would this cause PHP to take a performance hit at all?
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

Sounds reasonable enough to me, though using templates is the typical solution to this kind of issue. have you considered either writing either a dead-simple template engine* or using one of the pre-built ones out there? Smarty is popular, though I've never used it personally.

*a session array & str_replace() could be considered a template engine, it doesn't have to be brain surgery.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Session variables to cache HTML

Post by superdezign »

Echilon wrote:Would this cause PHP to take a performance hit at all?
PHP has to load the session data anyway. Go for it. :P
Post Reply