Hàm headers_sent () trong PHP
Thí dụ
Nếu không có tiêu đề nào được gửi, hãy gửi một tiêu đề:
<?php
if (!headers_sent()) {
header("Location: https://www.w3schools.com/");
exit;
}
?>
<html>
<body>
...
...
Định nghĩa và Cách sử dụng
Hàm headers_sent () kiểm tra xem / nơi các tiêu đề đã được gửi hay chưa.
Cú pháp
headers_sent(file,line)
Giá trị tham số
Parameter | Description |
---|---|
file | Optional. If the file and line parameters are set, headers_sent() will put the PHP source file name and line number where output started in the file and line variables |
line | Optional. Specifies the line number where the output started |
Chi tiết kỹ thuật
Giá trị trả lại: | TRUE nếu tiêu đề HTTP đã được gửi, nếu không thì FALSE |
---|---|
Phiên bản PHP: | 4.0+ |
PHP Changelog: | PHP 4.3: Đã thêm tệp và tham số dòng tùy chọn |
Các ví dụ khác
Thí dụ
Sử dụng tệp tùy chọn và các tham số dòng:
<?php
// $file and $line are passed in for later use
// Do not assign them values beforehand
if (!headers_sent($file, $line))
{
header("Location: https://www.w3schools.com/");
exit;
// Trigger an error here
}
else
{
echo "Headers sent in $file on line $line";
exit;
}
?>
<html>
<body>
...
...
❮ Tham chiếu mạng PHP