国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home php教程 php手冊(cè) 用PHP控制您的瀏覽器cache

用PHP控制您的瀏覽器cache

Jun 21, 2016 am 09:11 AM
flush quot start

cache|控制|瀏覽器

Output Control 函數(shù)可以讓你自由控制腳本中數(shù)據(jù)的輸出。它非常地有用,特別是對(duì)于:當(dāng)你想在數(shù)據(jù)已經(jīng)輸出后,再輸出文件頭的情況。輸出控制函數(shù)不對(duì)使用 header() 或 setcookie(), 發(fā)送的文件頭信息產(chǎn)生影響,只對(duì)那些類似于 echo() 和 PHP 代碼的數(shù)據(jù)塊有作用。

我們先舉一個(gè)簡(jiǎn)單的例子,讓大家對(duì)Output Control有一個(gè)大致的印象:
Example 1.
ob_start(); //打開緩沖區(qū)
echo "Hellon"; //輸出
header(“l(fā)ocation:index.php”); //把瀏覽器重定向到index.php
ob_end_flush();//輸出全部?jī)?nèi)容到瀏覽器
?>
所有對(duì)header()函數(shù)有了解的人都知道,這個(gè)函數(shù)會(huì)發(fā)送一段文件頭給瀏覽器,但是如果在使用這個(gè)函數(shù)之前已經(jīng)有了任何輸出(包括空輸出,比如空格,回車和換行)就會(huì)提示出錯(cuò)。如果我們?nèi)サ舻谝恍械膐b_start(),再執(zhí)行此程序,我們會(huì)發(fā)現(xiàn)得到了一條錯(cuò)誤提示:“Header had all ready send by”!但是加上ob_start,就不會(huì)提示出錯(cuò),原因是當(dāng)打開了緩沖區(qū),echo后面的字符不會(huì)輸出到瀏覽器,而是保留在服務(wù)器,直到你使用flush或者ob_end_flush才會(huì)輸出,所以并不會(huì)有任何文件頭輸出的錯(cuò)誤!


一、 相關(guān)函數(shù)簡(jiǎn)介:
1、Flush:刷新緩沖區(qū)的內(nèi)容,輸出。
函數(shù)格式:flush()
說明:這個(gè)函數(shù)經(jīng)常使用,效率很高。
2、ob_start :打開輸出緩沖區(qū)
函數(shù)格式:void ob_start(void)
說明:當(dāng)緩沖區(qū)激活時(shí),所有來自PHP程序的非文件頭信息均不會(huì)發(fā)送,而是保存在內(nèi)部緩沖區(qū)。為了輸出緩沖區(qū)的內(nèi)容,可以使用ob_end_flush()或flush()輸出緩沖區(qū)的內(nèi)容。
3 、ob_get_contents :返回內(nèi)部緩沖區(qū)的內(nèi)容。
使用方法:string ob_get_contents(void)
說明:這個(gè)函數(shù)會(huì)返回當(dāng)前緩沖區(qū)中的內(nèi)容,如果輸出緩沖區(qū)沒有激活,則返回 FALSE 。
4、ob_get_length:返回內(nèi)部緩沖區(qū)的長(zhǎng)度。
使用方法:int ob_get_length(void)
說明:這個(gè)函數(shù)會(huì)返回當(dāng)前緩沖區(qū)中的長(zhǎng)度;和ob_get_contents一樣,如果輸出緩沖區(qū)沒有激活。則返回 FALSE。
5、ob_end_flush :發(fā)送內(nèi)部緩沖區(qū)的內(nèi)容到瀏覽器,并且關(guān)閉輸出緩沖區(qū)。
使用方法:void ob_end_flush(void)
說明:這個(gè)函數(shù)發(fā)送輸出緩沖區(qū)的內(nèi)容(如果有的話)。
6、ob_end_clean:刪除內(nèi)部緩沖區(qū)的內(nèi)容,并且關(guān)閉內(nèi)部緩沖區(qū)
使用方法:void ob_end_clean(void)
說明:這個(gè)函數(shù)不會(huì)輸出內(nèi)部緩沖區(qū)的內(nèi)容而是把它刪除!
7、ob_implicit_flush:打開或關(guān)閉絕對(duì)刷新
使用方法:void ob_implicit_flush ([int flag])
說明:使用過Perl的人都知道$|=x的意義,這個(gè)字符串可以打開/關(guān)閉緩沖區(qū),而ob_implicit_flush函數(shù)也和那個(gè)一樣,默認(rèn)為關(guān)閉緩沖區(qū),打開絕對(duì)輸出后,每個(gè)腳本輸出都直接發(fā)送到瀏覽器,不再需要調(diào)用 flush()


二、深入了解:

1. 關(guān)于Flush函數(shù):
這個(gè)函數(shù)在PHP3中就出現(xiàn)了,是一個(gè)效率很高的函數(shù),他有一個(gè)非常有用的功能就是刷新browser的cache.我們舉一個(gè)運(yùn)行效果非常明顯的例子來說明flush.
Example 2.
for($i = 1; $i // 這一句話非常關(guān)鍵,cache的結(jié)構(gòu)使得它的內(nèi)容只有達(dá)到一定的大小才能從瀏覽器里輸出
// 換言之,如果cache的內(nèi)容不達(dá)到一定的大小,它是不會(huì)在程序執(zhí)行完畢前輸出的。經(jīng)
// 過測(cè)試,我發(fā)現(xiàn)這個(gè)大小的底限是256個(gè)字符長(zhǎng)。這意味著cache以后接收的內(nèi)容都會(huì)
// 源源不斷的被發(fā)送出去。
For($j = 1; $j echo $j.”
”;
flush(); //這一部會(huì)使cache新增的內(nèi)容被擠出去,顯示到瀏覽器上
sleep(1); //讓程序“睡”一秒鐘,會(huì)讓你把效果看得更清楚
}
?>

具體效果你可以到這里看看http://www.php2000.com/~uchinaboy/out.php
PHP2000的最新的PHP聊天室就是用的這個(gè)技術(shù),可惜的是源代碼未公開 L
注:如果在程序的首部加入ob_implicit_flush()打開絕對(duì)刷新,就可以在程序中不再使用flush(),這樣做的好處是:提高效率!

2. 關(guān)于ob系列函數(shù):
我想先引用我的好朋友y10k的一個(gè)例子:
Example 3.
比如你用得到服務(wù)器和客戶端的設(shè)置信息,但是這個(gè)信息會(huì)因?yàn)榭蛻舳说牟煌煌?,如果想要保存phpinfo()函數(shù)的輸出怎么辦呢?在沒有緩沖區(qū)控制之前,可以說一點(diǎn)辦法也沒有,但是有了緩沖區(qū)的控制,我們可以輕松的解決:
ob_start(); //打開緩沖區(qū)
phpinfo(); //使用phpinfo函數(shù)
$info=ob_get_contents(); //得到緩沖區(qū)的內(nèi)容并且賦值給$info
$file=fopen('info.txt','w'); //打開文件info.txt
fwrite($file,$info); //寫入信息到info.txt
fclose($file); //關(guān)閉文件info.txt
?>

用以上的方法,就可以把不同用戶的phpinfo信息保存下來,這在以前恐怕沒有辦法辦到!其實(shí)上面就是將一些“過程”轉(zhuǎn)化為“函數(shù)”的方法!
或許有人會(huì)問:“難道就這個(gè)樣子嗎?還有沒有其他用途?”當(dāng)然有了,比如筆者論壇的PHP 語法加亮顯示就和這個(gè)有關(guān)(PHP默認(rèn)的語法加亮顯示函數(shù)會(huì)直接輸出,不能保存結(jié)果,如果在每次調(diào)用都顯示恐怕會(huì)很浪費(fèi)CPU,筆者的論壇就把語法加亮函數(shù)顯示的結(jié)果用控制緩沖區(qū)的方法保留了),大家如果感興趣的話可以來看看http://www.zphp.com/bbs/!

可能現(xiàn)在大家對(duì)ob_start()的功能有了一定的了解,上面的一個(gè)例子看似簡(jiǎn)單,但實(shí)際上已經(jīng)掌握了使用ob_start()的要點(diǎn)。
.使用ob_start打開browser的cache,這樣可以保證cache的內(nèi)容在你調(diào)用flush(),ob_end_flush()(或程序執(zhí)行完畢)之前不會(huì)被輸出。
.現(xiàn)在的你應(yīng)該知道你所擁有的優(yōu)勢(shì):可以在任何輸出內(nèi)容后面使用header,setcookie以及session,這是ob_start一個(gè)很大的特點(diǎn);也可以使用ob_start的參數(shù),在cache被寫入后,然后自動(dòng)運(yùn)行命令,比如ob_start("ob_gzhandler");而我們最常用的做法是用ob_get_contents()得到cache中的內(nèi)容,然后再進(jìn)行處理……
.當(dāng)處理完畢后,我們可以使用各種方法輸出,flush(),ob_end_flush(),以及等到程序執(zhí)行完畢后的自動(dòng)輸出。當(dāng)然,如果你用的是ob_get_contents(),那么就要你自己控制輸出方式了。

來,讓我們看看能用ob系列函數(shù)做些什么……

一、 靜態(tài)模版技術(shù)

簡(jiǎn)介:所謂靜態(tài)模版技術(shù)就是通過某種方式,使得用戶在client端得到的是由PHP產(chǎn)生的html頁面。如果這個(gè)html頁面不會(huì)再被更新,那么當(dāng)另外的用戶再次瀏覽此頁面時(shí),程序?qū)⒉粫?huì)再調(diào)用PHP以及相關(guān)的數(shù)據(jù)庫,對(duì)于某些信息量比較大的網(wǎng)站,例如sina,163,sohu。類似這種的技術(shù)帶來的好處是非常巨大的。

我所知道的實(shí)現(xiàn)靜態(tài)輸出的有兩種辦法:
.通過y10k修改的phplib的一個(gè)叫template.inc.php類實(shí)現(xiàn)。
.使用ob系列函數(shù)實(shí)現(xiàn)。
對(duì)于第一種方法,因?yàn)椴皇沁@篇文章所要研究的問題,所以不再贅述。
我們現(xiàn)在來看一看第二種方法的具體實(shí)現(xiàn):
Example 4.

ob_start();//打開緩沖區(qū)
?>
php頁面的全部輸出

$content = ob_get_contents();//取得php頁面輸出的全部?jī)?nèi)容
$fp = fopen(“output00001.html”, “w”); //創(chuàng)建一個(gè)文件,并打開,準(zhǔn)備寫入
fwrite($fp, $content); //把php頁面的內(nèi)容全部寫入output00001.html,然后……
fclose($fp);
?>
這樣,所謂的靜態(tài)模版就很容易的被實(shí)現(xiàn)了……

二、 捕捉輸出

以上的Example 4.是一種最簡(jiǎn)單的情況,你還可以在寫入前對(duì)$content進(jìn)行操作……
你可以設(shè)法捕捉一些關(guān)鍵字,然后去對(duì)它進(jìn)行再處理,比如Example 3.所述的PHP語法高亮顯示。個(gè)人認(rèn)為,這個(gè)功能是此函數(shù)最大的精華所在,它可以解決各種各樣的問題,但需要你有足夠的想象力……
Example 5.

Function run_code($code) {
If($code) {
ob_start();
eval($code);
$contents = ob_get_contents();
ob_end_clean();
}else {
echo “錯(cuò)誤!沒有輸出”;
exit();
}
return $contents;
}

以上這個(gè)例子的用途不是很大,不過很典型$code的本身就是一個(gè)含有變量的輸出頁面,而這個(gè)例子用eval把$code中的變量替換,然后對(duì)輸出結(jié)果再進(jìn)行輸出捕捉,再一次的進(jìn)行處理……

Example 6. 加快傳輸


/*
** Title.........: PHP4 HTTP Compression Speeds up the Web
** Version.......: 1.20
** Author........: catoc
** Filename......: gzdoc.php
** Last changed..: 18/10/2000
** Requirments...: PHP4 >= 4.0.1
** PHP was configured with --with-zlib[=DIR]
** Notes.........: Dynamic Content Acceleration compresses
** the data transmission data on the fly
** code by sun jin hu (catoc)
** Most newer browsers since 1998/1999 have
** been equipped to support the HTTP 1.1
** standard known as "content-encoding."
** Essentially the browser indicates to the
** server that it can accept "content encoding"
** and if the server is capable it will then
** compress the data and transmit it. The
** browser decompresses it and then renders
** the page.
**
** Modified by John Lim (jlim@natsoft.com.my)
** based on ideas by Sandy McArthur, Jr
** Usage........:
** No space before the beginning of the first '' tag.
** ------------Start of file----------
** |
** | include('gzdoc.php');
** |? >
** |
** |... the page ...
** |
** |
** | gzdocout();
** |? >
** -------------End of file-----------
*/
ob_start();
ob_implicit_flush(0);
function CheckCanGzip(){
global $HTTP_ACCEPT_ENCODING;
if (headers_sent() || connection_timeout() || connection_aborted()){
return 0;
}
if (strpos($HTTP_ACCEPT_ENCODING, 'x-gzip') !== false) return "x-gzip";
if (strpos($HTTP_ACCEPT_ENCODING,'gzip') !== false) return "gzip";
return 0;
}
/* $level = compression level 0-9, 0=none, 9=max */
function GzDocOut($level=1,$debug=0){
$ENCODING = CheckCanGzip();
if ($ENCODING){
print "nn";
$Contents = ob_get_contents();
ob_end_clean();
if ($debug){
$s = "

Not compress length: ".strlen($Contents);
$s .= "
Compressed length: ".strlen(gzcompress($Contents,$level));
$Contents .= $s;
}
header("Content-Encoding: $ENCODING");
print "x1fx8bx08x00x00x00x00x00";
$Size = strlen($Contents);
$Crc = crc32($Contents);
$Contents = gzcompress($Contents,$level);
$Contents = substr($Contents, 0, strlen($Contents) - 4);
print $Contents;
print pack('V',$Crc);
print pack('V',$Size);
exit;
}else{
ob_end_flush();
exit;
}
}
?>
這是catoc的一段很早以前的代碼,是在weblogs.com看到的,他利用了zlib的函數(shù),對(duì)傳輸?shù)膬?nèi)容進(jìn)行了壓縮,測(cè)試表明,對(duì)于10k以上的頁面,會(huì)產(chǎn)生效果,而且頁面越大,效果越明顯……



Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What should I do if docker start cannot start? What should I do if docker start cannot start? Oct 21, 2022 pm 03:43 PM

Solution to docker start failure: 1. Check the running status, and then release the occupied memory through the "echo 3 > /proc/sys/vm/drop_caches" command; 2. Use "$netstat -nltp|grep .. ." command to check whether the port has been occupied. If it is found to be occupied after going online, change it to an available port and restart.

What to do if node start reports an error What to do if node start reports an error Dec 29, 2022 pm 01:55 PM

Solution to node start error: 1. Execute "node xx.js" directly in the terminal; 2. Add start startup item "scripts": {"test": "echo \"Error: no test specified\" && exit 1 ","start":"node service.js"}"; 3. Re-execute "npm start".

php提交表單通過后,彈出的對(duì)話框怎樣在當(dāng)前頁彈出,該如何解決 php提交表單通過后,彈出的對(duì)話框怎樣在當(dāng)前頁彈出,該如何解決 Jun 13, 2016 am 10:23 AM

php提交表單通過后,彈出的對(duì)話框怎樣在當(dāng)前頁彈出php提交表單通過后,彈出的對(duì)話框怎樣在當(dāng)前頁彈出而不是在空白頁彈出?想實(shí)現(xiàn)這樣的效果:而不是空白頁彈出:------解決方案--------------------如果你的驗(yàn)證用PHP在后端,那么就用Ajax;僅供參考:HTML code

What is the difference between start and run in java thread What is the difference between start and run in java thread Apr 20, 2023 pm 12:37 PM

publicclassTest1extendsThread{@Overridepublicvoidrun(){while(true){System.out.println(Thread.currentThread().getName());}} publicstaticvoidmain(String[]args){Test1test1=newTest1();test1.run() ;//Output result maintest1.start();//Output result Thread-0}}1.startstart is to start a

How to start a session using the session_start function in PHP How to start a session using the session_start function in PHP Jun 26, 2023 pm 01:33 PM

The session_start() function is one of the functions that opens a session in PHP. Session is a very common technology in web development, and the basis of session is to assign a unique session ID to the user when he visits the website, and to use this session ID to identify the user in subsequent visits. Sessions can be used to save the user's login status, shopping cart contents, and other user information, and can also be used to implement permission control for some functions on the website. The way to open a session in PHP is very simple, just use sess

How to use the start method and run method in Java thread How to use the start method and run method in Java thread Apr 20, 2023 am 08:58 AM

start method and run method The $start()$ method is used to start a thread. At this time, the thread is in the ready (runnable) state and is not running. Once the $cpu$ time slice is obtained, the $run()$ method starts to be executed. . Directly calling the $run()$ method only calls a method in a class, which is essentially executed in the current thread. Therefore, it can only be achieved by using the $start()$ method to call the $run()$ method. True multithreading. Sample code@Slf4j(topic="c.Test4")publicclassTest4{publicstaticvoidmain(Strin

圖片消失怎么解決 圖片消失怎么解決 Apr 07, 2024 pm 03:02 PM

圖片消失如何解決先是圖片文件上傳$file=$_FILES['userfile']; ?if(is_uploaded_file($file['tmp_name'])){$query=mysql_query("INSERT INTO gdb_banner(image_src ) VALUES ('images/{$file['name'

不用數(shù)據(jù)庫來實(shí)現(xiàn)用戶的簡(jiǎn)單的下載,代碼如下,但是卻不能下載,請(qǐng)高手找下原因,文件路勁什么的沒有關(guān)問題 不用數(shù)據(jù)庫來實(shí)現(xiàn)用戶的簡(jiǎn)單的下載,代碼如下,但是卻不能下載,請(qǐng)高手找下原因,文件路勁什么的沒有關(guān)問題 Jun 13, 2016 am 10:15 AM

不用數(shù)據(jù)庫來實(shí)現(xiàn)用戶的簡(jiǎn)單的下載,代碼如下,但是卻不能下載,請(qǐng)高手找下原因,文件路勁什么的沒問題。

See all articles