Web-based verification code creation function instance code, web-based verification code creation instance

Source: Internet
Author: User

Web-based verification code creation function instance code, web-based verification code creation instance

In web development, the verification code function is often used before logon, registration, or other key functions. Rational Use

The verification function can prevent ddos and crawler attacks.

 Effect:

Implementation principle:

The backend provides an interface for generating verification codes. Each request from the front end generates Verification Code images and verification codes. The verification code images are sent to the client for display,

The verification code string is saved in the subsequent Session, and the business interface to be requested by the front-end is compared with the verification code string in the session.

Implementation ideas:

1. the backend provides APIs for generating Verification Code images.

2. The front-end uses the src attribute in img to request the interface generated by the Verification code.

3. Set a click event for img. Each time you click img, the src value will be changed to request src again.

4. Check whether the entered verification code is correct during page interface operations.

Implementation Code:

Front end:

Html:

<Div class = "centent-top" style = ""> <div class = "centent-left"> <span> * </span> Verification Code: </div> <input type = "text" class = "verification-code-input"> <div class = "verification-code">  </div> <div class = "change"> <span> can't see it clearly? </Span> <span style = "color: #37CAF2; cursor: pointer;" id = "changeImgCode"> change one </span> </div>

Js:

Var yzm = document. getElementById ("yzm"); var changeImgCode = document. getElementById ("changeImgCode"); yzm. onclick = function () {changPin ();} changeImgCode. onclick = function () {changPin ();} // function changPin () {$ ("# yzm") for the verification code "). attr ("src", "/SchoolRoll/accuser/code/check? Time = "+ Math. random ());}

The above is an example code of the Web production verification code function introduced by xiaobian. I hope it will be helpful to you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!

Related Article

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.