Last active
January 3, 2016 20:19
-
-
Save miccheng/8513972 to your computer and use it in GitHub Desktop.
Alternatives to the massive if-else statements.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function register() | |
| { | |
| // Just return the form because there's no need going through the rest of the call stack below. | |
| if (empty($_POST)) return register_form(); | |
| // Setting some defaults. | |
| $msg = ''; | |
| $success = false; | |
| try | |
| { | |
| /* Rule: Place the error message close to the validation rule. | |
| Updating the validation rule should help you notice & update the error message. */ | |
| if (empty($_POST['user_name'])) | |
| throw new Exception('Empty Username'); | |
| if (empty($_POST['user_password_new'])) | |
| throw new Exception('Empty Password'); | |
| if ($_POST['user_password_new'] !=== $_POST['user_password_repeat']) | |
| throw new Exception('Passwords do not match'); | |
| if (strlen($_POST['user_password_new']) < 6) | |
| throw new Exception('Password must be at least 6 characters'); | |
| if (strlen($_POST['user_name']) > 64 || strlen($_POST['user_name']) < 2) | |
| throw new Exception('Username must be between 2 and 64 characters'); | |
| if (!preg_match('/^[a-z\d]{2,64}$/i', $_POST['user_name'])) | |
| throw new Exception('Username must be only a-z, A-Z, 0-9'); | |
| $user = read_user($_POST['user_name']); | |
| if (isset($user['user_name'])) | |
| throw new Exception('Username already exists'); | |
| if (empty($_POST['user_email'])) | |
| throw new Exception('Email cannot be empty'); | |
| if (strlen($_POST['user_email'] > 64)) | |
| throw new Exception('Email must be less than 64 characters'); | |
| if (filter_input(INPUT_POST, 'user_email', FILTER_VALIDATE_EMAIL) === false) | |
| throw new Exception('You must provide a valid email address'); | |
| create_user(); | |
| $success = true; | |
| $msg = 'You are now registered so please login'; // Update with the success message | |
| } | |
| catch (Exception $ex) | |
| { | |
| // All exceptions are handled in the same way. | |
| $msg = $ex->getMessage(); | |
| } | |
| if (!empty($msg)) | |
| $_SESSION['msg'] = $msg; // Needed for both success/failure case anyways | |
| if ($success) | |
| header('Location: '.$_SERVER['PHP_SELF']); exit(); // Just redirect | |
| else | |
| return register_form(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function register() | |
| { | |
| if (!empty($_POST)) { | |
| $msg = ''; | |
| if ($_POST['user_name']) { | |
| if ($_POST['user_password_new']) { | |
| if ($_POST['user_password_new'] === $_POST['user_password_repeat']) { | |
| if (strlen($_POST['user_password_new']) > 5) { | |
| if (strlen($_POST['user_name']) < 65 && strlen($_POST['user_name']) > 1) { | |
| if (preg_match('/^[a-z\d]{2,64}$/i', $_POST['user_name'])) { | |
| $user = read_user($_POST['user_name']); | |
| if (!isset($user['user_name'])) { | |
| if ($_POST['user_email']) { | |
| if (strlen($_POST['user_email'] < 65)) { | |
| if (filter_var($_POST['user_email'], FILTER_VALIDATE_EMAIL)) { | |
| create_user(); | |
| $_SESSION['msg'] = 'You are now registered so please login'; | |
| header('Location: '.$_SERVER['PHP_SELF']); | |
| exit(); | |
| } else $msg = 'You must provide a valid email address'; | |
| } else $msg = 'Email must be less than 64 characters'; | |
| } else $msg = 'Email cannot be empty'; | |
| } else $msg = 'Username already exists'; | |
| } else $msg = 'Username must be only a-z, A-Z, 0-9'; | |
| } else $msg = 'Username must be between 2 and 64 characters'; | |
| } else $msg = 'Password must be at least 6 characters'; | |
| } else $msg = 'Passwords do not match'; | |
| } else $msg = 'Empty Password'; | |
| } else $msg = 'Empty Username'; | |
| $_SESSION['msg'] = $msg; | |
| } | |
| return register_form(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment