最近迁移服务器,将 PHP 升级到 8.0.10 之后,且关闭 PHP warning 报错,博客首页、文章页显示“有点尴尬诶!该页无法显示”。
一开始以为数据库导入有问题,文章没有导入。进入后台查看,文章虽然都还在,但查看文章具体页面,均是“有点尴尬诶!该页无法显示”。
恰好点开评论,遇到函数报错,无法打开评论管理界面,猜测是受 PHP 8。
回退到 PHP 7.4.23 之后,文章正常显示。
科技改变生活,编程改变世界。
最近迁移服务器,将 PHP 升级到 8.0.10 之后,且关闭 PHP warning 报错,博客首页、文章页显示“有点尴尬诶!该页无法显示”。
一开始以为数据库导入有问题,文章没有导入。进入后台查看,文章虽然都还在,但查看文章具体页面,均是“有点尴尬诶!该页无法显示”。
恰好点开评论,遇到函数报错,无法打开评论管理界面,猜测是受 PHP 8。
回退到 PHP 7.4.23 之后,文章正常显示。
CSS 属性 image-orientation 用来修正某些图片的预设方向。
当需要对图像原始状态进行操作时,这种自动修正会带来错误。尤其是在新版浏览器中该属性默认值从不生效(none)被改成了 from-image,此时导致在 Chrome v81 浏览器上处理一些带角度的图像时产出错误的信息。
/* 正确的默认值应该是0 */
image-orientation: 0deg /* 实际该值无效 */
image-orientation: none /* 实际该值生效,可以阻止浏览器自动旋转图像(disable html img auto rotation) */
/* 非 90 度的整数倍, 所以会被四舍五入到 0 度 */
image-orientation: 6.4deg
/* 相当于 270deg */
image-orientation: -90deg
/* 使用图片的 EXIF 数据 */
image-orientation: from-image
/* 旋转 90deg, 再水平翻转 */
image-orientation: 90deg flip
/* 不旋转, 只进行水平翻转 */
image-orientation: flip
/* 继承 */
image-orientation: inherit
该属性还未进入标准文档,在未来版本的浏览器中该功能的语法和行为可能随之改变。
蜜源APP这款神器搜集了很多淘宝天猫的优惠券,按照下面的步骤,有很大概率领取到大额优惠券(可能是渠道限制,在淘宝上不一定能找到):
这款软件注册时需要输入邀请码,我是别人推荐注册的。通过邀请码注册,可以免费升级永久vip,神奇-_-||。不通过邀请码,就不能注册。
一般只要不是特别小众的商品,基本上都会搜索到淘宝上的同一家店。当然不是每款商品都有优惠券。
(商品无关,纯举例)假设在淘宝上看到某款商品:
使用下来,这款APP不提供商品,但搜集了很多商品的优惠券。
蜜源 APP 注册有效邀请码 FBEMYW
除了领取优惠券,这款 APP 里还有大家发的购买攻略,尤其在电商平台做活动期间能够有效避免被坑。
也可以在蜜源里把商品分享给别人购买,这个还没试过。
JavaScript 浮点数神坑当属 0.1 + 0.2 == 0.3
为 false。
> 0.1 + 0.2 == 0.3 false > 0.1 + 0.2 0.30000000000000004 >
还有一些比较隐蔽的问题,比如 Math.round、Number.prototype.toFixed 也都不是能完全正常工作的。
> Math.round(1.105 * 100) 111 > Math.round(1.015 * 100) 101 > Math.round(1.025 * 100) 102 > 0.25.toFixed(1) '0.3' > 0.35.toFixed(1) '0.3'
Math.round、toFixed 计算出错主要因为浮点数不能精确表示。
在这里 0.35 和 1.015 的值都不准确,一个办法是转换成整数计算再除以对应的十百千;另一个办法是采用现有的 lib,比如 accounting。
> 1.015 * 100 101.49999999999999
代码运行环境
D:\node -v v8.1.0
ECMAScript 6 新增 const 和 let 命令,用来声明变量。
声明方式 | 变量提升 | 作用域 | 初始值 | 重复定义 |
const | 否 | 块级 | 需要 | 不允许 |
let | 否 | 块级 | 不需要 | 不允许 |
var | 是 | 函数级 | 不需要 | 允许 |
变量提升:const 和 let 必须先声明再使用,不支持变量提升
console.log(c1, l1, v1); // 报错 // Uncaught ReferenceError: c1 is not defined const c1 = 'c1'; let l1 = 'l1'; var v1 = 'v1';
作用域:const,let 支持块级作用域,有效避免变量覆盖
const c21 = 'c21'; let l21 = 'l21'; var v21 = 'v21'; if (0.1 + 0.2 != 0.3) { const c21 = 'c22'; let l21 = 'l22'; var v21 = 'v22'; console.log(c21, l21, v21); // 输出 c22 l22 v22 } console.log(c21, l21, v21); // 输出 c21 l21 v22
块级作用域,在外层不能直接访问内层变量
if (0.1 + 0.2 != 0.3) { const c22 = 'c22'; let l22 = 'l22'; var v22 = 'v22'; console.log(c22, l22, v22); // 输出 c22 l22 v22 } console.log(c22, l22, v22); // 报错 // Uncaught ReferenceError: c22 is not defined // 同样地, l22 is not defined
const 定义常量,该常量不能赋值,但该常量的属性可以赋值
const c231 = {}; const c232 = []; c231.name = 'seven'; c232.push(27); console.log(c231, c232); // 输出 {name: "seven"} [27] // 禁止给对象赋值,应该使用 Object.freeze const c233 = Object.freeze({}); const c234 = Object.freeze([]); c233.name = 'seven'; // 普通模式下不报错 // 严格模式下报错 // Uncaught TypeError: Cannot add property name, object is not extensible c234.push(27); // 普通模式下就会报错 // Uncaught TypeError: Cannot add property 0, object is not extensible console.log(c233, c234); // 输出 {} []
全局变量不再设置为顶层对象(window)的属性,有效避免全局变量污染
const c24 = 'c24'; let l24 = 'l24'; console.log(c24, l24); // 输出 c24 l24 console.log(window.c24, window.l24); // 输出 undefined undefined
符合预期的 for 循环
for (var i = 0; i != 3; i++) { setTimeout(function() { console.log(i); },10); } // 依次打印 3 3 3 for (let i = 0; i != 3; i++) { setTimeout(function() { console.log(i); },10); } // 依次打印,为啥呢 0 1 2
可以看到在 for 循环中使用 let 方式声明变量才是符合预期。
在 for 中每一次循环,let 都是重新声明变量,并且因为 JavaScript 引擎会记住上一次循环的值,初始化 i 时在上一轮的基础上计算。
可以看到在 for 循环中至少有两层作用域,看下面的例子更容易理解。
for (let i = 0; i != 3; i++) { let i = 'seven'; console.log(i); } console.log('eight'); // 依次打印 seven seven seven eight
初始值:const 声明的变量必须设置初始值,且不能重复赋值。
const c3 = 'c3'; let l3 = 'l3'; var v3 = 'v3'; console.log(c3, l3, v3); // 输出 c3 l3 v3 c3 = 2; // Uncaught TypeError: Assignment to constant variable l3 = 2; v3 = 2; console.log(c3, l3, v3); // 输出 c3 2 2 const c32; // 报错 // Uncaught SyntaxError: Missing initializer in const declaration
重复定义:const 和 let 不支持重复定义
const、let 缩小了变量作用域,完美避免变量污染;const 固定变量(即固定变量类型),对于弱类型 JavaScript 来说,可以明显提升性能。推荐在应用中使用 const、let 声明变量。
在网页上展示列表时经常需要对列表进行排序:按照修改/访问时间排序、按照地区、按照名称排序。
对于中文列表按照名称排序就是按照拼音排序,不能简单通过字符串比较—— ‘a’ > ‘b’——这种方式来实现。
比如比较 ‘北京’ vs ‘上海’,实际是比较 ‘běijīng’ vs ‘shànghǎi’;比较 ‘北京’ vs ‘背景’,实际是比较 ‘běijīng’ vs ‘bèijǐng’。
一般需要获取到字符串的拼音,再比较各自的拼音。
JavaScript 提供本地化文字排序,比如对中文按照拼音排序,不需要程序显示比较字符串拼音。
String.prototype.localeCompare 在不考虑多音字的前提下,基本可以完美实现按照拼音排序。
在没有出现意外的情况下,各个支持 localeCompare 的浏览器都很正常。最近将 Chrome 更新到 58.0.3029.110,突然发现中文排序不正常。
// 正常应该返回 1, 拼音 jia 在前, kai 在后 '开'.localeCompare('驾'); // 得到 -1; // Chrome 58.0.3029.110 下返回 -1, 其他浏览器正常 // 确认之后是 localeCompare 需要明确指定 locales 参数 '开'.localeCompare('驾', 'zh'); // 得到 1
在 Chrome 下传递 locales 参数才能获得正常预期结果
Edge 浏览器支持 localeCompare
Firefox 浏览器支持 localeCompare
IE 11 浏览器支持 localeCompare
其他浏览器对 localeCompare 支持也很友好,目前也不需要明确传递 locales,浏览器支持参考 developer.mozilla.org
CSS 支持通过 ––variable-name: variable-value 声明变量,并通过 var(––variable-name) 使用变量。
.parent { --color-red: red; } .child { color: var(--color-red); }
Less 和 Sass 已分别使用 $ 和 @ 声明变量,CSS 则采用双划线声明变量。
/* Less 变量示例 */ @blue: #f938ab; .blue { color: @blue; } /* Sass 变量示例 */ $blue: #1875e7; .blue { color: $blue; }
不同于 Less/Sass 在最外层声明, CSS Variable 需要在具体 selector 下声明变量,因此可以在子元素下覆盖父元素定义的变量。
一般将 CSS Variable 定义在伪类 :root 下,让所有子元素都可以继承。
:root { --base-color: #333; --base-size: 14px; } .btn { color: var(--base-color); font-size: var(--base-size); }
div { padding: 2px; } .one { --border: 1px solid red; border: var(--border); } .two { border: var(--border); --border: 2px solid green; } .three { border: var(--border); }
<div class="one"> 1 <div class="two"> 2-1 <div class="three">3</div> </div> <div class="two">2-2</div> </div>
解析到 .two selector 会优先解析 .two 拥有的变量,再应用。因此 .two 应用的 --border
是 2px solid green
。
.three 也会应用 .two 定义的变量。
网页渲染完成之后,再修改变量值,会重新应用 CSS。
因为 val(variable-name) 返回的是 variable-value,所以当 variable-value 不包含单位是,某些样式需要明确指定单位。
:root { --line-height: 20; } p { line-height: var(--line-height)px; }
这点不像 jQuery 自动检测浏览器设置的默认单位。需要显示设置单位。
Proxy 对象可以捕获源对象(对象、数组、函数)的操作,包括增删改查(获取、赋值、枚举、删除)以及函数调用,可以更加全面地控制源对象。
// 源对象 必须是复杂数据类型,如 对象、数组、函数 var target = {age: 27}; var handler = { get: function(target, key) { // 代理 target[key] console.log('get'); if (key in target) { return target[key]; } }, set: function(target, key, value) { // 代理 target[key] = value console.log('set'); target[key] = value; }, deleteProperty: function(target, key) { // 代理 delete target[key] console.log('deleteProperty'); return key in target ? delete target[key] : false; }, enumerate: function(target, key) { console.log('enumerate'); return Object.keys(target); }, ownKeys: function(target, key) { // 代理 Object.getOwnPropertyNames(target) // 代理 Object.getOwnPropertySymbols(target) console.log('ownKeys'); return Object.keys(target); }, has: function(target, key) { // 代理 key in target console.log('has'); return key in target; }, defineProperty: function(target, key, desc) { // 代理 Object.defineProperty(proxy, key, desc) console.log('defineProperty'); Object.defineProperty(target, key, desc); return target; }, getOwnPropertyDescriptor: function(target, key) { console.log('getOwnPropertyDescriptor'); return key in target ? { value: target[key], writable: true, enumerable: false, configurable: true } : undefined; } }; // 新建代理 var proxy = new Proxy(target, handler); proxy.age; proxy.age = 28; Object.defineProperty(proxy, 'name', {value: 'seven'}); proxy.name; target.name;
Object.keys(proxy),会分别调用 handler.ownKeys 和 getOwnPropertyDescriptor,前者用于获取 target 的 key 列表,后者用于依次判断每个 key 是否可以迭代。
若 getOwnPropertyDescriptor 返回值得 enumerate 为 false,则该 key 不会出现在 Object.keys(proxy) 返回结果中。
Object.getOwnPropertyDescriptors(proxy) 和 Object.keys(proxy) 一样,会分别调用 handler.ownKeys 和 getOwnPropertyDescriptor,但不会根据 getOwnPropertyDescriptor 返回结果过滤 key。
var target = [1, 2, 3]; var handler = { set: function(target, key, value) { // 代理 target[key] = value console.log('set'); target[key] = value; // 代理数组 set,需要 返回源对象 // 代理对象不需要 return target; } }; var proxy = new Proxy(target, handler); proxy.push(4); proxy.pop(); proxy.unshift(5); proxy.shift();
代理数组 handler.set 需要返回源对象,其余与代理对象一致。
var target = function (name) { this.name = name; this.sayHello = function() { return 'Hi, I am ' + this.name; }; return 'Hi, I am ' + name; }; var handler = { construct: function(target, args) { console.log('construct'); var inst = Object.create(target.prototype); target.apply(inst, args); return inst; }, apply: function(target, args) { console.log('apply'); return target.apply(target, args); } }; var proxy = new Proxy(target, handler); proxy(); var inst = new proxy('seven'); inst.sayHello();
代理函数可以设置 handler.construct 和 handler.apply,其余与代理对象一致。
var target = {age: 27}; var handler = { get: function(target, key) { // 代理 target[key] console.log('get'); if (key in target) { return target[key]; } } }; var revocable = Proxy.revocable(target, handler); var proxy = revocable.proxy; proxy.age; // 执行撤销 revocable.revoke(); // 撤销之后再执行任何代理操作都会报错 // TypeError proxy.age;
无意间发现站点 ua.zhengxianjun.com 的 Network 中出现两条未知的 js 请求。
这两个域名跟 ua.zhengxianjun.com 没有任何关系,查看源码发现网页被注入了一个 script 标签。
刚开始以为网站被黑了,可是黑我的小网站没意思呀。
在服务器上访问 ua.zhengxianjun.com 发现一切正常,感觉可能是被运营商注入了广告。
在 Network 中看到第二条脚本没有加载成功,可能是这个原因网页没有出现异常。试了多次也没出现广告。
查了一下域名 fcy6.cc,但对方开启了隐私保护。
从另外一个域名 sho9wbox.com 的 whois 信息可以看到,它属于 veryci.com,跟电驴(verycd.com)非常接近。
veryci.com 官网大部分是图片,而且用词不清。最主要是启信宝上的公司地址跟官网的地址不一样。(如下图)
当然这些都不是重点。
重点是:推荐 HTTP 站点启用 CSP,尽量保证即使网页被注入脚本,也不会出现危害。
如何使用 CSP,参考 https://blog.zhengxianjun.com/2015/04/web-security-csp/
为了能够清晰区分 1Il 0Oo、准确识别相似字符,测试了 70+ 种 Windows 系统下使用的字体,得到 3 种能清晰区分 1Il 0Oo 的字体,效果如下:
# | 字体 | 1, i, l, 零和欧, 全角 |
1 | Consolas | 1iIl 0Oo0Oo ø |
2 | Tahoma | 1iIl 0Oo0Oo ø |
3 | Verdana | 1iIl 0Oo0Oo ø |
在这 3 种字体中,Verdana 和 Tahoma 效果最好,Consolas 次之,在 Excel 中也常用到 Tahoma。
最终采取的方案是:
.input { font-family: Verdana, Tahoma, Consolas, monospace; }
全部对比效果如下:
# | 字体 | 1, i, l, 零和欧, 全角 |
1 | Consolas | 1iIl 0Oo0Oo ø |
2 | Tahoma | 1iIl 0Oo0Oo ø |
3 | Verdana | 1iIl 0Oo0Oo ø |
4 | ‘Angsana New’ | 1iIl 0Oo0Oo ø |
5 | Arial | 1iIl 0Oo0Oo ø |
6 | ‘Arial Black’ | 1iIl 0Oo0Oo ø |
7 | Basemic | 1iIl 0Oo0Oo ø |
8 | Batang | 1iIl 0Oo0Oo ø |
9 | ‘Book Antiqua’ | 1iIl 0Oo0Oo ø |
10 | ‘Browallia New’ | 1iIl 0Oo0Oo ø |
11 | Calibri | 1iIl 0Oo0Oo ø |
12 | Cambria | 1iIl 0Oo0Oo ø |
13 | Candara | 1iIl 0Oo0Oo ø |
14 | Century | 1iIl 0Oo0Oo ø |
15 | ‘Comic Sans MS’ | 1iIl 0Oo0Oo ø |
16 | Constantia | 1iIl 0Oo0Oo ø |
17 | Corbel | 1iIl 0Oo0Oo ø |
18 | ‘Cordia New’ | 1iIl 0Oo0Oo ø |
19 | Courier | 1iIl 0Oo0Oo ø |
20 | ‘Courier New’ | 1iIl 0Oo0Oo ø |
21 | DilleniaUPC | 1iIl 0Oo0Oo ø |
22 | Dotum | 1iIl 0Oo0Oo ø |
23 | Garamond | 1iIl 0Oo0Oo ø |
24 | Geneva | 1iIl 0Oo0Oo ø |
25 | Georgia | 1iIl 0Oo0Oo ø |
26 | Gulim | 1iIl 0Oo0Oo ø |
27 | GungSuh | 1iIl 0Oo0Oo ø |
28 | Impact | 1iIl 0Oo0Oo ø |
29 | JasmineUPC | 1iIl 0Oo0Oo ø |
30 | ‘Lucida Console’ | 1iIl 0Oo0Oo ø |
31 | ‘Lucida Sans Unicode’ | 1iIl 0Oo0Oo ø |
32 | ‘Malgun Gothic’ | 1iIl 0Oo0Oo ø |
33 | Mangal | 1iIl 0Oo0Oo ø |
34 | Marlett | 1iIl 0Oo0Oo ø |
35 | Meiryo | 1iIl 0Oo0Oo ø |
36 | ‘Microsoft JhengHei’ | 1iIl 0Oo0Oo ø |
37 | MingLiu | 1iIl 0Oo0Oo ø |
38 | MingLiU_HKSCS | 1iIl 0Oo0Oo ø |
39 | monospace | 1iIl 0Oo0Oo ø |
40 | Modern | 1iIl 0Oo0Oo ø |
41 | ‘MS Gothic’ | 1iIl 0Oo0Oo ø |
42 | ‘MS Mincho’ | 1iIl 0Oo0Oo ø |
43 | ‘MS PGothic’ | 1iIl 0Oo0Oo ø |
44 | ‘MS PMincho’ | 1iIl 0Oo0Oo ø |
45 | ‘MS Sans Serif’ | 1iIl 0Oo0Oo ø |
46 | ‘MS Serif’ | 1iIl 0Oo0Oo ø |
47 | ‘Palatino Linotype’ | 1iIl 0Oo0Oo ø |
48 | PMingliU | 1iIl 0Oo0Oo ø |
49 | PMingLiU-ExtB | 1iIl 0Oo0Oo ø |
50 | serif | 1iIl 0Oo0Oo ø |
51 | Symbol | 1iIl 0Oo0Oo ø |
52 | Times | 1iIl 0Oo0Oo ø |
53 | ‘Times New Roman’ | 1iIl 0Oo0Oo ø |
54 | ‘Trebuchet MS’ | 1iIl 0Oo0Oo ø |
55 | Wingdings | 1iIl 0Oo0Oo ø |
56 | ‘Yu Gothic’ | 1iIl 0Oo0Oo ø |
57 | ‘Yu Mincho’ | 1iIl 0Oo0Oo ø |
58 | 方正舒体 | 1iIl 0Oo0Oo ø |
59 | 方正姚体 | 1iIl 0Oo0Oo ø |
60 | 仿宋 | 1iIl 0Oo0Oo ø |
61 | 黑体 | 1iIl 0Oo0Oo ø |
62 | 华文彩云 | 1iIl 0Oo0Oo ø |
63 | 华文仿宋 | 1iIl 0Oo0Oo ø |
64 | 华文行楷 | 1iIl 0Oo0Oo ø |
65 | 华文琥珀 | 1iIl 0Oo0Oo ø |
66 | 华文楷体 | 1iIl 0Oo0Oo ø |
67 | 华文隶书 | 1iIl 0Oo0Oo ø |
68 | 华文宋体 | 1iIl 0Oo0Oo ø |
69 | 华文细黑 | 1iIl 0Oo0Oo ø |
70 | 华文新魏 | 1iIl 0Oo0Oo ø |
71 | 华文中宋 | 1iIl 0Oo0Oo ø |
72 | 楷体 | 1iIl 0Oo0Oo ø |
73 | 隶书 | 1iIl 0Oo0Oo ø |
74 | 宋体 | 1iIl 0Oo0Oo ø |
75 | 宋体-ExtB | 1iIl 0Oo0Oo ø |
76 | 微软雅黑 | 1iIl 0Oo0Oo ø |
77 | 新宋体 | 1iIl 0Oo0Oo ø |
78 | 幼圆 | 1iIl 0Oo0Oo ø |