PHP development basic tutorial to implement a user registration
We make the simplest registration page. There are three parameters in the registration page:
Username
Password
Repeat Password
After the user writes the three parameters, when clicking submit, the POST record is passed to the connect.php page.
We can process the POST record and write it into the MySQL database, which completes the user registration.
The code is as follows:
In order to achieve faster performance, our code interface has not been beautified. We will guide you through user registration as quickly as possible.
1. Determine repeated passwords
Because there are repeated passwords, if the passwords entered by the user twice are inconsistent, it means whether the password has been entered. Any sense of the next step.
Repeated passwords are still used in many places on the web page. Because the fear is that users will make mistakes. The password was entered incorrectly.
Users may enter two more spaces on the left and right sides when entering their password. Therefore, we will use trim to remove spaces from both sides of passwords and repeated passwords.
if(trim($_POST['password']) != trim($_POST['repassword'])){ exit('两次密码不一致,请返回上一页'); }
2. Prepare the data to be written
We need to write both the user's input data and hidden data to the database.
The visible data are:
We need to remove the spaces on both sides of the username to avoid entering unnecessary information.
In the mysql chapter, we have said that the user's password should not be visible to people including company insiders. Ensure that the password is irreversible. At the initial stage, just learn MD5. We will teach you other encryption methods in the future.
Invisible data includes:
Variables |
Description |
$_POST['username'] |
## Username |
$_POST['password'] |
Password |
The unix timestamp returned by time
REMOTE_ADDR returns the IP address, which we can use ip2long to convert to integer storage.
$username = trim($_POST['username']); $password = md5(trim($_POST['password'])); $time = time(); $ip = ip2long($_SERVER['REMOTE_ADDR']);
3. Connect to database, judge errors, select library and character set
We use mysqli_connect to connect to the database server.
If there is an error, use mysqli_errno to get the error number
When there is an error mysqli_error prints out all errors and exits the program execution
Select the database and set the character set to utf8.
//连接数据库 $conn = mysqli_connect('localhost','root','liwenkaihaha'); //如果有错误,存在错误号 if(mysqli_errno($conn)){ echo mysqli_error($conn); exit; } mysqli_select_db($conn,'user'); mysqli_set_charset($conn,'utf8');
4. Combine SQL statements
We need to write the obtained information into the database. We have obtained the user name, password, creation time, and IP.
Insert the corresponding variables into the SQL statement. The combined SQL statement is as follows:
$sql = "insert into user(username,password,createtime,createip) values('" . $username . "','" . $password . "','" . $time . "','" . $ip . "')";
And our statement to create the table is as follows:
CREATE TABLE IF NOT EXISTS user ( id int(11) NOT NULL, username varchar(30) NOT NULL, password char(32) NOT NULL, createtime int(11) NOT NULL, createip int(11) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Format of the table (field corresponding description):
## Variable |
Description |
User’s registered IP |
5. Send a statement and determine the status
mysqli_query As we said above, two parameters need to be passed in:
Connected resources, the corresponding variable here is $conn.
The SQL statement sent. $sql is already prepared above.
SQL statements can be sent to the MySQL server through mysqli_query. $result is true if sent successfully. Otherwise false.
If successful, we can prompt the user that the registration is successful.
In some cases, mysqli_insert_id() may also need to be used. Print the auto-incremented primary key ID here.
Please remember this knowledge point to avoid forgetting it when needed in the future.
mysqli_insert_id application scenario: newly added row of data. We need to get the automatically growing ID value when inserting this ID value into another table. You need to use this function.
$result = mysqli_query($conn,$sql); if($result){ echo '注册成功'; }else{ echo '注册失败'; } echo '当前用户插入的ID为'.mysqli_insert_id($conn);
6. Close the database connection
Pass the resource variable to the mysqli_close function.
mysqli_close($conn);
The basic implementation code for user registration has been written. What we talked about above are code snippets.
The connect.php code we implemented is as follows:
- Course Recommendations
- Courseware download
-
IntermediateFront-end Vue3 actual combat [handwritten vue project]
2857 people are watching -
ElementaryAPIPOST tutorial [Popularization of technical concepts related to network communication]
1795 people are watching -
IntermediateIssue 22_Comprehensive actual combat
5521 people are watching -
ElementaryIssue 22_PHP Programming
5172 people are watching -
ElementaryIssue 22_Front-end development
8713 people are watching -
IntermediateBig data (MySQL) video tutorial full version
4525 people are watching -
ElementaryGo language tutorial-full of practical information and no nonsense
2794 people are watching -
ElementaryGO Language Core Programming Course
2814 people are watching -
IntermediateJS advanced and BootStrap learning
2563 people are watching -
IntermediateSQL optimization and troubleshooting (MySQL version)
3374 people are watching -
IntermediateRedis+MySQL database interview tutorial
2963 people are watching -
ElementaryDeliver food or learn programming?
5708 people are watching
Students who have watched this course are also learning
- Let's briefly talk about starting a business in PHP
- Quick introduction to web front-end development
- Large-scale practical Tianlongbabu development of Mini version MVC framework imitating the encyclopedia website of embarrassing things
- Getting Started with PHP Practical Development: PHP Quick Creation [Small Business Forum]
- Login verification and classic message board
- Computer network knowledge collection
- Quick Start Node.JS Full Version
- The front-end course that understands you best: HTML5/CSS3/ES6/NPM/Vue/...[Original]
- Write your own PHP MVC framework (40 chapters in depth/big details/must read for newbies to advance)
- About us Disclaimer Sitemap
- php.cn:Public welfare online PHP training,Help PHP learners grow quickly!
id |
username |
##password |
createtime |
createip |
Create IP |