header('Content-Type:text / plain');

时间:2019-11-01 09:50:21

标签: php html escaping content-type

要在我的php代码中使用逸出字符,我使用:header('Content-Type:text / plain');但这导致我的html代码在浏览器中显示为纯文本。如何将Content-Type改回text / html尝试从我的php代码以及其后的HTML代码中切换Content-Type。

<?php
      header('Content-Type: text/plain');
      echo 'Date Now:' . "\t" . date('m-d-Y') . "\n"; 
      header('Content-Type: text/html']);   
?>
<!DOCTYPE html>
<html lang="en" dir="ltr">
      <head>
             <title>date.php</title>
      </head>
      <body>
            ......
      </body>
</html>

我希望浏览器可以正确解释HTML代码,但它会显示为纯文本。

1 个答案:

答案 0 :(得分:0)

如图所示,您的代码中只能有一个content-type标头-否则,HTML将无效,并且在文本字符串之前使用文本字符串

我认为您打算这样做

<?php
      header('Content-Type: text/html']);   
?>
<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <title>date.php</title>
  </head>
  <body>
    <pre>Date Now:<?= "\t". date('m-d-Y')."\n" ?></pre> 
  </body>
</html>