Example tutorial of traditional validation in php

零下一度
Release: 2023-03-10 17:12:02
Original
1267 people have browsed it

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>";
      }
}?>
Copy after login

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>";
                      }
                  }
Copy after login

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 =&#39;user&#39;;     protected $_validate=array(                                  //进行静态验证
          //array(验证字段1,验证规则,错误提示,[验证条件,附加规则,验证时间]),array(&#39;username&#39;,&#39;require&#39;,&#39;用户名必填!&#39;),array(&#39;username&#39;,&#39;&#39;,&#39;帐号名称已经存在!&#39;,0,&#39;unique&#39;,1),array(&#39;repassword&#39;,&#39;password&#39;,&#39;两次密码不一致!&#39;,0,&#39;confirm&#39;),array(&#39;qq&#39;,&#39;require&#39;,&#39;qq必填!&#39;),array(&#39;qq&#39;,&#39;&#39;,&#39;帐号名称已经存在!&#39;,0,&#39;unique&#39;,1),array(&#39;class&#39;,&#39;require&#39;,&#39;班级必填!&#39;),array(&#39;j_verify&#39;,&#39;require&#39;,&#39;验证码必须!&#39;),);
         
    }?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!