- class ValidationCode
- {
- //属性
- private $width;
- private $height;
- private $ codeNum;
- 비공개 $image;
- 비공개 $disturbColorNum; //干扰元素数目
- private $checkCode;
- function __construct($width=80,$height=20,$codeNum=4)
- {
- $this->width=$width;
- $this->height=$height;
- $this->codeNum=$codeNum;
- $number=floor($width*$height/15);
- if($number> ;240-$codeNum)
- {
- $this->disturbColorNum=240-$codeNum;
- }else
- {
- $this->disturbColorNum=$number;
- }
- $this->checkCode=$this->createCheckcode();
- }
- function getCheckCode()
- {
- return $this->checkCode;
- }
- 비공개 함수 createImage(){
- $this->image=imagecreatetruecolor($this->width,$this->height);
- $backcolor=imagecolorallocate($this->image ,rand(225,255),rand(225,255),rand(255,255));
- imagefill($this->image,0,0,$backcolor);
- $border=imagecolorallocate($this-> image,0,0,0);
- imageRectangle($this->image,0,0,$this->width-1,$this->height-1,$border);
- }
- 비공개 함수 setDisturbColor(){
- for($i=0;$i<$this->disturbColorNum;$i ){
- $color=imagecolorallocate($this->image,rand (0,255),rand(0,255),rand(0,255));
- imagesetpixel($this->image,rand(1,$this->width-2),rand(1,$this-> height-2),$color);
- }
- for($i=0;$i<10;$i )
- {
- $color=imagecolorallocate($this->image, rand(0,255),rand(0,255),rand(0,255));
- imagearc($this->image,rand(-10,$this->width),rand(-10,$this-> ;height),rand(30,300),rand(20,300),55,44,$color);
- }
- }
- 비공개 함수 outputText($fontFace=""){
- for($ i=0;$i<$this->codeNum;$i )
- {
- $fontcolor=imagecolorallocate($this->image,rand(0,128),rand(0,128),rand(0,128) );
- if($fontFace=="")
- {
- $fontsize=rand(3,5);
- $x=floor($this->width/$this-> ;codeNum)*$i 5;
- $y=rand(0,$this->height-15);
- imagechar($this->image,$fontsize,$x,$y,$ this->checkCode{$i},$fontcolor);
- }
- else
- {
- $fontsize=rand(12,16);
- $x=floor(($this ->width-8)/$this->codeNum)*$i 8;
- $y=rand($fontsize,$this->height-8);
- imagettftext($this-> ;image,$fontsize,rand(-45,45),$x,$y,$fontcolor,$fontFace,$this->checkCode{$i});
- }
- }
- }
-
- 비공개 함수 createCheckCode(){
- $code="23456789abcdefghijkmnpqrstuvwrst";
- $str="";
- for($i=0;$i<$this->codeNum ;$i )
- {
- $char=$code{rand(0,strlen($code)-1)};
- $str.=$char;
- }
- $ 반환 str;
- }
- 비공개 함수 outputImage()
- {
- if(imagetypes()&IMG_GIF)
- {
- header("Content-Type:image/gif");
- imagepng($this->image);
- }else if(imagetypes()&IMG_JPG)
- {
- header("Content-Type:image/jpeg");
- imagepng($this ->image);
- }else if(imagetypes()&IMG_PNG)
- {
- header("Content-Type:image/png");
- imagepng($this->image) ;
- }else if(imagetypes()&IMG_WBMP){
- header("Content-Type:image/vnd.wap.wbmp");
- imagepng($this->image);
- }else
- {
- die("PHP不支持图文验证码");
- }
- }
- //통합过该方法向浏览器输출图이미지
- function showImage($fontFace ="")
- {
- //创建图image背景
- $this->createImage();
- //设置干扰元素
- $this->setDisturbColor();
- //向图 Image中随机画출문본
- $this->outputText($fontFace);
- //输ude图image
- $this->outputImage();
- }
-
- function __destruct()
- {
- imagedestroy($this->image);
- }
- }
- function checklogin(){
- if(empty( $_POST['name']))
- die('사용자 이름은 비워둘 수 없습니다.');
- if(empty($_POST['password']))
- die("비밀번호는 비워둘 수 없습니다." ) ;
- if($_SESSION['code']!=$_POST['vertify'])
- die("인증코드 입력이 잘못되었습니다.".$_SESSION['code']);
-
- $username=$_POST['name'];
- $password=md5($_POST['password']);
- //존재하는지 확인
- conndb($username,$password) ;
- }
- 함수 conndb($name="",$ps=""){
- $conn=mysql_connect('localhost','root','123456');
- if( ! $conn) die("데이터베이스 연결 실패".mysql_error());
- mysql_select_db('5kan',$conn) 또는 die('데이터베이스 선택 실패'.mysql_error());
- mysql_set_charset('utf8 ' ,$conn);
- $sql="사용자 이름='{$name}' 및 비밀번호='{$ps}'"인 k_user에서 ID 선택;
- $result=mysql_query($sql) 또는 사망 ( "SQL 문 오류".mysql_error());
- if(mysql_num_rows($result)>0) die("로그인 성공");
- else die("사용자 이름 또는 비밀번호가 잘못되었습니다.");
- mysql_close($conn);
- }
- session_start();
- if(!isset($_POST['randnum']))
- {
- $code=new ValidationCode(120, 20,4);
- $code->showImage("comicbd.ttf"); //페이지에 표시
- $_SESSION['code']=$code->getCheckCode();//저장 서버에서
- }
- else
- {
- checklogin();
- }
-
- ?>
코드 복사
|