Cocos2d-x network programming connection php server Notes 2, cocos2d-x Network Programming
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:
<? Php/*** @ author blog.anchen8.net * @ copyright 2015 */$ us =$ _ GET ["uname"]; // obtain the uname field from the parameter, that is, the username $ ps = $ _ GET ["upass"]; // obtain the upass field from the parameter, that is, the password // create a mysql database connection. The address is 127.0.0.1, And the login name is root, the password is root $ conn = mysql_connect ("127.0.0.1", "root", "root"); // open the database mysql_select_db ("game "); // new insert query $ SQL = "insert into newsuser (uname, upass) values ('". $ us. "','". $ ps. "')"; // execute this query mysql_query ($ SQL); // close the database mysql_close ($ conn ); Echo "1, insertok";?>
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:
<? Php/*** @ author blog.anchen8.net * @ copyright 2015 */$ us =$ _ GET ["uname"]; $ ps =$ _ GET ["upass"]; // 1. connect to the database $ conn = mysql_connect ("127.0.0.1", "root", "root"); // 2. open the database mysql_select_db ("game"); // 3. create a query $ SQL = "select * from newsuser where uname = '". $ us. "'and upass = '". $ ps. "';"; // 4. execute this query $ result = mysql_query ($ SQL); $ n = mysql_num_rows ($ result); // return the number of results found // 5. close mysql_close ($ conn); if ($ n> 0) // if the number of results is large At 0, that is, the username and password, and the input parameters 1 to {session_start (); $ row = mysql_fetch_array ($ result ); // get the query result $ _ SESSION ["uid"] = $ row ["id"]; // return the id echo "1" in the database "; // The server returns a "1"} else {echo "0"; // If no result is found, the server returns "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.
HOHO! The server is partially done (this can also be called the server), and the next section begins to write the cocos2d-x client part we are best!