Cocos2d-x network programming connection php server Note 2
After the environment is ready and the tool is ready, let's start to write the php program. In the previous section, we talked about setting the local directory of the website to d:/phpWWW, you create a new testPhp directory in it. The name indicates the location where your job is stored, but it must be in English. By the way, there is no database. The first step is to build your own server database.
1. Database Construction
In the MySQL manager of phpStudy, click MySQL-Front to open the mySql Front-end tool. A dialog box such as "Open login information..." appears, and click the OPEN button to open it.
After entering, the interface is quite clear. The tree structure on the left shows your existing databases. Right-click on the localhost root node and choose create database "game", as shown below:
Right-click the game Database node and choose create> table to create a newsuser table to store the user name and password. The table structure is as follows: the id is the primary key that is created by the system.
Click data browser to enter table data. For example, after entering a row, right-click a grid to insert a new record and delete the record.
I don't want to say more about other detailed usage of this tool. It's just a silly thing. Even people with mediocre IQ can understand it, which is hard for readers.
2. php server program
We write two modules. One is to register and the other is to verify the login. First, let's see how to write the registration program.
Registration:
In the testPhp directory of the website root directory (d:/phpWWW), create a reguser. php file and write it using the phpDesigner tool we mentioned in the previous section. The Code is as follows:
The comment is clear. Just copy it.
The above implements the user registration logic. If you want to see the effect, you can enter http: // 127.0.0.1/testPhp/reguser. php In the browser? Uname = wang & upass = 123 to add a user. "1, insertok" is displayed on the webpage, indicating that the user has been added successfully. Open mysql database and check whether the user has been added successfully.
Verify Logon:
Create another checkLogin02.php file in the testPhp directory. The Code is as follows:
0) // if the number of results found is greater than 0, there is a user name and password and the passed parameters 1 to {session_start (); $ row = mysql_fetch_array ($ result ); // get the query result $ _ SESSION [uid] = $ row [id]; // return id echo 1 in the database; // The server returns a client 1} else {echo 0; // No result found, return 0}?>
The above logic implements the php user name and password verification function. To see the effect, enter http: // 127.0.0.1/testPhp/checkLogin02.php in the browser? Uname = wang & upass = 123. 1 is displayed on the logon success page, and 0 is displayed on the logon Failure page.