public static function output($type=0, $length=4, $sessionName = 'VerificationCode'){ self::$length = $length ? $length : 1; self::$sessionName = $sessionName; self::$type = $type ? $type : mt_rand() % 4 +1; if (self::$type == 4) { self::$fontSize[0] =16; self::$fontSize[1] =18; self::$fontFace = self::$fontChinese[mt_rand() % count(self::$fontChinese)]; } else{ self::$fontSize[0] =20; self::$fontSize[1] =24; self::$fontFace = self::$font[mt_rand() % count(self::$font)]; } self::$width = (int)(24 * self::$length+tan(deg2rad(30))*24); //self::$width = 110; self::$height = 40; if (strlen(self::$code)==0) self::createCode(); //生成码值 self::$image=imagecreatetruecolor(self::$width,self::$height); //两种生成模式的图片 if (self::$type == 4) { self::createImage1(); } else{ if (mt_rand()%2) { self::createImage1(); } else self::createImage2(); } //加入干扰象素; for($i=0; $i<self::$width; $i++){ $randcolor = ImageColorallocate(self::$image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)); imagesetpixel(self::$image, mt_rand()%self::$width , mt_rand()%self::$height , $randcolor); } header("Content-type: image/png"); imagepng(self::$image); imagedestroy(self::$image); /*echo self::$type; echo ' - '; echo self::$code;*/ }
/** * validate code * @author Kydz 2015-06-14 * @param string $code the vode to be alidate * @return bool */ public function authVCode($code) { $now = new DateTime(); $now = $now->format('Y-m-d H:i:s'); $vCode = VerificationCode::where('v_code', '=', $code)->where('verifiable_id', '=', $this->_mobile)->where('verifiable_type', '=', 'Phone')->where('v_reuse', '=', 0)->where('expire_at', '>', $now)->whereNull('verify_at')->first(); if (!isset($vCode->v_id)) { throw new Exception("验证码无效", 1); } else { $vCode->verify_at = $now; $vCode->save(); return true; } }
public function code() { $code = new VerificationCode(); $code->show(); $_SESSION['code'] = $code->code; }
} for ($i = 0; $i < 100; $i++) { $color = imagecolorallocate($this->img, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255)); imagestring($this->img, mt_rand(1, 5), mt_rand(0, $this->width), mt_rand(0, $this->height), '*', $color); } } //输出 private function outPut() { header("Content-Type:image/png"); imagepng($this->img); imagedestroy($this->img); } public function showCode() { $this->createBg(); $this->createCode(); $this->createDisturb(); $this->createFont(); $this->outPut(); } //获取验证码 public function getCode() { return strtolower($this->code); } } session_start(); $code = new VerificationCode(); $code->showCode(); $_SESSION['admincode'] = $code->getCode();
/** * confirm user account via code sent in email * @param int $userId * @param string $code * @return bool */ public function confirm($userId, $code) { $vcode = new VerificationCode($this->db, $this->logger); if (!$vcode->getByUserId($userId)) { return array(false, self::ERROR_VERIFICATION_ERROR); } if ($vcode->code != $code) { return array(false, self::ERROR_VERIFICATION_ERROR); } $user = new User($this->db, $this->logger); if ($user->getById($userId)) { $user->status = User::STATUS_ENABLED; $user->save(); } return array(true, 0); }