Tiêu đề php utf8

Khi bạn code website html và gõ tiếng việt, khi bạn nhấn Save back nó sẽ báo lỗi ko Save được. Bạn có thể chèn đoạn mã sau vào thẻ Head. And try press Save back nhé

VD




Kiểm tra thử tiếng việt tại đây, và nhấn Lưu

$a = array['',"'bar'",'"baz"','&blong&', "\xc3\xa9"];

echo "Normal: ",  json_encode[$a], "\n";
echo "Tags: ",    json_encode[$a, JSON_HEX_TAG], "\n";
echo "Apos: ",    json_encode[$a, JSON_HEX_APOS], "\n";
echo "Quot: ",    json_encode[$a, JSON_HEX_QUOT], "\n";
echo "Amp: ",     json_encode[$a, JSON_HEX_AMP], "\n";
echo "Unicode: ", json_encode[$a, JSON_UNESCAPED_UNICODE], "\n";
echo "All: ",     json_encode[$a, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE], "\n\n";

$b = array[];

echo "Empty array output as array: ", json_encode[$b], "\n";
echo "Empty array output as object: ", json_encode[$b, JSON_FORCE_OBJECT], "\n\n";

$c = array[array[1,2,3]];

echo "Non-associative array output as array: ", json_encode[$c], "\n";
echo "Non-associative array output as object: ", json_encode[$c, JSON_FORCE_OBJECT], "\n\n";

$d = array['foo' => 'bar', 'baz' => 'long'];

________số 8_______

Ví dụ trên sẽ xuất ra

Trong khi làm việc với PHP, đôi khi cần thiết lập Header thành UTF-8. Trong hướng dẫn này, chúng tôi sẽ minh họa cho bạn cách thực hiện điều đó với sự trợ giúp của hàm header[]. Cái sau được sử dụng để gửi tiêu đề HTTP thô

Vì vậy, hãy xem cách đặt Tiêu đề thành UTF-8 bằng cách sử dụng tiêu đề[]

Bên dưới, bạn có thể xem cách sử dụng hàm header[] để đặt Header thành UTF-8

header['Content-Type: text/html; charset=utf-8'];

Vì vậy, bất cứ lúc nào trước khi gửi bất kỳ đầu ra nào cho máy khách, bạn cần chạy mã ở trên

Tất cả những gì bạn cần là thêm nó vào đầu trang. Cẩn thận không để trống bất kỳ khoảng trống nào trước nó, vì nó sẽ dẫn đến lỗi. Để kiểm tra xem các tiêu đề đã được gửi hay chưa, chỉ cần áp dụng chức năng headers_sent

Chức năng này hoạt động trên tất cả các phiên bản PHP

Nó được sử dụng để gửi một tiêu đề PHP thô

Một điều cần thiết cần lưu ý. hàm header[] phải được gọi trước khi gửi bất kỳ đầu ra thực tế nào

Để biết thêm ví dụ về cách sử dụng hàm header[], bạn có thể tham khảo nguồn này

Hàm PHP này cho phép kiểm tra xem các tiêu đề đã được gửi ở đâu hay chưa. Nhưng lưu ý rằng sau khi khối tiêu đề đã được gửi, không thể thêm dòng tiêu đề nào nữa với chức năng tiêu đề

After lots of research and testing, I'd like to share my findings about my problems with Internet Explorer and file downloads.

  Take a look at this code, which replicates the normal download of a Javascript:

if[strstr[$_SERVER["HTTP_USER_AGENT"],"MSIE"]==false] {
  header["Content-type: text/javascript"];
  header["Content-Disposition: inline; filename=\"download.js\""];
  header["Content-Length: ".filesize["my-file.js"]];
} else {
  header["Content-type: application/force-download"];
  header["Content-Disposition: attachment; filename=\"download.js\""];
  header["Content-Length: ".filesize["my-file.js"]];
}
header["Expires: Fri, 01 Jan 2010 05:00:00 GMT"];
if[strstr[$_SERVER["HTTP_USER_AGENT"],"MSIE"]==false] {
  header["Cache-Control: no-cache"];
  header["Pragma: no-cache"];
}
include["my-file.js"];
?>

Now let me explain:

  I start out by checking for IE, then if not IE, I set Content-type [case-sensitive] to JS and set Content-Disposition [every header is case-sensitive from now on] to inline, because most browsers outside of IE like to display JS inline. [User may change settings]. The Content-Length header is required by some browsers to activate download box. Then, if it is IE, the "application/force-download" Content-type is sometimes required to show the download box. Use this if you don't want your PDF to display in the browser [in IE]. I use it here to make sure the box opens. Anyway, I set the Content-Disposition to attachment because I already know that the box will appear. Then I have the Content-Length again.

  Now, here's my big point. I have the Cache-Control and Pragma headers sent only if not IE. THESE HEADERS WILL PREVENT DOWNLOAD ON IE!!! Only use the Expires header, after all, it will require the file to be downloaded again the next time. This is not a bug! IE stores downloads in the Temporary Internet Files folder until the download is complete. I know this because once I downloaded a huge file to My Documents, but the Download Dialog box put it in the Temp folder and moved it at the end. Just think about it. If IE requires the file to be downloaded to the Temp folder, setting the Cache-Control and Pragma headers will cause an error!

I hope this saves someone some time!
~Cody G.

Chủ Đề