How to prevent HTML output from PHP in a browser?

I am very new to PHP, so I suspect a stupid mistake. I was looking for someone with a similar problem, but could not find.

So, I have a PHP file that should output some HTML from a template (via Smarty ). Instead of viewing the HTML presented in Chrome, I see the HTML text itself. Here is 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 this was an encoding problem, but I made sure everything uses UTF-8 (i.e. MYSQL, HTTP header and template file). What else could be?

+6
source share
1 answer

A get_article.php or get_article.php script should at some point set a content type header. Try moving the line

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

Everywhere down to the display function, to make sure that it cancels previous changes

+2
source

Source: https://habr.com/ru/post/914996/


All Articles