Advertisement
Guest User

Untitled

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