Php+ajax Real-time verification _php instance of verification code

Source: Internet
Author: User
PHP's image processing is powerful, making a verification code picture is also very simple, but in the implementation of real-time verification, there is a problem encountered:
I use nat123 to my computer on the site of the port mapping, to achieve the external network access, is the way to use a cookie to verify the verification code with JS, but after the test found that nat123 although my site can be accessed through a custom URL, But the PHP file that generated the verification code in the network location and other pages are not a domain, after the query, because the output of the image, as a cache file is mapped in a folder, so--other pages in my URL, the page that generated the verification code in another URL, So their cookie can't be shared, no matter how to set the path and domain does not work, so I have to use the session storage verification code, because the session exists in the server, that is, in my local URL can be accessed.
Next is the play, before just a rough understanding of Ajax, for its purposes and features still know, get back to the background of the more general concept of the use of the more general do not know how to start.
First, I analyzed the code of the case, the purpose is 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 function that the GET request sends to the page and the XMLHttpRequest object to send back the response, and the data is obtained through Request.responsetext.

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

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

Request.onreadystateschange=alertcontent;function alertcontent () {...  alert (Request.responsetext);

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

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

While running the instance impressively in the static page pop-up message box, content such as check.php page output!!!
That is, Ajax can request the output of a PHP page, so the next simple, because the image is also the output of the Verification code page, so you can not directly Ajax verification code page, or the image of the data also acquired.
To create a new PHP page:

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

Then in the front end with Ajax to this page get a request, you can use Request.responsetext to obtain the verification code.
Then the verification process is encapsulated in a certain conditional trigger function, which can be verified in real time.

The above is the whole content of this article, I hope that everyone's learning has helped, but also hope that we support the script home.

  • 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.