Simple PHP Data background for User Login

Source: Internet
Author: User
I recently wrote a small iOS project and used logon. Therefore, I used SAE to build a simple pure data backend. It is easy to get started with PHP statements, but it is a little difficult to combine SQL statements. (The code runs on the SAE platform. It is estimated that some methods are different from those of PHP. For example, SaeMysql is a function of SAE itself.) First, let's talk about the basic PHP syntax used. The most basic

I recently wrote a small iOS project and used logon. Therefore, I used SAE to build a simple pure data backend. It is easy to get started with PHP statements, but it is difficult to combine SQL statements. (The code runs on the SAE platform. It is estimated that some methods are different from those of PHP. For example, SaeMysql is a function of SAE itself.) First, let's talk about the basic PHP syntax used. The most basic

I recently wrote a small iOS project and usedLoginTherefore, using SAE is a simple pureDataBackground. PHP statementsSimpleBut it is a little difficult to combine SQL. (The code runs on the SAE platform. It is estimated that some methods are different from those of PHP. For example, SaeMysql is a function of SAE itself)

First, let's talk about the basic PHP syntax used.

The most basic method is to extract parameters from the client request. There are three methods in PHP: GET and POST, which correspond to the client request methods. There is also an all-powerful, as shown below

 

1 $ username = $ _ REQUEST ['username']; // parameter name 2 $ password = $ _ REQUEST ['Password'] in single quotes;

Parameters can be extracted from requests regardless of whether the client is a get request or a post request. _ GET and _ POST

The above is the extractionDataAnd then registerLogin. Write it nowLoginLet's talk about it during registration.DataHow to Create a database.

  LoginThe idea isDataCheck whether there are any elements that match the extracted username parameter. If so, continue to match the password; otherwise, returnLoginFailed. First, let's take a look at how to query, use SQL query statements, and execute and return the queriedDataUse the getData method in SAE.

1 // query statement 2 $ SQL = "select * from table name where username = 'username'"; 3 // createDataLibrary object to execute the preceding SQL statement 4 $ mysql = new SaeMysql (); 5 // after execution, an array of 6 $ arr = $ mysql-> getData (SQL) is returned );

After the query is complete, make a judgment. First, check whether there are any elements in the array.DataIs there any corresponding table?User.

1 if (count (arr) = 0) 2 {3 // return 1 in JSON to indicate that the account does not exist 4 $ dic = array ('success' => '1 '); 5 echo json_encode ($ dic); 6}

IfUserIf the password already exists, you need to determine whether the password is correct.

1 else 2 {3 // retrieve from the arrayUserDictionary 4 $ userDic = $ arr [0]; 5 $ sqlPassword = $ userDic ['Password']; 6 if ($ sqlPassword = $ password) 7 {8 $ dic = array ('success' => '0'); 9 echo json_encode ($ dic ); 10} 11 else12 {13 $ dic = array ('success' => '2'); 14 echo json_encode ($ dic); 15} 16}

In this way, JSON is returned.LoginResult. Don't forget to closeDataLibrary.

1 $mysql ->closeDb();

  LoginThat's it. Registration is troublesome. You need to createDataLibrary, configure some properties. Another 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.