Home > Article > Backend Development > Example tutorial of traditional validation in php
PHP (hypertext preprocessor) can be used to build small websites. When users need to register and log in, they need to match the background database to register and log in. The traditional method has many steps and needs to connect to the database first and then use sql. statement to insert.
<?phpheader("Content-type: text/html; charset=utf-8");$conn =mysqli_connect("localhost","root","");if (!$conn){ echo "<script>alert('连接失败!');history.go(-1);</script>"; } mysqli_select_db($conn,"liuyanban");mysqli_query($conn,'SET NAMES utf8');$password=$_POST['password'];$username=$_POST['username'];$face="yellow.png";$result=mysqli_query($conn,"SELECT username from user1 where username = '$username'"); $a=mysqli_num_rows($result);if($a) { echo "<script language=javascript>alert('用户名已存在!');location.href='reg.html'</script>"; }else{ $sql = mysqli_query($conn,"INSERT INTO user1(username,password,face)VALUES('1' ,'2','yellow.png')"); if($sql) { echo "<script language=javascript>alert('注册成功!');location.href='login.html'</script>"; } else {echo "<script>alert('注册失败!');location.href='reg.html'</script>"; } }?>
The above is a native PHP registration example. You need to use mysqli_select_db(), mysqli_query() and other functions to connect to the database first. At the same time, the sql statement can only be executed through the mysqli_query() function. Finally Category judgment and a series of other restriction operations are performed through if statements. In the native PHP stage, it is more practical, easy to understand, and the process is very clear. However, writing code with such statements in a project is inconvenient for mutual communication and is very cumbersome and complicated. Therefore, it is necessary to use the thinkphp framework to build projects so that coders can communicate with each other. Docking also facilitates later code modifications and function additions. So I won’t go into details about the framework here, so use the controller (C) and model (M) in the mvc mode under the thinkphp framework for automatic form verification:
Use form static verification in the controller:
public function doreg(){ $data=D('user'); $d=array(); $d['username']=$_POST['username']; $d['password']=$_POST['password']; $d['time']=date("Y-m-d H:i:s",time()); $d['qq']=$_POST['qq']; $d['class']=$_POST['class']; $mess=$data->create(); if (!$mess){ //表单自动验证$this->error($data->getError(),'Member/member',3); }else{$data->add();echo "<script language=javascript>alert('注册成功!');location.href='member.html'</script>"; } }
The fields that need to be verified are listed in the template:
<?php namespace Home\Model;use Think\Model;class UserModel extends Model{ protected $tableName ='user'; protected $_validate=array( //进行静态验证 //array(验证字段1,验证规则,错误提示,[验证条件,附加规则,验证时间]),array('username','require','用户名必填!'),array('username','','帐号名称已经存在!',0,'unique',1),array('repassword','password','两次密码不一致!',0,'confirm'),array('qq','require','qq必填!'),array('qq','','帐号名称已经存在!',0,'unique',1),array('class','require','班级必填!'),array('j_verify','require','验证码必须!'),); }?>
Here is registration as an example, login is similar, if there is an error in verification , then use $this->error($data->getError(),'Member/member',3); form static verification is very convenient to use.
The above is the detailed content of Example tutorial of traditional validation in php. For more information, please follow other related articles on the PHP Chinese website!