- Class Validationcode
- {
- Property
- Private $width;
- Private $height;
- Private $codeNum;
- Private $image;
- Private $disturbColorNum; Number of interfering elements
- 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;
- }
- Private Function 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);
- }
- Private Function 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);
- }
- }
- Private Function 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});
- }
- }
- }
- Private Function Createcheckcode () {
- $code = "23456789abcdefghijkmnpqrstuvwrst";
- $str = "";
- for ($i =0; $i < $this->codenum; $i + +)
- {
- $char = $code {rand (0,strlen ($code)-1)};
- $str. = $char;
- }
- return $str;
- }
- Private Function 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 does not support image verification code");
- }
- }
- Use this method to output an image to the browser
- function ShowImage ($fontFace = "")
- {
- Create an image background
- $this->createimage ();
- Setting interference elements
- $this->setdisturbcolor ();
- Randomly draw text into an image
- $this->outputtext ($fontFace);
- Output image
- $this->outputimage ();
- }
- function __destruct ()
- {
- Imagedestroy ($this->image);
- }
- }
- function Checklogin () {
- if (Empty ($_post[' name '))
- Die (' User name cannot be empty ');
- if (Empty ($_post[' password '))
- Die ("Password cannot be empty");
- if ($_session[' code ']!=$_post[' vertify ')
- Die ("The captcha is not entered correctly". $_session[' code ');
- $username =$_post[' name '];
- $password =md5 ($_post[' password ');
- Check for Presence
- Conndb ($username, $password);
- }
- function conndb ($name = "", $ps = "") {
- $conn =mysql_connect (' localhost ', ' root ', ' 123456 ');
- if (! $conn) Die ("Database connection Failed". Mysql_error ());
- mysql_select_db (' 5kan ', $conn) or Die (' Select Database Failed '. Mysql_error ());
- Mysql_set_charset (' UTF8 ', $conn);
- $sql = "SELECT id from K_user where username= ' {$name} ' and password= ' {$ps} '";
- $result =mysql_query ($sql) or Die ("SQL statement error". Mysql_error ());
- if (mysql_num_rows ($result) >0) die ("Login Successful");
- Else die ("User name or password error");
- Mysql_close ($conn);
- }
- Session_Start ();
- if (!isset ($_post[' randnum '))
- {
- $code =new Validationcode (120,20,4);
- $code->showimage ("Comicbd.ttf"); Show on page
- $_session[' code ']= $code->getcheckcode ();//Save in Server
- }
- Else
- {
- Checklogin ();
- }
- ?>
Copy Code
|