In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is about caching ob functions on php pages. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Php page cache mainly uses ob series functions, such as ob_start (), ob_end_flush (), ob_get_contents ().
Here is the coding part.
1. Initialization function, generally set the page cache path, cache file naming format, etc., can be customized according to personal preference. The identification ID used here is the encrypted $_ server [request _ URI] parameter. At the end of this function, there is an if judgment: if the cache period is not over, the cache file is loaded, otherwise the source file is loaded.
The copy code is as follows:
Function page_init ()
{
$url = $_ SERVER ['REQUEST_URI']; / / Sub-url, which is generally unique
$pageid = md5 ($url)
$dir = str_replace ('/','_', substr ($_ SERVER ['SCRIPT_NAME'], 1mai Murray 4)
/ / Directory naming method, such as exp_index
If (! file_exists ($pd = PAGE_PATH.$dir.'/')) @ mkdir ($pd,0777) or die ("$pd directory creation failed")
/ / such as cache / page/exp_index/
Define ('PAGE_FILE',$pd.$pageid.'.html')
/ / such as cache / page/exp_index/cc8ef22b405566745ed21305dd248f0e.html
$contents = file_get_contents (PAGE_FILE); / / read out
If ($contents & & substr ($contents, 13,10) > time ()) / / the custom header added in the corresponding page_cache () function
{
Echo substr ($contents, 27)
Exit (0)
}
Return true
}
two。 Page caching function, here uses a trick: add a header information-expiration time to the contents of the cache file, so you only need to compare the expiration time in the header with the current time (in the page_init () function) to determine whether the cache has expired.
The copy code is as follows:
Function page_cache ($ttl = 0)
{
$ttl = $ttl? $ttl: PAGE_TTL;// cache time. Default is 3600s.
$contents = ob_get_contents (); / / get content from cache
$contents = "\ n". $contents
/ / add custom header: expiration time = generation time + cache time
File_put_contents (PAGE_FILE, $contents); / / write to the cache file
Ob_end_flush (); / / release the cache
}
3. Functions, note that the two functions are executed in sequence, and don't forget ob_start ()
The copy code is as follows:
Thank you for reading! This is the end of this article on "what are the functions of php page cache ob series". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.