Hàm headers_sent() của PHP
Ví 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.example.com.vn/");
exit;
}
?>
<html>
<body>
...
...
Định nghĩa và cách sử dụng
Hàm headers_sent() kiểm tra xem/nơi 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ả về: | TRUE nếu tiêu đề HTTP đã được gửi, FALSE nếu không |
---|---|
Phiên bản PHP: | 4.0+ |
Nhật ký thay đổi PHP: | PHP 4.3: Đã thêm các tham số dòng và tệp tùy chọn |
Thêm ví dụ
Ví dụ
Sử dụng các tham số tệp và dòng tùy chọn:
<?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.example.com.vn/");
exit;
// Trigger an error here
}
else
{
echo "Headers sent in $file on line $line";
exit;
}
?>
<html>
<body>
...
...
❮ Tham chiếu mạng PHP