本文主要给大家介绍了关于PHP实践教程之过滤、验证、转义与密码的相关资料,需要的朋友可以参考借鉴,下面来一起看看吧。
一、过滤、验证和转义
1).不要相信任何来自不受自己直接控制的数据源中的数据。包括但不限于:
$_GET
$_POST
$_REQUEST
$_COOKIE
$argv
php://stdin
php://input
file_get_contents()
远程数据库
远程API
来自客户端的数据
2).解决办法:过滤输入。删除不安全的字符,在数据到达应用的存储层之前,必须过滤数据。需要过滤的数据包括不限于:HTML、SQL查询和用户资料信息。
HTML:使用htmlentities()
函数过滤HTML成对应的实体。这个函数会转义制定字符的HTML字符,以便在存储层安全的渲染。正确的使用方式是使用htmlentities($input, ENT_QUOTES, 'UTF-8')
过滤输入。或者使用HTML Purifier。缺点是慢
SQL查询: 有时必须根据数据构建SQL查询。这时要要使用PDO预处理语句过滤外部数据。
用户资料信息:使用filter_var()
和filter_input()
过滤用户资料信息
3).验证数据:也可以使用filter_var()
,验证成功返回要验证的值,失败返回false。但是这个函数无法验证所有数据,所以可以使用一些验证功能组件。例如aura/filter或者symfony/validator
4)转义输出:任然可以使用htmlentities这个函数,一些模板引擎也自带了转义功能。
密码
1).绝对不能知道用户的密码。
2).绝对不要约束用户的密码,要限制的话只限制最小长度。
3).绝对不能使用电子邮件发送用户的密码。你可以发送一个修改密码的链接,上面带一个token验证是用户本人就行了。
4).使用bcrypt计算用户密码的哈希值。加密和哈希不是一回事,加密是双向算法,加密的数据可以被解密。但是哈希是单项算法,哈希之后的数据无法被还原,想同的数据哈希之后得到的数据始终是相同的。使用数据库存储通过bcrypt哈希密码之后的值。
5).使用密码哈希API简化计算密码哈希和验证密码的操作。下面的注册用户的一般操作
1 2 3 4 5 | POST /register.php HTTP/1.1
Content-Length: 43
Content-type: application/x-www-form-urlencoded
email=xiao@hello.world&password=nihao
|
登录后复制
下面是接受这个请求的PHP文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | <?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).登录用户的一般流程
1 2 3 4 5 | POST /login.php HTTP1.1
Content-length: 43
Content-Type: application/x-www-form-urlencoded
email=xiao@hello.wordl&pasword=nihao
|
登录后复制
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | 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验证码类ValidateCode
php验证码类实例分享
php验证输入的手机号码是否合法
以上是PHP实践教程之过滤、验证、转义与密码的方法的详细内容。更多信息请关注PHP中文网其他相关文章!