设为首页 加入收藏

TOP

身份证校验-----后端
2019-09-03 03:46:16 】 浏览:50
Tags:身份证 校验 ----- 后端

用的是thinkphp5 + layui

代码:

public function add()
{
	if($this->request->isAjax())
	{
		$request = new Request();     
		$params = $this->request->param();   
		$title = $params['title']; 
		$identity = $params['identity'];
		if(empty($identity))
		{
			$this->error('请输入您的身份证号或护照号!');
		} 
		if( strlen($identity ) != 18 ) 
		{
			$this->error('您输入的身份证号码有误!');
		}
		$crad_id_base = substr($identity , 0, 17);
		$crad_id_verify = substr($identity , -1);
		// 加权因子 
		$factor = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3);
		// 校验码对应值 
		$verify_number_list = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
		$checksum = 0;
		for( $i = 0; $i < strlen($crad_id_base); $i++ ) 
		{
			$checksum += substr($crad_id_base, $i, 1) * $factor[$i % 10];
		}
		$mod = $checksum % 11;
		if( $crad_id_verify == $verify_number_list[$mod] ) 
		{
			$result = Db::name('volunteers')->where(['identity'=>$identity])->find();
			if($result)
			{
				$this->error('该志愿者身份证号已存在,请勿重复添加!');
			}else
			{
				$data = [
					'title'          => isset($params['title']) ? $params['title'] : '',
					'volun_no'       => isset($params['volun_no']) ? $params['volun_no'] : '',
					'userid'         => isset($params['userid']) ? $params['userid'] : 0,
					'createtime'     => time(),
					'updatetime'     => time(),
					'identity'       => $params['identity'],
					'identity_type'  => isset($params['identity_type']) ? $params['identity_type'] : 1,
				]; 
				if($result = Db::name('volunteers')->insert($data))
				{
					return $this->success('志愿者添加成功!');
				}else{
					return $this->error('志愿者添加失败!');
				} 
				return;
			} 
			return;	
		}                                                       
	}      
	return $this->fetch();
}

  

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇[PHP] 项目实践中的自动加载实现 下一篇php 连接webservice接口

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目