php安全 过滤、验证、转义
不要相信外部源
- $_GET
- $_POST
- $_REQUEST
- $_COOKIE
- $argv
- php://stdin
- php://input
- file_get_contents()
- 远程数据库
- 远程api
- 来自客户端的数据
1 <?php
2 $input = '<p><script>alert("You won the Nigerian lottery!");</script></p>';
3 echo htmlentities($input, ENT_QUOTES, 'UTF-8').PHP_EOL;
4 // <p><script>alert("You won the Nigerian lottery!");</script></p>
5
6 $email = 'john介样子@example.com';
7 $emailSafe = filter_var($email, FILTER_SANITIZE_EMAIL);
8 echo $emailSafe.PHP_EOL;
9 // john@example.com
10
11 $string = "\ni18n说的话\t";
12 $safeString = filter_var(
13 $string,
14 FILTER_SANITIZE_STRING,
15 FILTER_FLAG_STRIP_LOW | FILTER_FLAG_ENCODE_HIGH
16 );
17 echo $safeString.PHP_EOL;
18 // i18n说çè¯
19
20 // 转义输出
21 $output = '<p><script>alert("NSA backdoor installed")</script></p>';
22 echo htmlentities($output, ENT_QUOTES, 'UTF-8').PHP_EOL;
23 // <p><script>alert("NSA backdoor installed")</script></p>
模板引擎
一些加密函数
md5, sha1, bcrypt, scrypt
* 注册用户
POST /register.php HTTP/1.1
Content-Length: 43
Content-Type: application/x-www-form-urlencoded
email=john@example.com&password=sekritshhh!
<?php
/**
* Created by PhpStorm.
* User: Mch
* Date: 7/17/18
* Time: 22:47
*/
try {
// 验证电子邮箱地址
$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
if (!$email) {
throw new Exception('Invalid email');
}
// 验证密码
$password = filter_input(INPUT_POST, 'password');
if (!$password || mb_strlen($password) < 8) {
throw new Exception('Password must contain 8+ characters');
}
// 创建密码的hash
$passwordHash = password_hash($password, PASSWORD_DEFAULT, ['cost'=>12]);
if ($passwordHash === false) {
throw new Exception('Password hash failed');
}
// 创建用户账户(pseudo code)
$user = new User();
$user->email = $email;
$user->pasword_hash = $passwordHash;
$user->save(); // 重定向到登录页面
header('HTTP/1.1 302 Redirect');
header('Location: /login.php'); } catch (Exception $e) {
// 报告错误
header('HTTP/1.1 400 Bad request');
echo $e->getMessage();
}
register.php
1 <?php
2 /**
3 * POST /login.php HTTP/1.1
4 * Content-Length: 43
5 * Content-Type: application/x-www-form-urlencoded
6 *
7 * email=john@example.com&password=sekritshhh!
8 */
9 session_start();
10
11 try {
12 $email = filter_input(INPUT_POST, 'email');
13 $password = filter_input(INPUT_POST, 'password');
14
15 // (pseudo code)
16 $user = User::findByEmail($email);
17 // 如果需要, 重新计算密码的hash值
18 if (password_verify($password, $user->password_hash)===false) {
19 throw new Exception('Invalid password');
20 }
21
22 // 如果需要, 重新计算密码的hash值
23 $currentHashAlgorithm = PASSWORD_DEFAULT;
24 $currentHashOptions = ['cost' => 15];
25 $passwordNeedsRehash = password_needs_rehash(
26 $user->password_hash,
27 $currentHashAlgorithm,
28 $currentHashOptions
29 );
30 if ($passwordNeedsRehash === true) {
31 // 保存新计算得到的密码hash值 (pseudo code)
32 $user->password_hash = password_hash(
33 $password,
34 $currentHashAlgorithm,
35 $currentHashOptions
36 );
37 $user->save();
38 }
39 $_SESSION['user_logged_in'] = 'yes';
40 $_SESSION['user_email'] = $email;
41
42 // redirect
43 header('HTTP/1.1 302 Redirect');
44 header('Location: /user-profile.php');
45
46 } catch (Exception $e) {
47 header('HTTP/1.1 401 Unauthorized');
48 echo $e->getMessage();
49 }
login.php
php安全 过滤、验证、转义的更多相关文章
- ueditor的过滤、转义、格式丢失问题
1. 过滤 http://www.cnblogs.com/Olive116/p/3464495.html 2. 转义 http://segmentfault.com/q/101000000048928 ...
- php 自带过滤和转义函数
函数名 释义 介绍 htmlspecialchars 将与.单双引号.大于和小于号化成HTML格式 &转成&"转成"' 转成'<转成<>转成> ...
- 过滤器(Filter)对登陆页面进行过滤验证
import javax.servlet.*;import javax.servlet.annotation.WebFilter;import javax.servlet.http.HttpServl ...
- mysql in 过滤 解决转义问题
IF(headUser!='',instr(concat(',',headUser,','),concat(',',cr.headUser,',')),TRUE);
- 【快学SpringBoot】过滤XSS脚本攻击(包括json格式)
若图片查看异常,请前往掘金查看:https://juejin.im/post/5d079e555188251ad81a28d9 XSS攻击是什么 XSS攻击全称跨站脚本攻击,是为不和层叠样式表(Cas ...
- Yii CModel中rules验证规则[转]
array( array(‘username’, ‘required’), array(‘username’, ‘length’, ‘min’=>3, ‘max’=>12), array( ...
- Yii CModel中rules验证规则
array( array(‘username’, ‘required’), array(‘username’, ‘length’, ‘min’=>3, ‘max’=>12), array( ...
- YII中表单验证
关于表单的验证有三种: 1.yii的客户端验证 2.yii的服务器端验证 3.yii的ajax验证 例如: 1.在表单对应的模型中定义一个rules方法(该方添加后,在表单提交时,将自动被调用) pu ...
- ASP.NET Web API编程——模型验证与绑定
1.模型验证 使用特性约束模型属性 可以使用System.ComponentModel.DataAnnotations提供的特性来限制模型. 例如,Required特性表示字段值不能为空,Range特 ...
- Yii正则验证
required : 必须值验证属性 [['字段名'],required,'requiredValue'=>'必填值','message'=>'提示信息']; #说明:CRequiredV ...
随机推荐
- Linux部署PgSQL数据库
1.安装: yum install postgresql-server -y postgresql-setup initdb cd /var/lib/pgsql/data 2.进入 /var/lib/ ...
- Java社区——个人项目开发笔记(二)
1.B\S架构通信原理 浏览器,服务器之间产生通信,浏览器访问服务器,服务器返回一个HTML,浏览器会对HTML进行解析,并渲染相关的内容. 在解析过程中,会发现HTML里引用了css文件,js文件, ...
- pip的问题 Can't connect to HTTPS URL because the SSL module is not available
pip is configured with locations that require TLS/SSL, however the ssl module in Python is not avail ...
- mysqldump备份总结
常用的备份参数 -A 备份全库 -B 备某一个数据库下的所有表 -R, --routines 备份存储过程和函数数据 --triggers 备份触发器数据 --master-data={1|2} 告诉 ...
- TaskAwaiter<TResult> 结构
参考网址:https://docs.microsoft.com/zh-cn/dotnet/api/system.runtime.compilerservices.taskawaiter-1?view= ...
- C语言 Ubuntu系统 UTF-8 文字处理
关于UTF-8的规则:https://baike.baidu.com/item/UTF-8/481798?fr=aladdin 使用windows系统下的Ubuntu子系统,实现C语言对UTF-8编码 ...
- C#基础知识---匿名方法使用
一.匿名方法使用 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Tex ...
- reids rdb与aof
rdb:时合高并发场景,容易备份恢复,会丢失部分数据 1.默认开启的方式,可以进过压缩,可以根据时间点生成快照 2.数据量大的情况下恢复快 3.bgsave一边开启fork保存文件,一边继续响应客户端 ...
- 整型:int
整型:int 整型变量的定义和输出 注意://short<=int<=long<=longlong 代码示例一: #include<stdio.h> int ma ...
- GUI容器之布局管理器
布局管理器 布局管理器:frame.setLayout(); 默认值为new flowLayout() 流式布局 frame.setLayout(new FlowLayout(FlowLayout.R ...