我一直在本地(在 MAMP 中)从事一个项目,并决定将其在线上传到我的 LAMP 服务器。该服务器之前可以正常工作,但在上传我的所有新内容后,它不会呈现 HTML,只是将源代码显示为纯文本。
所有 PHP 似乎都工作正常,因为它显示了正确的内容。
无济于事,我尝试使用 header("Content-Type: text/html")
显式设置内容类型。然而,当我删除 PHP 包含并直接使用 HTML 时,它工作正常。
我正在使用一个包含其他几个文件的 index.php 文件:
<?php
include_once 'header.php';
include_once 'inc/window.php';
createWindow();
include_once 'footer.php';
?>
也许我的 header.php 文件也可能包含一些线索:
<?php session_start(); ?>
<!doctype html>
<html>
<?php
include_once 'inc/files.php';
$files = json_decode(getFiles($ids));
?>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<?php
$title = '';
foreach($files as $i=>$file){
$title .= $file->content . ' ';
if(count($file) > 1 && $i < count($file)-1) $title .= '+ ';
}
?>
<title>* <?php echo strtolower($title); ?></title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<link rel="stylesheet/less" type="text/css" href="css/main.css">
<script src="js/libs/modernizr-2.0.6.min.js"></script>
</head>
<body>
我的环境正在运行 PHP5+(我相信是 5.3?)、Ubuntu 和 Apache2。我也有 webmin 和 SSH 访问权限。
如有任何想法,我们将不胜感激!
强尼
最佳答案
哇——经过更多的调试后发现了!
问题与 files.php 文件中设置的内容类型有关:
if(!headers_sent()) header('Content-Type: application/json');
出于某种原因,在发送页面标题之前遇到了这种情况。
我不确定为什么我的本地网站上没有发生这种情况,如果有人可以就此发表意见,我很乐意对该答案投赞成票! :) 我暂时删除了它,因为它并不是真正必要的。
谢谢S/O
关于PHP 生成呈现为纯文本的 HTML 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16181139/