我对 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 header 和模板文件)。还能是什么?
最佳答案
Smarty 或 get_article.php
脚本必须在某个时候为您设置内容类型 header 。尝试移动线
header("Content-type: text/html; charset=utf-8");
一直向下到 display
函数的正上方,以确保它覆盖之前所做的任何更改
关于php - 如何防止 PHP 的 HTML 输出显示在浏览器中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10467309/