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 ...
随机推荐
- DVWA靶场之SQL Injection通关
SQL注入,一个大概的手工流程: 判断是否有注入,什么类型 破解SQL语句中查询的字段数是多少 确定回显位置 破库 破表 破字段 获得内容 Low: <?php if( isset( $_REQ ...
- 微信小程序开发(二)——使用WeUI组件库
一.前言 因为小程序的api描述都比较简单,并没有wxml及wxss的描述,一定会想小程序有没有一个UI库,类似于前端中的Bootstrap,MD,Semantic UI这样的框架UI库.有的,它就是 ...
- CTF-favorite_number
想到自己越来越菜就越发兴奋 这是一道CTF,PHP审计的一道: 其实我也知道,兄弟们看见也都头皮有点凉,我就直接看wp了. 也不是没提示,php5.5.9,和全等数组. PHP数组的key溢出问题 其 ...
- php Abstract 抽象类 与 Interface的
一.Abstract Class 与 Interface 的构造 抽象类 Abstract Class <?php abstract class A { abstract public func ...
- 你知道 JavaScript 中的 Arguments 对象都有哪些用途吗?
JavaScript 中 Arguments 对象的用途总结. 前言 相信我们很多人在代码开发的过程中都使用到过一个特殊的对象 -- Arguments 对象. 在实际开发中,Arguments 对象 ...
- mybatis gengeator一键生成
- windows 10 + tensorflow-gpu 环境搭建
安装过程可基本按照ubuntu装法,参考https://www.cnblogs.com/xbit/p/9768238.html 其中: conda配置文件:C:\Users\Administrator ...
- mybatis第一个程序随笔
今天继续学习了解如何写一个mybatis程序 创建了Dao层 1.1 创建一个UserDao接口 1.2 创建UserMapper.xml文件 在mybaits中文手册查找配置信息 <?xml ...
- Windows内核基础知识-8-监听进程、线程和模块
Windows内核基础知识-8-监听进程.线程和模块 Windows内核有一种强大的机制,可以在重大事件发送时得到通知,比如这里的进程.线程和模块加载通知. 本次采用链表+自动快速互斥体来实现内核的主 ...
- zigzag走线原理及应用
电路板上弯弯扭扭的走线有什么用 往期文章: 一文读懂高速互联的阻抗及反射(上) 一文读懂高速互联的阻抗及反射(中) 前面几篇文章有部分读者反馈太深奥,不好懂,要求来一点轻松易懂的.这不,它来了!本期文 ...