Codeigniter entry (2) verification code

Source: Internet
Author: User
A simple verification code is basically used during logon. First, use the captcha method provided by CI, which is located in the system/helpers/captcha_helper.php controller.

A simple verification code is basically used during logon. use the captcha method provided by CI, which is located in system/helpers/captcha_helper.php.

Controller

First, load the captcha_helper.php auxiliary method in the controller. the code is as follows:

123456789101112131415161718192021222324252627 _ Captcha (); $ this-> load-> view ('Welcome _ message', $ out_datas);} privatefunction_captcha () {$ this-> load-> helper ("my_captcha"); // load the verification code $ vals = array ('IMG _ path' => '. /captcha/', // address for storing the verification code Image 'IMG _ url' => base_url (). "/captcha/", // path for image access 'IMG _ width' => '60', // The Image width is 'IMG _ height' => 20, // height 'expiration' => 1, // storage time, 1 minute 'word _ length' => 4 // display several verification numbers ); $ cap = create_captcha ($ vals); $ out_datas ["v_img"] = $ cap ["image"]; // generated image file $ out_datas ["v_word"] = $ cap ["word"]; // The generated verification code can also be stored in the session to manage return $ out_datas ;}} # In 'IMG _ path' => '. /captcha/'. we store the generated images in the project/captcha Directory. Therefore, we need to create the captcha directory in the same-level Directory of the application, used to store the verification code Image applicationcaptcha -- create a file system in the same directory of the application

View

Then display the image in application/views/welcome_message.php

123456789101112131415 Welcome to CodeIgniter!

Verification code:

Page renderedin 0.4593Seconds. '. CI_VERSION .'':'?>


The form_open method is used here, so you need to load the form_helper.php file. However, this method is often used later, so it is added to application/config/autoload. load the php file, so that CI will automatically help us load.


1 $ Autoload ['helper '] = array ('URL', 'form ');

Shown below



# Since the image width we set is 60 PX and only four characters are displayed at the same time, and the default value in CI is PX and the length is 8, the verification code will jump out of the image during refresh, the modification method is as follows.

1. change the font color to a simhei, so that the font color is clear.

2. the verification code is displayed in the image (do not jump out of the image)


First

Create the MY_captcha_helper.php file in application/helper/. the content is consistent with that in system/helper/captcha_helper.php. then modify the code.


1234567891011 -- In row 88th // $ text_color = imagecolorallocate ($ im, 204,153,153); $ text_color = imagecolorallocate ($ im, 0 ); // The font is black -- in row 128 // $ x = rand (0, $ img_width/($ length/3); $ x = rand (0, $ img_width/($ length); // X coordinate -- in row 143 // $ y = rand (0, $ img_height/2); $ y = rand (0, $ img_height/3); // Y coordinate

Download my_captcha_helper.php

Then

Modify the loading code in the controller


1 $ This-> load-> helper ("my_captcha"); // load the verification code

Last display


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.