Php verification code session

Source: Internet
Author: User
Php verification code session
 Widht = $ widht; $ this-> height = $ height; $ this-> codenum = $ codenum;} function showcode () {$ this-> createcode (); // Create a canvas $ this-> createstring (); // create a string $ this-> createimage (); // generate an image} private function createcode () // Create a canvas {$ this-> image = imagecreate ($ this-> widht, $ this-> height); $ backcolor = imagecolorallocate ($ this-> image, 255,255,255 ); // to change the background color, set RGBimagefill ($ this-> image, $ backcolor) here; if ($ this-> bg = true) {$ bg = Imagecolorallocate ($ this-> image, 221,221,221); for ($ I = 0; $ I <$ this-> widht/2; $ I ++) // draw a vertical line at the bottom of {imageline ($ this-> image, $ I * 2, 0, $ I * 2, $ this-> height, $ bg );} for ($ I = 0; $ I <$ this-> height/3; $ I ++) // draw horizontal shading {imageline ($ this-> image, 0, $ I * 3, $ this-> widht, $ I * 3, $ bg) ;}} private function createstring () // write the string {$ string = $ this-> codestring (); session_start (); $ _ SESSION ["your code"] = $ string; for ($ I = 0; $ I <$ this-> coden Um; $ I ++) {if ($ this-> randttf = true) {$ ttf = 'ttfs/t '. rand (1, 9 ). '. ttf'; // ensure that the ttfs folder is in the same directory as the random font. if you change the path, change the path} else {$ ttf = 'ttfs/t1.ttf '; // for fonts in 9, please use your common sense. you only need to modify the numbers in t4 from 1 to 9 to see the effect of each font. select your favorite note: please turn off the random font test} $ fontsize = rand (); // Generate the random font size $ fontangle = rand ); // the character skew angle $ x = $ I * 12 + 4; $ y = rand ($ fontsize, $ this-> height-8 ); if ($ this-> randcolor = true) {$ textcolor = imagecolorallocate ($ this-> image, rand (0,180 ), Rand (0,180), rand (0,180);} else {$ textcolor = imagecolorallocate ($ this-> image, 0 );} imagettftext ($ this-> image, $ fontsize, $ fontangle, $ x, $ y, $ textcolor, $ ttf, $ string [$ I]);} private function codestring () // Generate a random string {$ string = ''; for ($ I = 0; $ I <$ this-> codenum; $ I ++) {$ num = rand ();/** For a single format, refer to ** as long as a number is required ** to set $ num = rand) change to $ num = rand () ** only lower case letters ** and change $ num = rand () above to $ num = rand (3), 3) ** only use uppercase letters **. Change $ num = rand () to $ num = rand) ** only lower-case letters and numbers are required ** Change $ num = rand () above to $ num = rand () **/switch ($ num) {case 1: $ num2 = rand (); // generates the ASCII value break for lowercase letters a-z at random; case 2: $ num2 = rand ); // Generate the ASCII code value corresponding to number 2 9 at random. if it is rand (48, 57): 0-9, there will be 0, which is not conducive to the user's determination of break; case 3: $ num2 = rand (97,122); // randomly generate break for the ASCII code corresponding to uppercase letters A-Z ;} /** remove 0 o I 1 z Z 2 **/if ($ num2 = 111 | $ num = 105 | $ num = 122 | $ num2 = 79 | $ num2 = 73 | $ num = 90) // if it is an O in an uppercase letter, I will replace {$ num2 = 112;} $ tmp = sprintf ("% c", $ num2) with P ); // use the sprintf function to obtain the character $ string corresponding to the generated ASCII code. = $ tmp;} return $ string;} private function createimage () // generate an image {if (function_exists ("imagegif") {header ("Content-type: image/gif "); imagegif ($ this-> image);} elseif (function_exists (" imagejpeg ") {header (" Content-type: image/jpeg "); imag Ejpeg ($ this-> image, "", 50); // 50 indicates the image quality. 0-100 indicates the worst quality. the smaller the image file, the better the quality, larger image file} elseif (function_exists ("imagepng") {header ("Content-type: image/png"); imagepng ($ this-> image );} elseif (function_exists ("imagewbmp") {header ("Content-type: image/vnd. wap. wbmp "); imagewbmp ($ this-> image);} else {die ('Servers do not support images; check the GD database');} function _ destruct () {imagedestroy ($ this-> image) ;}}?>

The above is the verification code class

Below is the test page

 Showcode (); if (strtoupper ($ _ GET ["code"])! = $ _ SESSION ["verification code"]) {echo 'verification code input error';} else {echo 'verification code right';}?>

I have already mentioned in the class that the verification code string is assigned to $ _ SESSION ['verification Code'], but when I test the page, I cannot obtain the printed session array and no result is returned.


Reply to discussion (solution)

It's too long. I didn't see it.

Is the form page and verification code page under different second-level domain names?

The logic of the test code below is incorrect.

PHP code


/*
* Class name: verification code class
* Function: you can use this type of object to obtain the verification code image and the verification code character set ($ _ SESSION ['verification Code']).
* Author: xiao,
* Time: 2012-7-3
*/
Final class Encoding code
{
Private $ width ;......

There is no second-level domain name. I will probably assign a value to $ _ SESSION directly in the verification code class, but I cannot get the SESSION that I assign a value to in the class on other pages.

It's too long. I didn't see it.

Is the form page and verification code page under different second-level domain names?
There is no second-level domain name. I will probably assign a value to $ _ SESSION directly in the verification code class, but I cannot get the SESSION that I assign a value to in the class on other pages.

Other pages session_start?

It's not because of initialization class.
Function _ construct ($ widht = 60, $ height = 30, $ codenum = 4)

Session_start ();

Php simple verification code (letters + numbers)
Http://3aj.cn/php/27.html

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.