簡體   English   中英

PHP:如何使用輸出緩沖區緩存頁面?

[英]PHP: How can I cache a page using output buffer?

我正在嘗試緩存從非常慢的API獲取/處理數據的頁面-因此我可以將其快速加載到用戶。 但是由於某種原因,輸出緩沖區為空?

<?php

ob_start();

// here I have php scripting accessing api's for information

?>


// Here I have HTML content with some php conditions and echos to filter and display the gathered information

// then I try to save the buffered page to the database:

<?php

//connect to database
$page = ob_get_contents();

mysql_query("UPDATE `pages` SET `page_cache` = '" . $page . "' WHERE `page_id` = '" . $page_id . "'");

?>

任何幫助,將不勝感激!

您確定$ page只包含數據庫安全字符嗎?

例如,如果輸出包含單個'怎樣?

您可能希望讓MySQL對$ page變量進行編碼:

mysql_query(sprintf(
    "UPDATE `pages` SET `page_cache`='%s' WHERE `page_id`=%s",
    mysql_real_escape_string( $page ),
    intval( $page_id )  // assuming it's an int
));

檢查/etc/php.ini中是否啟用了輸出緩沖: http ://www.php.net/manual/zh/outcontrol.configuration.php

另外,請執行以下操作:

<?php echo ob_get_contents(); ?>

如果顯示FALSE,則可能無法啟用輸出緩沖: http : //php.net/manual/en/function.ob-get-contents.php

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM