PHP + Ajax implements real-time verification of verification codes and ajax verification Codes

Source: Internet
Author: User

PHP + Ajax implements real-time verification of verification codes and ajax verification Codes

PHP's image processing capabilities are powerful, and it is very easy to make a verification code image. However, when real-time verification is implemented, a problem is encountered:
I used nat123 to map the port of the website on my computer to implement access over the Internet. Originally, I used the cookie method to verify the verification code using js, however, although nat123 enables my website to be accessed through a custom website, the PHP file that generates the verification code is located on the network and is not in the same domain as other pages, after query, the output image is mapped to a folder as a cache file, so other pages are under my website, the page that generates the verification code is in another website, so their cookies cannot be shared. No matter how you set the path and domain, they do not work, so I have to use session to store the verification code, because the session exists on the server, that is, it can be accessed in my local website.
Next is the main scene. I have only a rough understanding of ajax, but I still know its usage and features, I don't know how to get the data from the backend.
First, I analyzed the code of an example to obtain the content of a text file in the background:

function getHTTPObject(){  if(typedef XMLHttpRequest == "undefined")    XMLHttpRequest = function(){      try{ return new ActiveXObject("Msxml2.XMLHTTP.6.0");}      catch(e){}      try{ return new ActiveXObject("Msxml2.XMLHTTP.3.0");}      catch(e){}      try{ return new ActiveXObject("Msxml2.XMLHTTP");}      catch(e){}      return false;    }    return new XMLHttpRequest();}function getNewContent(){  var request = getHTTPObject();  if(request){    request.open("GET","TEST.txt",true);    request.onreadystateschange = function(){      alert(request.responseText);    }  }else{  alert("no support XMLHttpRequest!");  }}

The key is:

  request.open("GET","TEST.txt",true);    request.onreadystateschange =function(){      alert(request.responseText);    }

These three statements specify the processing function triggered when the GET request is sent to the page and the XMLHttpRequest object returns the response, and the data is obtained through request. responseText.

--------------------------------------------------------------------------------

However, the data I want to obtain is the verification code generated by the background PHP file. How can I send the verification code to the background?
Both GET and POST Methods send data to the background. Where can ajax retrieve the background data?
Finally, I found another document and found an example:
The processing statement for returning the XMLHttpRequest object to the response is:

request.onreadystateschange=alertContent;function alertContent(){...  alert(request.responseText);...}

The requested file is check. php.
There are several conditional outputs:

If ($ info) {echo "your user name can be used";} else {echo "this user name has been registered ";}

In the running instance, a message box pops up on the Static Page, such as the output on the check. php page !!!
That is to say, ajax can request the output content of a php page, so the next step is much simpler. Because the image is also the output of the verification code page, you cannot directly access the ajax verification code page, otherwise, the image data will be obtained.
Create a php page:

sission_start();$code="";if(!empty($_session['check']))$code=$_session['check'];echo $code;

GET a request from this page using ajax at the front end, and you can use request. responseText to obtain the verification code.
Then, the verification process is encapsulated in a function triggered by certain conditions, which can be verified in real time.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.