Simple PHP Data background for user login, php background for User Login

Source: Internet
Author: User

Simple PHP Data background for user login, php background for User Login

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 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 data extraction, and then registration and login. Now, let's write about the login. When registering, let's talk about how to create a database, and then let's talk about it in detail.

Login, the idea is to find in the database whether there are elements that match the extracted username parameter, if there is, continue to match the password, otherwise return logon failure. First, let's take a look at how to query, use SQL query statements, and execute and return the queried data. Use the getData method in SAE.

1 // query statement 2 $ SQL = "select * from table name where username = 'username'"; 3 // create a database object, to execute the preceding SQL statement 4 $ mysql = new SaeMysql (); 5 // after the execution is complete, an array 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 to determine whether there are users in the data table.

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}

If the user exists, you need to further determine whether the password is correct.

1 else 2 {3 // retrieve the user dictionary from the Array 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, the logon result is returned in JSON. Don't forget to close the database.

1 $mysql ->closeDb();

Log on here. Registration is troublesome. You need to create a database and configure some attributes. Another article


How to input data in a simple php background

Find a free php source code website.
Make a simple login page in PHP

You try again.
It is impossible for me to directly connect to the database you requested, because I do not know your database information and table design, so you need to replace the hostname, db_user, password in the code, the database, table name, and field name can be used directly after they are changed. I have modified the correct and wrong user name and password.

<? Php
Session_start ();
$ Host = "localhost"; // server name
$ Db_user = "root"; // User Name
$ Db_password = "74862856"; // Password
$ Db = "TEST"; // the database to connect
$ Link_id = @ mysql_connect ($ host, $ db_user, $ db_password) or die ("database connection failed". mysql_error ());
$ Db_selected = mysql_select_db ($ db, $ link_id );
If (! $ Db_selected ){
Die ("the specified database is not found". mysql_error ());

If (isset ($ _ COOKIE ['user']) {

$ SQL = 'select * from name where user = "'. $ _ COOKIE ['user'].'" ';
$ Result = @ mysql_query ($ SQL, $ link_id) or die ("SQL statement error ");
$ Row = mysql_fetch_array ($ result, MYSQL_ASSOC );
If (isset ($ row) {// if the user exists in the Database
Header ("Location: index. php"); // valid COOKIE directly jumps to the specified Interface
} Else {
$ _ COOKIE ['user'] = ""; // clear an invalid COOKIE
Header ("Location: login. php"); // reload the interface


If (isset ($ _ POST ['submitted']) {

$ User = $ _ POST ['user'];
$ Pwd = $ _ POST ['pwd'];
$ SQL = 'select * from name where user = "'. $ user .'"';
$ Result = @ mysql_query ($ SQL, $ link_id) or die ("SQL statement error ");
$ Row = mysql_fetch_array ($ result, MYSQL_ASSOC );
$ Cmp_pwd = $ row ['Password'];
If ($ cmp_p... remaining full text>

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