


Xnova (ogame) source code interpretation for PHP web game learning (3)_PHP tutorial
4. User registration (reg.php)
Before looking at the user registration process, I will briefly explain the purpose of the table. Now it is just a rough explanation. We will learn more about it later. You can write down this explanation.
game_aks 联合攻击组记录表,攻击完成删除数据 game_alliance 联盟表 game_annonce 公告表,基本无用 game_banned Ban玩家表 game_buddy 好友表 game_chat 聊天记录表 game_config 系统参数表 game_errors 错误日志表 game_fleets 舰队活动记录表,活动完成删除数据 game_galaxy 星系表 game_iraks 星际导弹活动表,活动完成删除数据 game_lunas 月球列表 game_messages 消息表 game_notes 笔记表 game_planets 星球列表,包括月球 game_rw 战报表 game_statpoints 积分表 game_users 用户表
Now we start the user registration process. At the beginning, we can see that two constants are defined,
define('INSIDE' , true); define('INSTALL' , false);
INSIDE is used to prevent attacks, the value is true; INSTALL is used to record whether it is in the process of installing the game, the value is false. Next are the Include files, extension.inc and common.php; and then the load language file reg.mo. In the following chapters, I will no longer introduce the previous code, everyone already understands it.
Two functions are declared below to send information after successful registration, including user name and password.
function sendpassemail($emailaddress, $username, $password) function mymail($to, $title, $body, $from = '')
Now I will introduce a structure that is often used in the Xnova source code. The following is a pseudo code description,
if ($_POST) { //如果用户有输入 //这里是有用户输入后执行过程 }else{ //这里是无用户输入后的执行过程,包括页面第一次显示的时候 }
Let's look at the user registration process. In order to illustrate the previous structure, the code is specially posted. As we said before, first get the template file, then parse it out and display the page.
if ($_POST) { //代码略 } else { $parse = $lang; $parse['servername'] = $game_config['game_name']; $page = parsetemplate(gettemplate('registry_form'), $parse); display ($page, $lang['registry'], false); }
Let’s look at the process after the user inputs information. A bunch of ifs are used to determine whether the information entered by the user is legal, and the $errors variable is used to record the number of errors, and the $errorlist variable is used to record the error message. If $errors is not zero, the error message will be output; if there is no error, enter the following process:
1. Check whether the username has illegal characters
2. Check whether the email contains illegal characters
3. Check whether the planet name contains illegal characters
4. Encrypt user’s password, md5
5. INSERT user information into table users
6. Get the user ID number just inserted and generated as a backup
7. A large piece of code function, find an empty planet coordinate for this user as the home planet, and use the function CreateOnePlanetRecord() to create the planet. Here, three system parameters LastSettedGalaxyPos, LastSettedSystemPos, and LastSettedPlanetPos are used. The naming should be Clearly, they are the last galaxy, the last solar system, and the last planet position
8. Update the planet ID in the users table to the newly generated planet ID
9. Send a welcome message to this player
10. Send registration information email to this player
11. The registration success page is displayed and the registration is completed
We can find the corresponding code for the above process. The code is not difficult, so it is not listed. The function CreateOnePlanetRecord() will be introduced in detail later, but it is too confusing to say now. Let’s take a look at the function SendSimpleMessage(), which is declared in the SendSimpleMessage.php file.
function SendSimpleMessage ( $Owner, $Sender, $Time, $Type, $From, $Subject, $Message) { global $messfields; if ($Time == '') { $Time = time(); } if ($Sender == null){ $Sender = 0; } $QryInsertMessage = "INSERT INTO {{table}} SET "; $QryInsertMessage .= "'message_owner' = ". $Owner .", "; $QryInsertMessage .= "'message_sender' = ". $Sender .", "; $QryInsertMessage .= "'message_time' = " . $Time . ", "; $QryInsertMessage .= "'message_type' = ". $Type .", "; $QryInsertMessage .= "'message_from' = '". addslashes( $From ) ."', "; $QryInsertMessage .= "'message_subject' = '". addslashes( $Subject ) ."', "; $QryInsertMessage .= "'message_text' = '". addslashes( $Message ) ."';"; doquery( $QryInsertMessage, 'messages'); $QryUpdateUser = "UPDATE {{table}} SET "; $QryUpdateUser .= "'".$messfields[$Type]."' = '".$messfields[$Type]."' + 1, "; $QryUpdateUser .= "'".$messfields[100]."' = '".$messfields[100]."' + 1 "; $QryUpdateUser .= "WHERE "; $QryUpdateUser .= "'id' = '". $Owner ."';"; doquery( $QryUpdateUser, 'users'); }
The function of the function is to insert detailed messages into the messages table and update the number of messages in the users table. It is very clear, haha. The function of another function message() is basically the same as display(), so I won’t write it in detail. Just take a look at it yourself. At this point, the analysis of the user registration process is complete.

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.