Thread: Learning PHP
View Single Post
Old 10-25-2016, 12:37 PM  
redwhiteandblue
Bollocks
 
redwhiteandblue's Avatar
 
Industry Role:
Join Date: Jun 2007
Location: Bollocks
Posts: 2,792
One basic way to do caching is use the output buffering.

Put

ob_start();

at the start of your script, before you write any HTML. At the end, after you've written all the HTML, put

ob_end_flush();

That sends the contents of the buffer to the client with headers. To cache the page instead of sending it, you can make use of ob_get_contents(); and save the result as a file. Then the next time that page is requested, serve that file instead of rebuilding the page. After the file is a certain age, delete it and rebuild the page.

That's a basic way of doing caching. The problems arise when you want to dynamically add a value in the HTML which is different for each visitor, say if you're dynamically writing JS. Then it gets a bit more tricky....
redwhiteandblue is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote