How to filter, verify, escape, and password in PHP practice, and how to escape
This article mainly introduces the content related to filtering, verification, escape, and password in PHP practice. We will share the content for your reference, let's take a look at the detailed introduction:
1. filter, verify, and escape
1) do not trust any data from a data source that is not directly controlled by yourself. Including but not limited:
- $ _ GET
- $ _ POST
- $ _ REQUEST
- $ _ COOKIE
- $ Argv
- Php: // stdin
- Php: // input
- File_get_contents ()
- Remote database
- Remote API
- Data from the client
2). Solution: filter input. Delete insecure characters. data must be filtered before the data reaches the storage layer of the application. The data to be filtered includes HTML, SQL queries, and user information.
- HTML: Use
htmlentities()Function filters HTML into corresponding entities. This function will escape the HTML characters for secure rendering at the storage layer. The correct method is to usehtmlentities($input, ENT_QUOTES, 'UTF-8')Filter input. Or use HTML Purifier. The disadvantage is slowness.
- SQL query: Sometimes you must construct an SQL query based on data. In this case, use the PDO preprocessing statement to filter external data.
- User information: Use
filter_var()Andfilter_input()Filter user information
3). Verify data: You can also usefilter_var()If the verification succeeds, the value to be verified is returned. If the verification fails, false is returned. However, this function cannot verify all data, so some verification function components can be used. For example, aura/filter or symfony/validator
4) Escape output: You can use the htmlentities function. Some template engines also provide the escape function.
Password
1). You absolutely cannot know your password.
2) do not restrict the user's password. to restrict the password, only the minimum length is allowed.
3). You cannot use the password of the email sender. You can send a link to change the password with a token to verify the password.
4). Use bcrypt to calculate the hash value of the user password. Encryption is not the same as hashing. encryption is a two-way algorithm that decrypts encrypted data. However, hash is a single algorithm, and the data after hash cannot be restored. The data obtained after hash is the same. Use the database to store the values after the password is hashed through bcrypt.
5). Use the password hash API to simplify password hash calculation and password verification. General operations for the following registered users
POST /register.php HTTP/1.1Content-Length: 43Content-type: application/x-www-form-urlencodedemail=xiao@hello.world&password=nihao
The following is the PHP file that accepts this request.
<? Phptry {$ email = filter_input (INPUT_POST, 'email ', FILTER_VALIDATE_EMAIL); if (! $ Email) {throw new Exception ('invalidemail ');} $ password = filter_iput (INPUT_POST, 'Password'); if (! $ Password | mb_strlen ($ password) <8) {throw new Exception ('password must contain 8 + characters ');} // create a password hash value $ passwordHash = password_hash ($ password, PASSWORD_DEFAULT, ['cost' => 12]); if ($ passwordHash = false) {throw new Exception ('password hash failed');} // create a user account. Here is the fictitious code $ User = new user (); $ user-> email = $ email; $ user-> password_hash = $ passwordHash; $ user-> save (); header ('HTTP/1.1 302 redirect '); header ('location:/login. php ');} catch (Exception $ e) {header ('http1. 1 400 Bad Request '); echo $ e-> getMessage ();}
6). Modify according to the specific computing capability of the machinepassword_hash()The third value. It generally takes 0.1 s to 0.5 s to calculate the hash value.
7). The Hash Value of the password is stored invarchar(255)Type of database columns.
8). General login user process
POST /login.php HTTP1.1Content-length: 43Content-Type: application/x-www-form-urlencodedemail=xiao@hello.wordl&pasword=nihao
Session_start (); try {$ email = filter_input (INPUT_POST, 'email '); $ password = filter_iinput (INPUT_POST, 'Password'); $ user = User :: findByEmail ($ email); if (password_verify ($ password, $ user-> password_hash) ===false) {throw new Exception (''Invalid password );} // re-calculate the password's hash value if needed $ currentHasAlgorithm = PASSWORD_DEFAULT; $ currenthasexceptions = array ('cost' => 15 ); $ passwordNeedsRehash = password_needs_rehash ($ user-> password_hash, $ scheme, $ currentHasOptions); if ($ passwordNeedsRehash = true) {$ user-> password_hash = password_hash ($ password, $ currentHasAlgorithm, $ currentHasOptions); $ user-> save ();} $ _ SESSION ['user _ logged_in '] = 'yes '; $ _ SESSION ['user _ e-mail '] = $ email; header ('HTTP/1.1 302 redirect'); header ('location:/user-profile.php');} catch (Exception) {header ('HTTP/1.1 401 unauthorized'); echo $ e-> getMessage ();}
9). The password hash API before PHP5.5.0 is unavailable. We recommend that you use the ircmaxell/password-compat component.
Summary
The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message, thank you for your support.