[PHP 4 , PHP 5]
ob_get_contents -- Trả lại nội dung của bộ đệm đầu raSự miêu tả
chuỗi ob_get_contents [ void ]Điều này sẽ trả về nội dung của bộ đệm đầu ra hoặc FALSE, nếu bộ đệm đầu ra không hoạt động
Xem thêm ob_start[] và ob_get_length[]
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. Điều này hữu ích cho nhiều nhiệm vụ khác nhauNgă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
Chụp đầ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
9Bộ đệ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
4Và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ệnhlà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ốiDo đó, đ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
0Kích thước bộ đệm [kích thước chunk]
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
2Khi 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. Bạn có thể đạt được điều tương tự bằng cách đặt chỉ thị , chỉ thị này bị bỏ qua trong CLIHã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ậmtiê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
5xử 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
6Ngoà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]
44 khi bộ đệm được bậtob_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ộ đệmob_start[]; // turns on output buffering $foo->bar[]; // all output goes only to buffer ob_end_clean[]; // clears buffer and closes buffering
46 khi gọiob_start[]; // turns on output buffering $foo->bar[]; // all output goes only to buffer ob_end_clean[]; // clears buffer and closes buffering
1 [chứ không phải$output = ob_get_clean[]; // stores buffer content into variable and turns off buffering
6 hoặcob_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ọiob_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,$output = ob_get_clean[]; // stores buffer content into variable and turns off 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
4ob_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ự độngob_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
8$output = ob_get_clean[]; // stores buffer content into variable and turns off buffering
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
06ob_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
3Giai đ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ưngob_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
6Trì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
20 – bạn có thể gọi hàmob_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 và hàm liên quan$output = ob_get_clean[]; // stores buffer content into variable and turns off buffering
22 – bạn có thể gọi 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
1$output = ob_get_clean[]; // stores buffer content into variable and turns off buffering
24 – có thể tắt bộ đệmob_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 địnhob_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
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ĩaTươ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 độngVà 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
52 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
53 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
0Tí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
54, 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