1).不要相信任何来自不受本身直接控制的数据源中的数据。包括但不限于:php
2).解决办法:过滤输入。删除不安全的字符,在数据到达应用的存储层以前,必须过滤数据。须要过滤的数据包括不限于:HTML、SQL查询和用户资料信息。html
htmlentities($input, ENT_QUOTES, 'UTF-8')
过滤输入。或者使用HTML Purifier。缺点是慢filter_var()
和filter_input()
过滤用户资料信息3).验证数据:也可使用filter_var()
,验证成功返回要验证的值,失败返回false
。可是这个函数没法验证全部数据,因此可使用一些验证功能组件。例如aura/filter
或者symfony/validator
4)转义输出:任然可使用htmlentities
这个函数,一些模板引擎也自带了转义功能。git
1).绝对不能知道用户的密码。
2).绝对不要约束用户的密码,要限制的话只限制最小长度。
3).绝对不能使用电子邮件发送用户的密码。你能够发送一个修改密码的连接,上面带一个token验证是用户本人就好了。
4).使用bcrypt
计算用户密码的哈希值。加密和哈希不是一回事,加密是双向算法,加密的数据能够被解密。可是哈希是单项算法,哈希以后的数据没法被还原,想同的数据哈希以后获得的数据始终是相同的。使用数据库存储经过bcrypt
哈希密码以后的值。
5).使用密码哈希API简化计算密码哈希和验证密码的操做。下面的注册用户的通常操做github
POST /register.php HTTP/1.1
Content-Length: 43
Content-type: application/x-www-form-urlencoded
email=xiao@hello.world&password=nihao复制代码
下面是接受这个请求的PHP文件web
<?php
try {
$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
if (!$email) {
throw new Exception('Invalid email');
}
$password = filter_iput(INPUT_POST, 'password');
if (!$password || mb_strlen($password) < 8) {
throw new Exception('Password must contain 8+ characters');
}
//建立密码的哈希值
$passwordHash = password_hash(
$password,
PASSWORD_DEFAULT,
['cost' => 12]
);
if ($passwordHash === false) {
throw new Exception('Password hash failed');
}
//建立用户帐户,这里是虚构的代码
$user = new User();
$user->email = $email;
$user->password_hash = $passwordHash;
$user->save();
header('HTTP/1.1 302 Redirect');
header('Location: /login.php');
} catch (Exception $e) {
header('HTTP1.1 400 Bad Request');
echo $e->getMessage();
}复制代码
6).根据机器的具体计算能力修改password_hash()的第三个值。计算哈希值通常须要0.1s-0.5s。
7).密码的哈希值存储在varchar(255)类型的数据库列中。
8).登陆用户的通常流程算法
POST /login.php HTTP1.1
Content-length: 43
Content-Type: application/x-www-form-urlencoded
email=xiao@hello.wordl&pasword=nihao复制代码
session_start();
try {
$email = filter_input(INPUT_POST, 'email');
$password = filter_iinput(INPUT_POST, 'password');
$user = User::findByEmail($email);
if (password_verify($password, $user->password_hash) === false) {
throw new Exception(''Invalid password);
}
//若是须要的话,从新计算密码的哈希值
$currentHasAlgorithm = PASSWORD_DEFAULT;
$currentHashOptions = array('cost' => 15);
$passwordNeedsRehash = password_needs_rehash(
$user->password_hash,
$currentHasAlgorithm,
$currentHasOptions
);
if ($passwordNeedsRehash === true) {
$user->password_hash = password_hash(
$password,
$currentHasAlgorithm,
$currentHasOptions
);
$user->save();
}
$_SESSION['user_logged_in'] = 'yes';
$_SESSION['user_email'] = $email;
header('HTTP/1.1 302 Redirect');
header('Location: /user-profile.php');
} catch (Exception) {
header('HTTP/1.1 401 Unauthorized');
echo $e->getMessage();
}复制代码
9).PHP5.5.0版本以前的密码哈希API没法使用,推荐使用ircmaxell/password-compat组件。数据库
PHP专题系列目录地址:github.com/xx19941215/…
PHP专题系列预计写二十篇左右,主要总结咱们平常PHP开发中容易忽略的基础知识和现代PHP开发中关于规范、部署、优化的一些实战性建议,同时还有对Javascript语言特色的深刻研究。安全