PHP:Cannot modify header information - headers already sent by出错解决

在PHP配置文件中加入output_buffering = On
在这里插入图片描述

在header()函数前如果有输出,就必须加上打开缓冲区函数ob_start();,否则会出现PHP:Cannot modify header information - headers already sent by警告

<?php
#通过路径获取路径下的所有文件信息

#接受数据:保证安全
$dir = $_GET['dir'] ?? '';

#判断路径有效性:无效返回,进入到index.html
if (!is_dir($dir)) {
	ob_start();				#打开缓冲区
	#路径无效
	echo '路径无效!';
	#跳转
	header('Refresh:3; url=index.html');			//header不会终止代码执行
	#终止执行
	exit;
}

?>
原创文章 7 获赞 3 访问量 404

猜你喜欢

转载自blog.csdn.net/weixin_42951763/article/details/104124732