一般在把数据写入数据库之前,先对将要写入的数据进行校验,可以避免出现比较严重的安全问题(例如一般性的SQL注入攻击)。
mayfish 可以灵活的自定义将要执行写入的数据内容的校验规则,以减少开发人员手动对每一个字段的数据进行校验的麻烦。
例子如下:
一、首先定义数据库模块
class MemberModel extends AppModel
{
/** 设置数据库表名称 **/
protected $tableName = "members";
/**
* 数据验证规则
*/
protected $verify = array(
array("NotEmpty", "username", "用户名不能留空"),
array("hasOne", "username", "此用户已经存在,请换另一个用户名称再试一次"),
array("NotEmpty", "password", "密码不能留空"),
array("NotEmpty", "email", "邮箱地址不能留空"),
array("isEmail", "email", "邮箱地址格式不正确"),
array("hasOne", "email", "邮箱地址已经被占用")
);
/**
* 覆盖父类添加数据入库的方法
* 先对用户密码进行md5加密,再调用父类的方法写入数据库中
*/
public function create($data) {
$data = array_map("addslashes", $data); //将数据中的标点符号(单、双引号)进行安全转义
$data["password"] = md5($data["password"]);
return parent::create($data);
}
}
?>
二、执行数据写入操作
//执行写入数据的片段...
//执行数据入库的操作
private function PostData() {
$fields = array("username", "password", "email");
$post = array_map("trims", $_POST); //清除所有数据两边多余的空格
$post = parseHTML($post, $fields); //将指定的字段内容进行清除HTML处理
$data = parseFields($post, $fields); //提取可以写入数据库的字段(防止别人绕过你的页面进行提交一些别有用心的数据)
$DB = & M("member");
//进行数据验证
if (!$DB->verify($data)) {
//验证失败,取出失败的原因,并提交到模板页面中
$this->assign("error", $DB->getVerifyError());
//把提交过来的数据也提交到模板中(用以实现用户好像没有离开过页面的感觉)
$this->assign("default", $post);
//渲染注册页面模板
$this->display("/register.html");
}
else {
//写入数据库
$result = $DB->create($data);
//返回布尔型,说明数据写入失败,渲染注册页面模板
if (is_bool($result)) {
$this->assign("default", $post);
$this->display("/register.html");
}
else {
//注册成功,渲染注册成功页面模板
$this->assign("username", $data["username"]);
$this->display("/reg_success.html");
}
}
}
可执行验证的规则有
NotEmpty 不能为空
Number 只能是整数
isEmail 邮箱地址是否正确
hasOne 是否是唯一(是否重复,是否已经存在)
Regex 自定义正则表达式
验证的格式为
array(验证方法, 进行验证的字段名称, 验证错误的提示信息)
对于正则表达示的验证
array("Regex", "mobile", '/^13\d{9}$/', "用户名不能留空")
MayFish 下载

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Notepad++7.3.1
Easy-to-use and free code editor

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver CS6
Visual web development tools