We often set verification codes on the submission page to prevent repeated submission, but sometimes it is not necessarily useful to set verification codes. Let's take a look at the example below: to implement the user registration function, you need to submit the following fields: username, password, regCode. After successful registration, the logon page is displayed. The acceptance page is processed as follows: if ($ _ POST [regCode]! $ _ SESSIO
We often set verification codes on the submission page to prevent repeated submission, but sometimes it is not necessarily useful to set verification codes. Let's take a look at the example below: to implement the user registration function, you need to submit the following fields: username, password, regCode. After successful registration, the logon page is displayed. The acceptance page is processed as follows: if ($ _ POST ['regcode']! =_ _ SESSIO
We often set verification codes on the submission page to prevent repeated submission, but sometimes it is not necessarily useful to set verification codes. Let's look at the example below:
To enable user registration, you must submit the following fields: username, password, and regCode. After successful registration, the logon page is displayed. The acceptance page is processed as follows:
If ($ _ POST ['regcode']! = $ _ SESSION ['regcode']) {msg ('inconsistent Verification Code ');} $ username = $ _ POST ['username']; $ password = $ _ POST ['Password']; if (! Check_username ($ username) {msg ('invalid username ');} if (! Check_password ($ password) {msg ('invalid password ');} if (check_user_exists ($ username) {msg ('user name occupied ');} if (! $ This-> user_model-> save (array ('username' => $ username, 'Password' => $ password,) {msg ('registration failed ');} header ("Location: login. php ");
We can see that the user name and password have been verified and the verification code has been set. Is that okay? We can see that the server only performs verification code verification, but the verification code is not deleted after the verification code is used. Can the verification code be reused in the next request?
We know that the verification code is usually stored in the SESSION. As long as the verification code in the SESSION is not deleted and the CookieID requested by the client is the same, the server considers it as the same user, based on the same CookieID, the server must be able to obtain the verification code again.
We can simulate this action using a program and try to execute it multiple times:
$cookie = 'PHPSESSID=pgfac511q271k5vvije46kf634';$post_data = array( 'username' => 'pp'.time(), 'password' => time(), 'regCode' => 'dksi',);$ch = curl_init("http://localhost/regist.php");curl_setopt($ch, CURLOPT_HEADER, true);curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);curl_setopt($ch, CURLOPT_POST, true);curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));curl_setopt($ch,CURLOPT_HTTPHEADER, array("Cookie: ".$cookie));$rs = curl_exec($ch);if(curl_errno($ch)) { throw new Exception('Curl Error:'.curl_error($ch));}curl_close($ch);var_dump($rs);
We only need to get the generated cookie content and the displayed verification code from the browser, which can be easily obtained through ff or chrome. After multiple executions, you will find that multiple records are inserted into the database, that is, the Verification Code has not been used to prevent malicious attacks. Similarly, you can try to directly refresh F5 when submitting the page. The above logic will prompt that the user name is in use.
The root cause of this problem is that the verification code can be used again after it is used once, so the solution is very simple. You only need to clear the value of the Verification Code after the registration is successful, this value is no longer in the session on the server when the request is sent again, and the Verification Code fails.
This is a very small problem, but it happens inadvertently in the system. Do not make the verification code meaningless. Wait until millions of users have been refreshed before knowing it.
Original article address: useless verification code. Thank you for sharing it with the original author.