如何防止 PHP 的 HTML 输出显示在浏览器中


How do I prevent HTML output from PHP to show up in the browser?

我对PHP很陌生,所以我怀疑这是一个愚蠢的错误。我四处寻找有类似问题的人,但找不到。

所以我有一个PHP文件,应该从模板输出一些HTML(通过Smarty)。我看到的不是在Chrome中呈现的HTML,而是HTML文本本身。这是我正在使用的PHP代码:

<?php
header("Content-type: text/html; charset=utf-8");
ob_start();
include_once '../api/get_article.php';
$a_json = ob_get_clean();
$data = json_decode($a_json, true);
require('./libs/Smarty.class.php');
$smarty = new Smarty();
$smarty->template_dir = './templates/';
$smarty->compile_dir = './templates_c';
$smarty->assign("title_text",$data['title']);
$smarty->display('content.tpl');
?>

我认为这是编码问题,但我确保所有内容都使用 UTF-8(即 MYSQL、HTTP 标头和模板文件)。还能是什么?

Smarty 或 get_article.php 脚本必须在某个时候为您设置内容类型标头。尝试移动线条

header("Content-type: text/html; charset=utf-8");

一直到display函数的正上方,以确保它覆盖之前所做的任何更改