tp5生成6位不重复验证码
/** * 获取激活码 */ public function getnum() { $time = Db::name('fangchan_jihuoma')->group('id desc')->limit(1)->find();//从上一次生成的时间开始计算 $num=''; if(empty($time)){ $num = microtime(true); }else{ $num = $time['info_time']; } for ($i = $num; $i < ($num + 5); $i++) { // dump(substr(md5($i), 0, 6));exit; Db::name("fangchan_jihuoma")->insert([ 'num' => substr(md5($i), 0, 6), 'status' => '', 'created_time' => time(), 'info_time' => $i ]); } $sql = "select count(*) as sum,num from osc_fangchan_jihuoma group by num HAVING sum>1";//查出有多少个重复的验证码和重复验证码内容 $data = Db::query($sql); if(!empty($data)) { foreach($data as $k=> $v) { for($j=0;$j<$v['sum'];$j++){ Db::name("fangchan_jihuoma")->where('num',1)->where('status',0)->delete();删除重复的验证码 } } } $this->success('激活码已生成!'); }
CREATE TABLE `osc_fangchan_jihuoma` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增长id', `num` varchar(8) NOT NULL COMMENT '激活码', `uid` int(10) unsigned zerofill DEFAULT NULL COMMENT 'member表id', `status` tinyint(1) unsigned zerofill NOT NULL DEFAULT '0' COMMENT '是否核销 0:未核销,1:已核销', `created_time` int(11) NOT NULL DEFAULT '0' COMMENT '激活码创建时间', `info_time` int(11) NOT NULL DEFAULT '0' COMMENT '标记用时间', PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=134 DEFAULT CHARSET=utf8mb4;