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 ...
随机推荐
- Windows内核-7-(IRP)I/O请求包
Windows内核-7-(IRP)I/O请求包 IRP(I/O Request Packet)就是一个进行I/O操作的请求包. IRP是一个结构体,谁分配谁就得释放,通常由执行体里的管理器,获取内核驱 ...
- 快速解决flutter中package包版本冲突问题
当你的项目需要安装的依赖包越多,遇到包冲突可能性就越大,尤其是当依赖的包有重大更新时.比如下图: 上面可以看到是xml跟intl_translation两个包有冲突,因为他们依赖两个不同的petitp ...
- springboot与通用mapper的整合
找到springboot工程下的pom.xml文件,导入如下的依赖jar包 <!--配置通用Mapper start--> <dependency> <groupId&g ...
- 关于Ubuntu18.04 linux系统下使用Tim QQ 微信
先配上张图 步骤: 1.1 :需要安装环境deepin-wine 1.1:(你把他理解为jdk就好,没有jdk无法运行java程序,同理没有deepin-wine环境无法运行腾讯产品) 1.2 :去哪 ...
- python turtle的使用
turtle.pendown() # 放下画笔 turtle.penup() # 抬起画笔 turtle.pensize(int) # 设置画笔宽度,值为整数型 turtle.forward(f ...
- ElasticSearch之配置文件详解
################################### Cluster ################################### #定义集群名称,默认是elasticse ...
- Go初始化二维数组
初始化二维数组 var a = make([][]int, n) for i := 0; i < n; i++ { a[i] = make([]int, n) }
- 日常shell练习
2021-07-19 1.echo的使用 1.1 echo -n 表示不换行输出 # echo输出会自动换行,换行输出两个1 echo 1 echo 1 # 不换行输出,不换行输出两个1 echo - ...
- Lambda@edge 实现负载均衡器功能
一般的业务实现流程为CDN->ELB->EC2,但OTT业务往往会产生很高的流量费用,如果使用常规的架构,流量费用会成倍增加,为了降低费用,我们对架构做了一些优化. AWS Cloudfr ...
- 揭秘盒马鲜生 Android 短视频秒播优化方案
短视频作为内容重要的承载方式,是吸引用户的重点,短视频的内容与体验直接关系到用户是否愿意长时停留.因此,体验的优化就显得尤为重要.上一篇我们分享了 iOS 短视频秒播优化,这篇我们来聊聊 Androi ...