如何从$ _GET/$ _ POST获取信息并将其保存到数据库? [英] Practices for getting information from $_GET/$_POST and saving it to a database?

查看:75
本文介绍了如何从$ _GET/$ _ POST获取信息并将其保存到数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从get/post获取信息并将信息保存到数据库时,今天的最佳实践是什么?数据仍然像过去那样逃脱了吗?还是有其他实践?另外,HTMLPurifier可以放在哪里呢?我目前正在使用它来过滤富文本.

What are today's best practises when it comes to getting information from a get/post and saving information to a database? Is data still escaped like it used to or are there additional practises? Also, where can HTMLPurifier fit in this? I'm currently using it to filter rich text.

推荐答案

从不将数据从GET保存到数据库.

即使您进行了足够的验证和转义,也永远不会从GET保存数据. GET不应更改服务器上的信息.

Never ever save data from GET, even if you are doing sufficient validation and escaping. GET is not supposed to change information on server.

在更改服务器(数据库或服务器文件)上的任何内容之前,请先检查请求是否为POST或PUT或DELETE(如果适用)

POST应该更改服务器的状态.因此,在更新表或更改服务器上的任何文件之前,请检查请求方法是否已发布.

POST is supposed to change state of the server. Hence before updating your tables or changing any file on server check if request method is post.

在处理之前验证输入

如果您期望整数,请验证输入确实是整数.

If you are expecting an integer validate that input is indeed an integer.

在数据库查询中使用输入或将其添加到输出之前先转义输入

出于查询目的,请转义输入内容,以防万一您将输入内容直接打印到输出中,然后去除斜杠并对其进行清理.

For query purposes escape the inputs and in case you are using input to be directly printed to the output then strip the slashes and sanitize it.

在拥有用户会话权限的情况下,将易腐令牌用于POST

使用访问令牌,以防用户登录,并且每次访问或大约5分钟左右更新一次令牌.

Use access tokens in case you have user logged in and update the token every access or 5mins or so.

没有用户会话时使用访问令牌

如Ankur建议的那样,当您没有登录会话时,请使用访问令牌.但这不可靠.

As Ankur suggested use access tokens when you don't have login session. But this is not reliable.

这篇关于如何从$ _GET/$ _ POST获取信息并将其保存到数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆