create account

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

View this thread on: hive.blogpeakd.comecency.com
· @oflyhigh ·
$34.95
每天进步一点点:UTF-8 (BOM)导致的问题
之前发帖子说[正式启用eztk.net域名,见证人列表工具移至网站根目录](https://steemit.com/cn/@oflyhigh/eztk-net),但是这个站点目前只有一个[Witness List](https://www.eztk.net/witnesses.php),所以我实在不知道主页该写点啥。

![](https://steemitimages.com/DQmRkLq6rRew3mHfx4vYGWyqpC8wSebLPeC2iZCXAdpuGkR/image.png)
(图源 :[pixabay](https://pixabay.com/))


既然没啥写的,就先跳转到[Witness List](https://www.eztk.net/witnesses.php),其实跳转的方法有好多种,比如用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)`
>![](https://cdn.steemitimages.com/DQmXyjLyTWZ1LuLuj8jXGV682CbRoMYx9j9sSKg2xts8bFb/image.png)

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

![](https://cdn.steemitimages.com/DQmVus28gb3quKVB4WDj1MfxsyCfSU3hHKXcDQknYvcf7LB/image.png)

![](https://cdn.steemitimages.com/DQmYkK3Tw4Z45Zi6K5C3w3MsBvVefyySxnewiPMiEWXSk8x/image.png)

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

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

有关更多关于BOM的细节,去参考[Byte order mark](https://en.wikipedia.org/wiki/Byte_order_mark)吧,我就不献丑啦。

# 相关链接

* [Byte order mark](https://en.wikipedia.org/wiki/Byte_order_mark)
* [Witness List by @oflyhigh](https://www.eztk.net/witnesses.php)
* [温故而知新:复习一下字符编码(ASCII、GB2312、Unicode、UTF-8、区位码)](https://steemit.com/encoding/@oflyhigh/ascii-gb2312-unicode-utf-8)
* [纸上得来终觉浅:来验证一下编码的问题](https://steemit.com/encoding/@oflyhigh/hydbr)
* [每天进步一点点:VC中不同编码间字符串转换](https://steemit.com/encoding/@oflyhigh/vc)

----
<center><strong>Vote For Me As Witness</strong>
https://steemit.com/~witnesses type in **`oflyhigh`** and click ***`VOTE`***
[![](https://cdn.steemitimages.com/DQmX5NysqT44FBa3bhuWqQ69nAbseu8Nt5YQPn2pYejPVxA/image.png)](https://steemit.com/~witnesses)
[Vote @oflyhigh via Steemconnect](https://steemconnect.com/sign/account-witness-vote?witness=oflyhigh&approve=1)
<strong>Thank you!</strong></center>
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 73 others
properties (23)
authoroflyhigh
permlinkutf-8-bom
categorycn
json_metadata{"tags":["cn","programming","bom","utf-8"],"image":["https://steemitimages.com/DQmRkLq6rRew3mHfx4vYGWyqpC8wSebLPeC2iZCXAdpuGkR/image.png","https://cdn.steemitimages.com/DQmXyjLyTWZ1LuLuj8jXGV682CbRoMYx9j9sSKg2xts8bFb/image.png","https://cdn.steemitimages.com/DQmVus28gb3quKVB4WDj1MfxsyCfSU3hHKXcDQknYvcf7LB/image.png","https://cdn.steemitimages.com/DQmYkK3Tw4Z45Zi6K5C3w3MsBvVefyySxnewiPMiEWXSk8x/image.png","https://cdn.steemitimages.com/DQmX5NysqT44FBa3bhuWqQ69nAbseu8Nt5YQPn2pYejPVxA/image.png"],"links":["https://steemit.com/cn/@oflyhigh/eztk-net","https://www.eztk.net/witnesses.php","https://pixabay.com/","https://en.wikipedia.org/wiki/Byte_order_mark","https://steemit.com/encoding/@oflyhigh/ascii-gb2312-unicode-utf-8","https://steemit.com/encoding/@oflyhigh/hydbr","https://steemit.com/encoding/@oflyhigh/vc","https://steemit.com/~witnesses","https://steemconnect.com/sign/account-witness-vote?witness=oflyhigh&approve=1"],"app":"steemit/0.1","format":"markdown"}
created2018-11-01 02:52:42
last_update2018-11-01 02:52:42
depth0
children7
last_payout2018-11-08 02:52:42
cashout_time1969-12-31 23:59:59
total_payout_value26.816 HBD
curator_payout_value8.130 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length2,531
author_reputation6,410,924,137,254,968
root_title"每天进步一点点:UTF-8 (BOM)导致的问题"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id74,448,082
net_rshares31,136,761,371,899
author_curate_reward""
vote details (137)
@ilovecoding ·
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! 
 ![](https://codingforspeed.com/images/i-love-coding.jpg) 
*Reply !stop to disable the comment. Thanks!*
👍  
properties (23)
authorilovecoding
permlink20181101t025253735z
categorycn
json_metadata{"tags":["ilovecoding"],"app":"ilovecoding"}
created2018-11-01 02:52:54
last_update2018-11-01 02:52:54
depth1
children0
last_payout2018-11-08 02:52:54
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length323
author_reputation40,845,997,808
root_title"每天进步一点点:UTF-8 (BOM)导致的问题"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id74,448,088
net_rshares392,341,260
author_curate_reward""
vote details (1)
@julian2013 ·
$0.05
题图用了无数次了……
👍  ,
properties (23)
authorjulian2013
permlinkre-oflyhigh-utf-8-bom-20181101t025741862z
categorycn
json_metadata{"community":"busy","app":"busy/2.5.6","format":"markdown","tags":["cn"],"users":[],"links":[],"image":[]}
created2018-11-01 02:57:48
last_update2018-11-01 02:57:48
depth1
children4
last_payout2018-11-08 02:57:48
cashout_time1969-12-31 23:59:59
total_payout_value0.042 HBD
curator_payout_value0.012 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length10
author_reputation398,595,897,784,320
root_title"每天进步一点点:UTF-8 (BOM)导致的问题"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id74,448,264
net_rshares49,306,524,528
author_curate_reward""
vote details (2)
@crypto.talk ·
$0.06
哈哈这个叫做branding

Posted using [Partiko Android](https://steemit.com/@partiko-android)
👍  
properties (23)
authorcrypto.talk
permlinkcrypto-talk-re-julian2013-re-oflyhigh-utf-8-bom-20181101t064309221z
categorycn
json_metadata{"app":"partiko"}
created2018-11-01 06:43:09
last_update2018-11-01 06:43:09
depth2
children3
last_payout2018-11-08 06:43:09
cashout_time1969-12-31 23:59:59
total_payout_value0.042 HBD
curator_payout_value0.013 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length84
author_reputation5,672,582,596,884
root_title"每天进步一点点:UTF-8 (BOM)导致的问题"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id74,457,478
net_rshares48,769,126,721
author_curate_reward""
vote details (1)
@black109 ·
👍
properties (22)
authorblack109
permlinkre-cryptotalk-crypto-talk-re-julian2013-re-oflyhigh-utf-8-bom-20181102t132608372z
categorycn
json_metadata{"tags":["cn"],"app":"steemit/0.1"}
created2018-11-02 13:26:12
last_update2018-11-02 13:26:12
depth3
children0
last_payout2018-11-09 13:26:12
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1
author_reputation6,373,909,098
root_title"每天进步一点点:UTF-8 (BOM)导致的问题"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd0
post_id74,542,177
net_rshares0
@oflyhigh ·
机智👍
properties (22)
authoroflyhigh
permlinkre-cryptotalk-crypto-talk-re-julian2013-re-oflyhigh-utf-8-bom-20181101t140215028z
categorycn
json_metadata{"tags":["cn"],"app":"steemit/0.1"}
created2018-11-01 14:02:15
last_update2018-11-01 14:02:15
depth3
children1
last_payout2018-11-08 14:02:15
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length3
author_reputation6,410,924,137,254,968
root_title"每天进步一点点:UTF-8 (BOM)导致的问题"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id74,479,369
net_rshares0
@sunai ·
$0.05
码农的事儿真是不懂呀

Posted using [Partiko Android](https://steemit.com/@partiko-android)
👍  
properties (23)
authorsunai
permlinksunai-re-oflyhigh-utf-8-bom-20181101t042936958z
categorycn
json_metadata{"app":"partiko"}
created2018-11-01 04:29:39
last_update2018-11-01 04:29:39
depth1
children0
last_payout2018-11-08 04:29:39
cashout_time1969-12-31 23:59:59
total_payout_value0.041 HBD
curator_payout_value0.013 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length80
author_reputation17,899,844,015,065
root_title"每天进步一点点:UTF-8 (BOM)导致的问题"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id74,451,791
net_rshares48,785,421,787
author_curate_reward""
vote details (1)