每天进步一点点:UTF-8 (BOM)导致的问题

in cn •  6 years ago 

之前发帖子说正式启用eztk.net域名,见证人列表工具移至网站根目录,但是这个站点目前只有一个Witness List,所以我实在不知道主页该写点啥。


(图源 :pixabay)

既然没啥写的,就先跳转到Witness List,其实跳转的方法有好多种,比如用Apache的URL_REWRITE或者用HTML代码<meta http-equiv="refresh" content="5;url=https://www.eztk.net/witnesses.php">或者用JavaScripte的window.location.href='https://www.eztk.net/witnesses.php;

不过我最喜欢用的还是如下代码:

<?php
header('Location: https://www.eztk.net/witnesses.php');
?>

编辑index.php,填上上述代码,保存并上传到服务器,访问,然后美滋滋地等跳转,结果一片空白,并没有跳转至我要的页面,这是咋回事呢?

查看了一下error_log发现了如下内容:

[30-Oct-2018 02:44:49 UTC] PHP Warning: Cannot modify header information - headers already sent by (output started at /home/user/public_html/index.php:1) in /home/user/public_html/index.php on line 2

哦,原来是这个问题啊,这个问题几十年前刚学PHP的时候我遇到无数次了,不过是在header()之前有了其它输出嘛(字符啊、空格之类的),去掉就好了。结果我瞪大眼睛找了半天,也没找到任何字符。

会是什么原因呢?我冥思苦想,想了好久终于灵光一现,我该不会手贱把PHP文件格式设置成了UTF-8 (BOM)了吧?

点击另存为,看了一下当前的文件格式,果不其然是UTF-8 (BOM)

UTF-8UTF-8 (BOM)文件格式有啥区别呢?我们来对比一下两个文件的二进制码就知道了

经过对比我们发现,后者比前者多出来三个字节0xEF,0xBB,0xBF,这就是导致Cannot modify header information - headers already sent by的罪魁祸首啦。

那么解决起来也很简单了,把文件另存为UTF-8,就搞定啦。

有关更多关于BOM的细节,去参考Byte order mark吧,我就不献丑啦。

相关链接


Vote For Me As Witness
https://steemit.com/~witnesses type in oflyhigh and click VOTE

Vote @oflyhigh via Steemconnect
Thank you!

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  
  ·  6 years ago 

题图用了无数次了……

哈哈这个叫做branding

Posted using Partiko Android

机智👍

😎

Posted using Partiko Android

👍

  ·  6 years ago 

码农的事儿真是不懂呀

Posted using Partiko Android

Hello! Your post has been resteemed and upvoted by @ilovecoding because we love coding! Keep up good work! Consider upvoting this comment to support the @ilovecoding and increase your future rewards! ^_^ Steem On!

Reply !stop to disable the comment. Thanks!