You should learn php in this pose (3)

1、ob_start()

Enable file caching

2、ob_get_contents()

get the memory in the cache

3、ob_get_length()

get the length of the buffer

4、ob_flush()

Output the content of the buffer, if you want to get the content of the buffer, use ob_get_contents() before this function, otherwise the data will be cleared

5、ob_end_flush()

output buffer and close cache

6、ob_end_clean()

Clear the cache and turn off the output buffer. If this function is not executed after ob_start() is turned on, it will always be written to the buffer

Let's write a static example of a page to practice our hands. This case is explained by relying on the thinkphp framework, and other framework partners can make up their own minds!

1. First, there are four functions

ob_start() enables caching

ob_get_contents() Get the contents of the buffer

ob_clean() deletes the contents of the buffer

ob_get_clean() gets and then deletes the buffer contents

2. Then we define a function to generate static files

/**定义一个缓存文件

* @author crazy

* @time 2018-03-14

*/

public function createCache(){

$action = ACTION_NAME;

$c_name = CONTROLLER_NAME;

$dir = './Cache/'.$c_name.'/'.$action.'/';

if(is_dir($dir)){

file_put_contents("$dir$action".'.shtml',ob_get_contents());

}else{

if(mkdir($dir,0777,true)){

file_put_contents("$dir$action".'.shtml',ob_get_contents());

}

}

}

3. According to whether the file directory exists or not, then we do the corresponding redirection

$action = ACTION_NAME;

$c_name = CONTROLLER_NAME;

$dir = './Cache/'.$c_name.'/'.$action.'/'.$action.'.shtml';

if(file_exists($dir)){

header("Location:http://localhost/simengphp/$dir");

}

4. Local static

$.ajax({

url:'',

type:'get',

dataType:'json',

error: function () {

},

success:function(data){

$.each(data.result,function(key,val){

})

}

});

We write this ajax method in our template to get the content of the page, and then this method will be automatically created when we call the page

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324818844&siteId=291194637