user377628
user377628

Reputation:

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

I'm quite new to PHP, so I suspect a stupid mistake. I've looked around for someone with a similar problem, but couldn't find any.

So I have a PHP file that's supposed to output some HTML from a template (via Smarty). Instead of seeing the HTML rendered in Chrome, I see the HTML text itself. Here's the PHP code I'm using:

<?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');

?>

I thought that it was a problem with encoding, but I made sure that everything uses UTF-8 (that is, MYSQL, the HTTP header, and the template file). What else could it be?

Upvotes: 6

Views: 2035

Answers (1)

Hubro
Hubro

Reputation: 59408

Smarty or the get_article.php script must be setting the content type header for you at some point. Try moving the line

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

All the way down to right above the display function to make sure it overrides any changes made earlier

Upvotes: 2

Related Questions