Bộ đệm trong php

Những gì bạn không thể học được từ tài liệu + bản vá lỗ hổng bảo mật + lời khuyên về cách tăng tốc độ phản hồi của máy chủ

Bộ đệm đầu ra cho phép bạn có đầu ra của PHP [được tạo chủ yếu bởi

ob_start[];  // turns on output buffering
$foo->render[];  // output goes only to buffer
$output = ob_get_contents[];  // stores buffer contents to the variable
ob_end_clean[];  // clears buffer and closes buffering
1] được lưu vào bộ nhớ [tức là. bộ đệm] thay vì được truyền ngay đến trình duyệt hoặc thiết bị đầu cuối. Cái nào hữu ích cho nhiều nhiệm vụ

Ngăn chặn đầu ra

ob_start[];  // turns on output buffering
$foo->bar[];  // all output goes only to buffer
ob_end_clean[];  // clears buffer and closes buffering

Nắm bắt đầu ra cho một biến

ob_start[];  // turns on output buffering
$foo->render[];  // output goes only to buffer
$output = ob_get_contents[];  // stores buffer contents to the variable
ob_end_clean[];  // clears buffer and closes buffering

Cặp

ob_start[];  // turns on output buffering
$foo->render[];  // output goes only to buffer
$output = ob_get_contents[];  // stores buffer contents to the variable
ob_end_clean[];  // clears buffer and closes buffering
2 và
ob_start[];  // turns on output buffering
$foo->render[];  // output goes only to buffer
$output = ob_get_contents[];  // stores buffer contents to the variable
ob_end_clean[];  // clears buffer and closes buffering
3 có thể được thay thế bằng một chức năng duy nhất
ob_start[];  // turns on output buffering
$foo->render[];  // output goes only to buffer
$output = ob_get_contents[];  // stores buffer contents to the variable
ob_end_clean[];  // clears buffer and closes buffering
4, tên của nó đã biến mất
ob_start[];  // turns on output buffering
$foo->render[];  // output goes only to buffer
$output = ob_get_contents[];  // stores buffer contents to the variable
ob_end_clean[];  // clears buffer and closes buffering
5 mặc dù nó thực sự tắt bộ đệm đầu ra

$output = ob_get_clean[];  // stores buffer content into variable and turns off buffering

Trong các ví dụ trên, bộ đệm nhận được không được gửi đến đầu ra. Nếu bạn muốn gửi nó, hãy sử dụng

ob_start[];  // turns on output buffering
$foo->render[];  // output goes only to buffer
$output = ob_get_contents[];  // stores buffer contents to the variable
ob_end_clean[];  // clears buffer and closes buffering
6 thay vì
ob_start[];  // turns on output buffering
$foo->render[];  // output goes only to buffer
$output = ob_get_contents[];  // stores buffer contents to the variable
ob_end_clean[];  // clears buffer and closes buffering
3. Để có được nội dung của bộ đệm, hãy gửi nó đến đầu ra và tắt bộ đệm, lại có một chức năng duy nhất [bao gồm cả _____05 bị thiếu trong tên].
ob_start[];  // turns on output buffering
$foo->render[];  // output goes only to buffer
$output = ob_get_contents[];  // stores buffer contents to the variable
ob_end_clean[];  // clears buffer and closes buffering
9

Bộ đệm có thể được làm trống bất cứ lúc nào mà không cần phải tắt nó, sử dụng

$output = ob_get_clean[];  // stores buffer content into variable and turns off buffering
0 [xóa nó] hoặc
$output = ob_get_clean[];  // stores buffer content into variable and turns off buffering
1 [gửi nó ra đầu ra]

ob_start[];  // turns on output buffering
$foo->bar[];  // all output goes only to buffer
ob_end_clean[];  // clears buffer and closes buffering
4

Vào bộ đệm cũng được gửi đầu ra được ghi vào

$output = ob_get_clean[];  // stores buffer content into variable and turns off buffering
2, trong khi bộ đệm có thể tránh được bằng cách ghi vào `php. //stdout` [hoặc
$output = ob_get_clean[];  // stores buffer content into variable and turns off buffering
3], chỉ khả dụng trong CLI, tức là. khi chạy tập lệnh từ dòng lệnh

làm tổ

Các bộ đệm có thể được lồng vào nhau, vì vậy trong khi một bộ đệm đang hoạt động, một

$output = ob_get_clean[];  // stores buffer content into variable and turns off buffering
4 khác sẽ kích hoạt một bộ đệm mới. Vì vậy,
ob_start[];  // turns on output buffering
$foo->render[];  // output goes only to buffer
$output = ob_get_contents[];  // stores buffer contents to the variable
ob_end_clean[];  // clears buffer and closes buffering
6 và
$output = ob_get_clean[];  // stores buffer content into variable and turns off buffering
1 không thực sự gửi bộ đệm tới đầu ra mà tới bộ đệm chính. Và chỉ khi không có bộ đệm cha, nội dung mới được gửi tới trình duyệt hoặc thiết bị đầu cuối

Do đó, điều quan trọng là tắt tính năng đệm, ngay cả khi xảy ra ngoại lệ

ob_start[];  // turns on output buffering
$foo->render[];  // output goes only to buffer
$output = ob_get_contents[];  // stores buffer contents to the variable
ob_end_clean[];  // clears buffer and closes buffering
0

Kích thước bộ đệm [kích thước khối]

Bộ đệm cũng có thể cải thiện hiệu suất máy chủ khi PHP sẽ không gửi từng ________ 01 tới trình duyệt mà sẽ gửi các khối dữ liệu lớn hơn, vì . Chỉ cần gọi ở đầu tập lệnh.

ob_start[];  // turns on output buffering
$foo->render[];  // output goes only to buffer
$output = ob_get_contents[];  // stores buffer contents to the variable
ob_end_clean[];  // clears buffer and closes buffering
2

Khi kích thước bộ đệm vượt quá 4096 byte, PHP sẽ tự động thực thi

$output = ob_get_clean[];  // stores buffer content into variable and turns off buffering
8, tức là. bộ đệm được làm trống và gửi đi. Điều tương tự cũng có thể đạt được bằng cách đặt chỉ thị
$output = ob_get_clean[];  // stores buffer content into variable and turns off buffering
9, chỉ thị này bị bỏ qua trong CLI

Hãy cẩn thận, nếu bạn bắt đầu lưu vào bộ đệm mà không có kích thước khối [tức là. một

$output = ob_get_clean[];  // stores buffer content into variable and turns off buffering
4 đơn giản] nó sẽ khiến trang không được gửi liên tục mà gửi một lần ở cuối tập lệnh, do đó máy chủ sẽ phản hồi rất chậm

tiêu đề HTTP

Bộ đệm đầu ra không ảnh hưởng đến tiêu đề HTTP, chúng được xử lý theo cách khác. Tuy nhiên, do bộ đệm, bạn có thể gửi tiêu đề ngay cả sau khi đầu ra đã được gửi, vì nó vẫn còn trong bộ đệm. Tuy nhiên, bạn không nên dựa vào tác dụng phụ này vì không có gì chắc chắn khi đầu ra vượt quá kích thước bộ đệm

Lỗ hổng bảo mật

Khi tập lệnh PHP kết thúc, tất cả các bộ đệm đang chờ xử lý sẽ ghi nội dung của nó vào đầu ra. Đây có thể coi là lỗ hổng bảo mật khó chịu. Nếu bạn đang chuẩn bị dữ liệu nhạy cảm trong bộ đệm không dành cho đầu ra và xảy ra lỗi, PHP sẽ ghi dữ liệu đó vào đầu ra. Giải pháp là sử dụng trình xử lý tùy chỉnh

ob_start[];  // turns on output buffering
$foo->render[];  // output goes only to buffer
$output = ob_get_contents[];  // stores buffer contents to the variable
ob_end_clean[];  // clears buffer and closes buffering
6

xử lý tùy chỉnh

Bạn có thể đặt trình xử lý tùy chỉnh của riêng mình, tức là. một chức năng xử lý nội dung của bộ đệm trước khi gửi đi

ob_start[];  // turns on output buffering
$foo->render[];  // output goes only to buffer
$output = ob_get_contents[];  // stores buffer contents to the variable
ob_end_clean[];  // clears buffer and closes buffering
7

Ngoài ra, trình xử lý triệu gọi

$output = ob_get_clean[];  // stores buffer content into variable and turns off buffering
0 và
ob_start[];  // turns on output buffering
$foo->render[];  // output goes only to buffer
$output = ob_get_contents[];  // stores buffer contents to the variable
ob_end_clean[];  // clears buffer and closes buffering
3, nhưng sẽ loại bỏ đầu ra. Trình xử lý có thể xác định hàm nào được gọi và phản hồi nó thông qua tham số thứ hai
ob_start[];  // turns on output buffering
$foo->bar[];  // all output goes only to buffer
ob_end_clean[];  // clears buffer and closes buffering
43, là một mặt nạ bit [kể từ PHP 5. 4]

  • ob_start[];  // turns on output buffering
    $foo->bar[];  // all output goes only to buffer
    ob_end_clean[];  // clears buffer and closes buffering
    44 khi bộ đệm được bật
  • ob_start[];  // turns on output buffering
    $foo->bar[];  // all output goes only to buffer
    ob_end_clean[];  // clears buffer and closes buffering
    45 khi tắt bộ đệm
  • ob_start[];  // turns on output buffering
    $foo->bar[];  // all output goes only to buffer
    ob_end_clean[];  // clears buffer and closes buffering
    46 khi gọi
    $output = ob_get_clean[];  // stores buffer content into variable and turns off buffering
    1 [chứ không phải
    ob_start[];  // turns on output buffering
    $foo->render[];  // output goes only to buffer
    $output = ob_get_contents[];  // stores buffer contents to the variable
    ob_end_clean[];  // clears buffer and closes buffering
    6 hoặc
    ob_start[];  // turns on output buffering
    $foo->render[];  // output goes only to buffer
    $output = ob_get_contents[];  // stores buffer contents to the variable
    ob_end_clean[];  // clears buffer and closes buffering
    9]
  • ob_start[];  // turns on output buffering
    $foo->render[];  // output goes only to buffer
    $output = ob_get_contents[];  // stores buffer contents to the variable
    ob_end_clean[];  // clears buffer and closes buffering
    00 khi gọi
    $output = ob_get_clean[];  // stores buffer content into variable and turns off buffering
    0,
    ob_start[];  // turns on output buffering
    $foo->render[];  // output goes only to buffer
    $output = ob_get_contents[];  // stores buffer contents to the variable
    ob_end_clean[];  // clears buffer and closes buffering
    3 và 
    ob_start[];  // turns on output buffering
    $foo->render[];  // output goes only to buffer
    $output = ob_get_contents[];  // stores buffer contents to the variable
    ob_end_clean[];  // clears buffer and closes buffering
    4
  • ob_start[];  // turns on output buffering
    $foo->render[];  // output goes only to buffer
    $output = ob_get_contents[];  // stores buffer contents to the variable
    ob_end_clean[];  // clears buffer and closes buffering
    04 tự động
    $output = ob_get_clean[];  // stores buffer content into variable and turns off buffering
    8

Các giai đoạn bắt đầu, kết thúc và tuôn ra [tương ứng. sạch] có thể xảy ra đồng thời. Nó có thể được phân biệt bằng cách sử dụng toán tử nhị phân

ob_start[];  // turns on output buffering
$foo->render[];  // output goes only to buffer
$output = ob_get_contents[];  // stores buffer contents to the variable
ob_end_clean[];  // clears buffer and closes buffering
06

ob_start[];  // turns on output buffering
$foo->render[];  // output goes only to buffer
$output = ob_get_contents[];  // stores buffer contents to the variable
ob_end_clean[];  // clears buffer and closes buffering
4

Giai đoạn

ob_start[];  // turns on output buffering
$foo->render[];  // output goes only to buffer
$output = ob_get_contents[];  // stores buffer contents to the variable
ob_end_clean[];  // clears buffer and closes buffering
04 chỉ xảy ra khi kích thước bộ đệm đã bị vượt quá. Nó là một tuôn ra tự động. Hãy cẩn thận, hằng số
ob_start[];  // turns on output buffering
$foo->render[];  // output goes only to buffer
$output = ob_get_contents[];  // stores buffer contents to the variable
ob_end_clean[];  // clears buffer and closes buffering
04 có giá trị bằng 0, vì vậy bạn không thể sử dụng toán tử nhị phân, nhưng

ob_start[];  // turns on output buffering
$foo->render[];  // output goes only to buffer
$output = ob_get_contents[];  // stores buffer contents to the variable
ob_end_clean[];  // clears buffer and closes buffering
7

Trình xử lý có thể không hỗ trợ tất cả các hoạt động.

$output = ob_get_clean[];  // stores buffer content into variable and turns off buffering
4 có thể chỉ định các hoạt động được hỗ trợ bằng mặt nạ bit trong tham số thứ ba

  • ob_start[];  // turns on output buffering
    $foo->render[];  // output goes only to buffer
    $output = ob_get_contents[];  // stores buffer contents to the variable
    ob_end_clean[];  // clears buffer and closes buffering
    20 – bạn có thể gọi hàm
    $output = ob_get_clean[];  // stores buffer content into variable and turns off buffering
    0 và hàm liên quan
  • ob_start[];  // turns on output buffering
    $foo->render[];  // output goes only to buffer
    $output = ob_get_contents[];  // stores buffer contents to the variable
    ob_end_clean[];  // clears buffer and closes buffering
    22 – bạn có thể gọi số
    $output = ob_get_clean[];  // stores buffer content into variable and turns off buffering
    1
  • ob_start[];  // turns on output buffering
    $foo->render[];  // output goes only to buffer
    $output = ob_get_contents[];  // stores buffer contents to the variable
    ob_end_clean[];  // clears buffer and closes buffering
    24 – có thể tắt bộ đệm
  • ob_start[];  // turns on output buffering
    $foo->render[];  // output goes only to buffer
    $output = ob_get_contents[];  // stores buffer contents to the variable
    ob_end_clean[];  // clears buffer and closes buffering
    25 – là sự kết hợp của cả ba cờ, hành vi mặc định

Những cờ này có thể được sử dụng mà không cần trình xử lý riêng. Ví dụ: nếu tôi muốn ghi đầu ra vào một biến, việc bỏ qua cờ

ob_start[];  // turns on output buffering
$foo->render[];  // output goes only to buffer
$output = ob_get_contents[];  // stores buffer contents to the variable
ob_end_clean[];  // clears buffer and closes buffering
22 sẽ cho biết rằng bộ đệm không thể [có lẽ do nhầm lẫn] gửi đến đầu ra qua
$output = ob_get_clean[];  // stores buffer content into variable and turns off buffering
1. Tuy nhiên, bạn có thể làm như vậy thông qua
ob_start[];  // turns on output buffering
$foo->render[];  // output goes only to buffer
$output = ob_get_contents[];  // stores buffer contents to the variable
ob_end_clean[];  // clears buffer and closes buffering
6 hoặc
ob_start[];  // turns on output buffering
$foo->render[];  // output goes only to buffer
$output = ob_get_contents[];  // stores buffer contents to the variable
ob_end_clean[];  // clears buffer and closes buffering
9, vì vậy nó sẽ mất đi một số ý nghĩa

Tương tự, việc không có cờ

ob_start[];  // turns on output buffering
$foo->render[];  // output goes only to buffer
$output = ob_get_contents[];  // stores buffer contents to the variable
ob_end_clean[];  // clears buffer and closes buffering
20 tránh xóa bộ đệm, nhưng một lần nữa, nó không hoạt động

Và cuối cùng, sự vắng mặt của

ob_start[];  // turns on output buffering
$foo->render[];  // output goes only to buffer
$output = ob_get_contents[];  // stores buffer contents to the variable
ob_end_clean[];  // clears buffer and closes buffering
24 có nghĩa là người dùng không thể tắt bộ đệm. Một ví dụ về trình xử lý thích hợp để không thể xóa được, là
ob_start[];  // turns on output buffering
$foo->render[];  // output goes only to buffer
$output = ob_get_contents[];  // stores buffer contents to the variable
ob_end_clean[];  // clears buffer and closes buffering
62 nén đầu ra và do đó giảm lưu lượng và tăng tốc độ truyền dữ liệu. Khi bộ đệm này mở ra, nó sẽ gửi tiêu đề HTTP
ob_start[];  // turns on output buffering
$foo->render[];  // output goes only to buffer
$output = ob_get_contents[];  // stores buffer contents to the variable
ob_end_clean[];  // clears buffer and closes buffering
63 và tất cả đầu ra khác phải được nén. Vì vậy, bộ đệm không được loại bỏ

Cách sử dụng đúng là

ob_start[];  // turns on output buffering
$foo->render[];  // output goes only to buffer
$output = ob_get_contents[];  // stores buffer contents to the variable
ob_end_clean[];  // clears buffer and closes buffering
0

Tính năng nén đầu ra cũng có thể được kích hoạt bằng lệnh

ob_start[];  // turns on output buffering
$foo->render[];  // output goes only to buffer
$output = ob_get_contents[];  // stores buffer contents to the variable
ob_end_clean[];  // clears buffer and closes buffering
64, lệnh này sẽ bật tính năng lưu vào bộ đệm bằng một trình xử lý khác [tôi không biết chúng khác nhau như thế nào], nhưng thật không may là bộ đệm có thể tháo rời. Bởi vì nó phù hợp để nén tất cả các tệp văn bản, không chỉ đầu ra PHP, tốt hơn là nên kích hoạt tính năng nén trên máy chủ HTTP

Chủ Đề