Using PHPRPC to implement AJAX secure login 1th/2 page _php Basics

Source: Internet
Author: User
Tags sha1
Phprpc Unlike other Ajax frameworks, Phprpc follows the principle of "do one thing and do it well". It is only responsible for data transmission, and will deliver the best! It makes it easy to do things that you've never done before in traditional Ajax mode or are hard to do!
One of the main reasons to believe that many people choose PHPRPC is that it makes it easier for you to develop AJAX applications! Although the strength of PHPRPC is not limited to this, but I have to admit, this is indeed a phprpc bright spot!
So, our first stop, let's see how to write Ajax apps with Phprpc 3.0来.

Here's a simple example (even a boring example: Mrgreen:) to illustrate how to use PHPRPC 3.0 to write AJAX applications. This example is a good illustration of how the MVC pattern mentioned above is working specifically. Here we first take PHP as the server side of the case to illustrate. Our first example is simple, the client enters a string, and the server calculates its SHA1 value.
Copy Code code as follows:

<?php
Require_once (".. /php/phprpc_server.php ");
$server = new Phprpc_server ();
$server->add ("SHA1");
$server->start ();
?>

With just such a simple 4 lines of code, the PHP built-in SHA1 function is released. Now, the client can use this function directly.
Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title> Calculation sha1</title>
<script type= "Text/javascript" src= ". /js/compressed/phprpc_client.js "></script>
<script type= "Text/javascript" >
var rpc = new Phprpc_client (' sha1.php ', [' SHA1 ']);
function $ (ID) {
return document.getElementById (ID);
}
function Showresult (Result) {
$ (' SHA1 '). Value = result;
}
function SHA1 () {
var input = $ (' SHA1 '). Value;
$ (' SHA1 '). Value = "Loading ...";
RPC.SHA1 (input, showresult);
}
</script>
<body>
<input type= "text" id= "SHA1"/>
<input type= "button" value= "Calculate SHA1" onclick= "SHA1 ()"/>
</body>

This example is very simple, and there are only two statements related to PHPRPC, one of which is:
Copy Code code as follows:

var rpc = new Phprpc_client (' sha1.php ', [' SHA1 ']);

This statement is used to create a Phprpc_client object, the first parameter is the server address, where you can use a relative path or an absolute path.
And this path can cross domain! So you can do cross-domain applications!
The second parameter is the list of server-side function names you need to use, so, although we have only one function here, we have to write the form of an array. So we have a client object that can invoke the server-side method.
So how do you call it? Let's look at the second statement about PHPRPC:
Copy Code code as follows:

RPC.SHA1 (input, showresult);

This statement is fairly simple, and you will find that we call it directly using the function name of the server-side publication as a method of the RPC object. Its first parameter is the parameter value of the SHA1 function. The second parameter is a function, which is a callback function, that is, when the server-side method is executed, it is automatically called to complete the processing of the result. It is defined in the previous, and you will find that it has a parameter result, which is the return value of our remote procedure SHA1, which is passed through this parameter of the callback function.

Current 1/2 page 12 Next read the full text
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.