header() 重定向用户,常用的有两种方式
1、 header('Location: url'); url重定向 相当于jsp sendRedirect()
2、header('Content-Type:text/html;charset=utf'); //页面编码
使用header函数必须注意:header函数之前不能有输出<?php
echo 'hello';
header('Location:header01.php'); // 重定向用户
?>
客户端运行结果:
helloWarning: Cannot modify header information - headers already sent
要解决这个问题可以调用ob_start()缓存<?php
ob_start(); // 缓存
echo 'hello';
header('Location:header01.php'); // 重定向用户
?>
程序正常运行缓存后再在运行。但是不推荐这么使用
需要注意的是php代码之前不能有html输出<?php
// <?php 上有空行
?>
Warning: Cannot modify header information - headers already sent2、header('Content-Type:text/html;charset=utf');
中文乱码的问题: php文件是什么编码,header的编码应该与其相同,否则会出现中文乱码
header('Content-Type:text/html;charset=utf-8');
echo '我是中文';
//浏览器显示:鎴戞槸涓枃
开始我的zend studio(或php页面编码)默认编码是gbk,而header编码为utf-8,所以出现了乱码