所以我在为我的网站做错误处理。我希望能够通过使用
include_once("errorhandler.php");
到目前为止,它工作得很好,如果发生错误,它会显示错误,但是我面临另一个问题。我创建了一个名为"test.php“的php文件,如下所示:
<?php
include("errorhandler.php");
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<h1>My awesome page :D</h1>
<?php
mysql_connect("test","test");
opendir("test");
?>
</body>
</html>现在的问题是,在脚本崩溃并启动错误处理程序之前,会显示错误,但也会显示头部,因为它可以执行该部分。
它使页面看起来像这样:

基本上,我需要错误处理程序在编写Html之前清除所有内容。
我试过用<script> document.write('');</script>
在我的错误处理方法的开始,没有运气。
所以现在我被困在这个问题上,对如何解决这个问题有什么想法吗?
发布于 2013-03-15 10:52:32
在错误处理程序开始时使用ob_clean();,所以当它发生时,它将清除页面!
发布于 2013-03-15 10:48:23
放置die();在包含之后,将停止页的其余部分运行,或者围绕异常包装if,因此if (exception) { //do include } else { //show normal code }
发布于 2013-03-15 10:50:58
您可以尝试将html放在一个函数中:
public function render() {
$this->ht_content = '<html> Your html </html>';
return $this->ht_content;
}然后,在错误处理程序中,您可以这样说:
if($errors) {
// Error stuff here
} else {
$ht = new html();
$ht->render();
}你可以把它做得像你想要的那样大和好:)
https://stackoverflow.com/questions/15430322
复制相似问题