PHP Write login Verification code feature with call method _php tips

Source: Internet
Author: User
Tags imagejpeg learn php learn php programming php programming php write rand

This example for you to share a PHP write login verification code function for your reference, the specific content as follows

showkey.php

<?php session_start (); Set the cookie or Session function Esetcookie ($name, $str, $life =0) {//This function will string str
All lowercase strings make the validation code input case-insensitive----in submitting a form for session comparison also requires $_session[$name]=strtolower ($STR); //Get random characters This function distinguishes character case if case-insensitive can be added Strtolower function Domake_password ($len) {$chars = array (/* "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "s", "T", "U", "V", "w", "X", "Y", "Z"  "," A "," B "," C "," D "," E "," F "," G "," H "," I "," J "," K "," L "," M "," N "," O "," P "," Q "," R "," S "," T "," U "," V ", 
  "W", "X", "Y", "Z", */"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"); 
  $charsLen = count ($chars)-1; 
  Shuffle ($chars);//Disturb the array $output = "";
For ($i =0 $i < $len; $i + +) {$output. = $chars [Mt_rand (0, $charsLen)];//Get an array element} return $output; //Show Verification code function Showkey () {$key =domake_password (4);//Get random value $set =esetcookie ("Checkkey", $key); 
Whether the random value is written to a cookie or session/is supported by the GD library if (function_exists ("imagejpeg")) {Header ("content-type:image/jpeg");
  $img =imagecreate (47,20);
  $blue =imagecolorallocate ($img, 102,102,102);
  $white =imagecolorallocate ($img, 255,255,255);
  $black =imagecolorallocate ($img, 71,71,71);
  Imagefill ($img, 0,0, $blue);
  Imagestring ($img, 5,6,3, $key, $white);
  For ($i =0 $i <90; $i + +)//Add interference pixel {Imagesetpixel ($img, Rand ()%70,rand ()%30, $black);
  } imagejpeg ($IMG);
 Imagedestroy ($IMG);
  } elseif (Function_exists ("Imagepng")) {header ("content-type:image/png");
  $img =imagecreate (47,20);
  $blue =imagecolorallocate ($img, 102,102,102);
  $white =imagecolorallocate ($img, 255,255,255);
  $black =imagecolorallocate ($img, 71,71,71);
  Imagefill ($img, 0,0, $blue);
  Imagestring ($img, 5,6,3, $key, $white);
  For ($i =0 $i <90; $i + +)//Add interference pixel {Imagesetpixel ($img, Rand ()%70,rand ()%30, $black);
  } imagepng ($IMG);
 Imagedestroy ($IMG);
  } elseif (Function_exists ("Imagegif")) {header ("content-type:image/gif");
  $img =imagecreate (47,20); $blue =imagecolorallocate ($img, 102,102,102);
  $white =imagecolorallocate ($img, 255,255,255);
  $black =imagecolorallocate ($img, 71,71,71);
  Imagefill ($img, 0,0, $blue);
  Imagestring ($img, 5,6,3, $key, $white);
  For ($i =0 $i <90; $i + +)//Add interference pixel {Imagesetpixel ($img, Rand ()%70,rand ()%30, $black);
  } imagegif ($IMG);
 Imagedestroy ($IMG);
  } elseif (Function_exists ("Imagewbmp")) {header ("content-type:image/vnd.wap.wbmp");
  $img =imagecreate (47,20);
  $blue =imagecolorallocate ($img, 102,102,102);
  $white =imagecolorallocate ($img, 255,255,255);
  $black =imagecolorallocate ($img, 71,71,71);
  Imagefill ($img, 0,0, $blue);
  Imagestring ($img, 5,6,3, $key, $white);
  For ($i =0 $i <90; $i + +)//Add interference pixel {Imagesetpixel ($img, Rand ()%70,rand ()%30, $black);
  } imagewbmp ($IMG);
 Imagedestroy ($IMG);
  else {//does not support Authenticode header ("content-type:image/jpeg\r\n");
  Header ("pragma:no-cache\r\n");
  Header ("cache-control:no-cache\r\n");
  Header ("expires:0\r\n"); 
 $fp = fopen ("Data/vdcode.jpg", "R");
 } showkey ();?>

Call Method:

Copy Code code as follows:

The above is the entire content of this article, I hope that you learn PHP programming help.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.