PHP SOLUTIONS: DYNAMIC WEB DESIGN (Free web host) MADE EASY 4.
PHP SOLUTIONS: DYNAMIC WEB DESIGN MADE EASY 4. Add the second test after the appropriate comment like this: // check that the passwords match elseif ($pwd != $_POST[’conf_pwd’]) { $result = ‘Your passwords don’t match’; } 5. You can now add the else clause that runs only if the first two tests fail, indicating that the input is OK. This is where the main action takes place. // continue if OK else { // main processing code goes here } Let s pause to consider what the main script needs to do. First, you need to encrypt the password by combining it with the username as a salt. Then, before writing the details to a text file, you must check whether the username is unique. This presents a problem of which mode to use with fopen(). The various fopen() modes are described in Chapter 7. Ideally, you want the internal pointer at the beginning of the file so that you can loop through existing records. The r+ mode does this, but the operation fails unless the file already exists. You can t use w+, because it deletes existing content. You can t use x+ either, because it fails if a file of the same name already exists. That leaves a+ as the only option with the flexibility you need: it creates the file if necessary, and lets you read and write. The file is empty the first time you run the script (you can tell because the filesize() function returns 0), so you can go ahead and write the details. If filesize() doesn t return 0, you need to reset the internal pointer and loop through the records to see if the username is already registered. If there s a match, you break out of the loop and prepare an error message. If there isn t a match by the end of the loop, you not only know it s a new username, you also know you re at the end of the file. So, you write a new line followed by the new record. Now that you understand the flow of the script, you can insert it into register.php. 6. Replace the placeholder comment in the else clause from the preceding step with the following code: // continue if OK else { // encrypt password, using username as salt $pwd = sha1($username.$pwd); // define filename and open in read-write append mode $filename = ‘C:/private/encrypted.txt’; $file = fopen($filename, ‘a+’); // if filesize is zero, no names yet registered // so just write the username and password to file if (filesize($filename) === 0) {
If you are searching for cheap webhost for your web application, please visit MySQL5 Web Hosting services.