SHOW:
|
|
- or go back to the newest paste.
| 1 | <?php | |
| 2 | ||
| 3 | if(isset($_POST['submit'])){
| |
| 4 | ||
| 5 | - | $error = array(); |
| 5 | + | |
| 6 | if (!isset($_POST['email'])) $error[] = "Please fill out all fields"; | |
| 7 | if (!isset($_POST['password'])) $error[] = "Please fill out all fields"; | |
| 8 | ||
| 9 | $username = $_POST['username']; | |
| 10 | ||
| 11 | if(!$user->isValidUsername($username)){
| |
| 12 | $error[] = 'Usernames must be at least 3 Alphanumeric characters'; | |
| 13 | } else {
| |
| 14 | $stmt = $db->prepare('SELECT username FROM users WHERE username = :username');
| |
| 15 | $stmt->execute(array(':username' => $username));
| |
| 16 | $row = $stmt->fetch(PDO::FETCH_ASSOC); | |
| 17 | ||
| 18 | if(!empty($row['username'])){
| |
| 19 | $error[] = 'Username provided is already in use.'; | |
| 20 | } | |
| 21 | ||
| 22 | } | |
| 23 | ||
| 24 | if(strlen($_POST['password']) < 3){
| |
| 25 | $error[] = 'Password is too short.'; | |
| 26 | } | |
| 27 | ||
| 28 | if(strlen($_POST['passwordConfirm']) < 3){
| |
| 29 | $error[] = 'Confirm password is too short.'; | |
| 30 | } | |
| 31 | ||
| 32 | if($_POST['password'] != $_POST['passwordConfirm']){
| |
| 33 | $error[] = 'Passwords do not match.'; | |
| 34 | } | |
| 35 | ||
| 36 | $email = htmlspecialchars_decode($_POST['email'], ENT_QUOTES); | |
| 37 | if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
| |
| 38 | $error[] = 'Please enter a valid email address'; | |
| 39 | } else {
| |
| 40 | $stmt = $db->prepare('SELECT email FROM users WHERE email = :email');
| |
| 41 | $stmt->execute(array(':email' => $email));
| |
| 42 | $row = $stmt->fetch(PDO::FETCH_ASSOC); | |
| 43 | ||
| 44 | if(!empty($row['email'])){
| |
| 45 | $error[] = 'Email provided is already in use.'; | |
| 46 | } | |
| 47 | ||
| 48 | } | |
| 49 | ||
| 50 | if(!isset($error)){
| |
| 51 | ||
| 52 | $hashedpassword = $user->password_hash($_POST['password'], PASSWORD_BCRYPT); | |
| 53 | ||
| 54 | $activation = md5(uniqid(rand(),true)); | |
| 55 | ||
| 56 | try {
| |
| 57 | ||
| 58 | $stmt = $db->prepare('INSERT INTO users (username,password,email,active) VALUES (:username, :password, :email, :active)');
| |
| 59 | $stmt->execute(array( | |
| 60 | ':username' => $username, | |
| 61 | ':password' => $hashedpassword, | |
| 62 | ':email' => $email, | |
| 63 | ':active' => $activation | |
| 64 | )); | |
| 65 | $id = $db->lastInsertId('userID');
| |
| 66 | ||
| 67 | $to = $_POST['email']; | |
| 68 | $subject = "Account Confirmation"; | |
| 69 | $body = "<p>Thank you for signing up\</p> | |
| 70 | <p>To activate your account, please click on this link: <a href='".DIR."activate.php?x=$id&y=$activation'>".DIR."activate.php?x=$id&y=$activation</a></p> | |
| 71 | <p>Regards Site Admin</p>"; | |
| 72 | ||
| 73 | $mail = new Mail(); | |
| 74 | $mail->setFrom(SITEEMAIL); | |
| 75 | $mail->addAddress($to); | |
| 76 | $mail->subject($subject); | |
| 77 | $mail->body($body); | |
| 78 | $mail->send(); | |
| 79 | ||
| 80 | //redirect to index page | |
| 81 | header('Location: index.php?action=joined');
| |
| 82 | exit; | |
| 83 | ||
| 84 | //else catch the exception and show the error. | |
| 85 | } catch(PDOException $e) {
| |
| 86 | $error[] = $e->getMessage(); | |
| 87 | } | |
| 88 | ||
| 89 | if(!isset($error)){ $error = array(); }
| |
| 90 | } | |
| 91 | ||
| 92 | } | |
| 93 | ||
| 94 | ?> | |
| 95 | <div class="tab-pane" id="registerTab"> | |
| 96 | - | <div class="tab-pane" id=registerTab> |
| 96 | + | |
| 97 | <form role="form" method="post" action="" autocomplete="off"> | |
| 98 | ||
| 99 | <?php | |
| 100 | if(isset($error)){
| |
| 101 | foreach($error as $error){
| |
| 102 | echo '<p class="bg-danger">'.$error.'</p>'; | |
| 103 | } | |
| 104 | } | |
| 105 | ||
| 106 | //if action is joined show sucess | |
| 107 | if(isset($_GET['action']) && $_GET['action'] == 'joined'){
| |
| 108 | echo "<h2 class='bg-success'>Registration successful, please check your email to activate your account.</h2>"; | |
| 109 | } | |
| 110 | ?> | |
| 111 | ||
| 112 | <div class="form-group"> | |
| 113 | <input type="email" name="email" id="email" class="form-control" placeholder="Email" required="required" value="<?php if(isset($error)){ echo htmlspecialchars($_POST['email'], ENT_QUOTES); } ?>" tabindex="1">
| |
| 114 | </div> | |
| 115 | <div class="form-group"> | |
| 116 | <input type="text" name="username" id="username" class="form-control"placeholder="User Name" required="required" value="<?php if(isset($error)){ echo htmlspecialchars($_POST['username'], ENT_QUOTES); } ?>" tabindex="2">
| |
| 117 | </div> | |
| 118 | <div class="form-group"> | |
| 119 | <input type="password" name="password" id="password"class="form-control" placeholder="Password" required="required" tabindex="3"> | |
| 120 | </div> | |
| 121 | <div class="form-group"> | |
| 122 | <input type="password" name="passwordConfirm" id="passwordConfirm" class="form-control" placeholder="Confirm Password" required="required" tabindex="4"> | |
| 123 | </div> | |
| 124 | <div class="form-group"> | |
| 125 | <button type="submit" name="submit" class="btn btn-primary btn-lg btn-block login-btn" tabindex="5">Register</button> | |
| 126 | </div> | |
| 127 | </form> | |
| 128 | </div> | |
| 129 | <div class="modal-footer"> | |
| 130 | <div class="login-footer"> | |
| 131 | <span class="login-footer-item"> | |
| 132 | Have an Account? <a href="#registerTab" data-target="#registerTab">Sign in</a> | |
| 133 | </span> | |
| 134 | </div> | |
| 135 | </div> | |
| 136 | </div> |