Λειτουργία headers_sent() PHP

Ορισμός και Χρήση

The headers_sent() function checks if the HTTP headers have been sent and where they were sent.

If headers have been sent, it returns true, otherwise it returns false.

Γραμματική

headers_sent(file,line)
Parameters Περιγραφή
file,line

προαιρετικά.

If set file και line Parameters, headers_sent() will store the name of the PHP source file and line number at the start of output in the file and line variables.

Συμβουλές και Σημειώσεις

Σημείωση:Once the header block has been sent, it cannot be used Λειτουργία header() Για να στείλετε άλλες κεφαλίδες. Η χρήση αυτής της λειτουργίας μπορεί至少 να αποφύγει τα σφάλματα που σχετίζονται με τα HTTP κεφαλίδες.

Σημείωση:προαιρετικά file και line The parameter is a new addition in PHP 4.3.

Instance

Παράδειγμα 1

<?php
// If headers have not been sent, send one
if (!headers_sent())
  {
  header("Location: http://www.codew3c.com/");
  exit;
  }
?>
<html>
<body>
...
...

Παράδειγμα 2

Χρήση των επιλογικών παραμέτρων file και line:

<?php
// Pass $file and $line for future use
// Do not pre-assign them
if (!headers_sent($file, $line))
  {
  header("Location: http://www.codew3c.com/");
  exit;
  // Trigger an error here
  }
else
  {
  echo "Headers sent in $file on line $line";
  exit;
  }
?>
<html>
<body>
...
...