PHP SOLUTIONS: DYNAMIC WEB DESIGN MADE EASY else (Web hosting comparison)
PHP SOLUTIONS: DYNAMIC WEB DESIGN MADE EASY else { // if one of the suspect phrases is found, set Boolean to true if (preg_match($pattern, $val)) { $suspect = true; } } } The isSuspect() function is another piece of code that you may want to just copy and paste without delving too deeply into how it works. The important thing to notice is that the third argument has an ampersand (&) in front of it (&$suspect). This means that any changes made to the variable passed as the third argument to isSuspect() will affect the value of that variable elsewhere in the script. The other feature of this function is that it s what s known as a recursive function. It keeps on calling itself until it finds a value that it can compare against the regex. 3. Don t worry if that last paragraph makes your brain hurt. Calling the function is very easy. You just pass it three values: the $_POST array, the pattern, and the $suspect Boolean variable. Insert the following code immediately after the code in the previous step: // check the $_POST array and any subarrays for suspect content isSuspect($_POST, $pattern, $suspect); Note that you don t put an ampersand in front of $suspect this time. The amper- sand is required only when you define the function in step 2, not when you call it. 4. If any suspect phrases are detected, the value of $suspect changes to true, so you need to set $mailSent to false and delete the $missing array to prevent the email from being sent, and to display an appropriate message in the form. There s also no point in processing the $_POST array any further. Wrap the code that processes the $_POST variables in the second half of an if… else statement like this: if ($suspect) { $mailSent = false; unset($missing); } else { // process the $_POST variables foreach ($_POST as $key => $value) { // assign to temporary variable and strip whitespace if not an array $temp = is_array($value) ? $value : trim($value); // if empty and required, add to $missing array if (empty($temp) && in_array($key, $required)) { array_push($missing, $key); } // otherwise, assign to a variable of the same name as $key elseif (in_array($key, $expected)) { ${$key} = $temp;
We recommend cheap and reliable webhost to host and run your web applications: Coldfusion Web Hosting services.