king71 发表于 2018-12-21 11:32:56

java 到 php入门:header()重定向

  header() 重定向用户,常用的有两种方式
  1、 header('Location: url'); url重定向 相当于jsp sendRedirect()
  2、header('Content-Type:text/html;charset=utf'); //页面编码
  使用header函数必须注意:header函数之前不能有输出
  客户端运行结果:
  hello
Warning: Cannot modify header information - headers already sent
  

  要解决这个问题可以调用ob_start()缓存
  程序正常运行缓存后再在运行。但是不推荐这么使用
  需要注意的是php代码之前不能有html输出

  Warning: Cannot modify header information - headers already sent
2、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,所以出现了乱码
  




页: [1]
查看完整版本: java 到 php入门:header()重定向