Real-time verification _php instance of PHP+AJAX implementation verification code

Source: Internet
Author: User

PHP's image processing is powerful, make a verification code picture is very simple, but, in the implementation of real-time verification, there is a difficult problem:
I use nat123 on my computer's web site for port mapping, to achieve the extranet access, It is the way to use a cookie to verify the code with JS, but the test found that nat123 although my site can be accessed through a customized web site, but the production of verification code PHP file on the network location and other pages are not a domain, after the query that Because the output of the picture, is as a cached file mapped in a folder, so--other pages in my web site, generate the Captcha page in another URL, so their cookies can not be shared, no matter how the set path and domain is not useful, So I have to use the session storage verification code, because the session exists in the server, that is, in my local Web site can be accessed. The
Next is the play, which was just a rough glimpse of Ajax, and for its purpose and features, it was known that the more generic concept of getting data from the background was used specifically to not know how to do it.
First, I analyzed the code for the case, which was used to get the contents 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 sentences specify the processing functions that are triggered when the page to which the GET request is sent and the XMLHttpRequest object is sent back to the response, and the data is obtained by Request.responsetext.

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

But I want to get the data is the background PHP file generated by the verification code, how to let the background to send verification code?
Whether get or post means to send data to the background, Ajax from where to retrieve the background data?
Baffled, I queried another information to find an example:
The processing statement where the XMLHttpRequest object sends back the response is:

request.onreadystateschange=alertcontent;
function Alertcontent () {
...
  alert (request.responsetext);
...
}

The requested file is check.php
There are a few of these conditional outputs:

if ($info) {
  echo "Your user name can be used";
} else {
  echo "This username has been registered";
}

And the Run instance effect chart impressively in static page pop-up message box, content such as check.php page output!!!
That is to say Ajax can request to a PHP page output, so, the next is much simpler, because the picture is also the output of the Verification code page, so can not directly Ajax verification code page, or the image of the data also obtained.
Create a new PHP page:

Sission_start ();
$code = "";
if (!empty ($_session[' check ')) $code =$_session[' Check '];
Echo $code;

Then in front of Ajax to this page get a request, you can use Request.responsetext to obtain the authentication code.
The verification process is then encapsulated in a function triggered by a certain condition and can be validated in real time.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.