Guest User

My PHP Email Code

a guest
Jul 13th, 2020
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.72 KB | None | 0 0
  1. <?php
  2. // Include config file
  3. require_once "config.php";
  4. use PHPMailer;
  5.  
  6. // Define variables and initialize with empty values
  7. $username = $password = $confirm_password = "";
  8. $username_err = $password_err = $confirm_password_err = "";
  9.  
  10. // Processing form data when form is submitted
  11. if($_SERVER["REQUEST_METHOD"] == "POST"){
  12.  
  13. // Validate username
  14. if(empty(trim($_POST["username"]))){
  15. $username_err = "Please enter a username.";
  16. } else{
  17. // Prepare a select statement
  18. $sql = "SELECT id FROM users WHERE username = ?";
  19.  
  20. if($stmt = mysqli_prepare($link, $sql)){
  21. // Bind variables to the prepared statement as parameters
  22. mysqli_stmt_bind_param($stmt, "s", $param_username);
  23.  
  24. // Set parameters
  25. $param_username = trim($_POST["username"]);
  26.  
  27. // Attempt to execute the prepared statement
  28. if(mysqli_stmt_execute($stmt)){
  29. /* store result */
  30. mysqli_stmt_store_result($stmt);
  31.  
  32. if(mysqli_stmt_num_rows($stmt) == 1){
  33. $username_err = "This username is already taken.";
  34. } else{
  35. $username = trim($_POST["username"]);
  36. }
  37. } else{
  38. echo "Oops! Something went wrong. Please try again later.";
  39. }
  40.  
  41. // Close statement
  42. mysqli_stmt_close($stmt);
  43. }
  44. }
  45.  
  46. if(empty(trim($_POST["email"]))){
  47. $email_err = "Please enter an email.";
  48. } else{
  49. // Prepare a select statement
  50. $sql = "SELECT id FROM users WHERE email = ?";
  51.  
  52. if($stmt = mysqli_prepare($link, $sql)){
  53. // Bind variables to the prepared statement as parameters
  54. mysqli_stmt_bind_param($stmt, "s", $param_email);
  55.  
  56. // Set parameters
  57. $param_email = trim($_POST["email"]);
  58.  
  59. // Attempt to execute the prepared statement
  60. if(mysqli_stmt_execute($stmt)){
  61. /* store result */
  62. mysqli_stmt_store_result($stmt);
  63.  
  64. if(mysqli_stmt_num_rows($stmt) == 1){
  65. $email_err = "This email is already in use.";
  66. } else{
  67. $email = trim($_POST["email"]);
  68. }
  69. } else{
  70. echo "Oops! Something went wrong. Please try again later.";
  71. }
  72.  
  73. // Close statement
  74. mysqli_stmt_close($stmt);
  75. }
  76. }
  77.  
  78. // Validate password
  79. if(empty(trim($_POST["password"]))){
  80. $password_err = "Please enter a password.";
  81. } elseif(strlen(trim($_POST["password"])) < 6){
  82. $password_err = "Password must have atleast 6 characters.";
  83. } else{
  84. $password = trim($_POST["password"]);
  85. }
  86.  
  87. // Validate confirm password
  88. if(empty(trim($_POST["confirm_password"]))){
  89. $confirm_password_err = "Please confirm password.";
  90. } else{
  91. $confirm_password = trim($_POST["confirm_password"]);
  92. if(empty($password_err) && ($password != $confirm_password)){
  93. $confirm_password_err = "Password did not match.";
  94. }
  95. }
  96.  
  97. // Check input errors before inserting in database
  98. if(empty($username_err) && empty($password_err) && empty($confirm_password_err) && empty($email_err)){
  99.  
  100. // Prepare an insert statement
  101. $sql = "INSERT INTO users (username, password, email, isEmailConfirmed, token) VALUES (?, ?, ?, ?, ?)";
  102.  
  103. if($stmt = mysqli_prepare($link, $sql)){
  104. // Bind variables to the prepared statement as parameters
  105. mysqli_stmt_bind_param($stmt, "ss", $param_username, $param_password, $param_email, $token);
  106.  
  107. // Set parameters
  108. $param_username = $username;
  109. $param_password = password_hash($password, PASSWORD_DEFAULT); // Creates a password hash
  110. $param_email = $email
  111. $token = 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789!$/()*';
  112. $token = str_shuffle($token);
  113. $token = substr($str, 0, 10);
  114. $isEmailConfirmed = "0";
  115.  
  116. include_once "PHPMailer/PHPMailer.php";
  117.  
  118. $mail = new PHPMailer();
  119. $mail->setFrom('no-reply@XXX.XXX');
  120. $mail->addAddress($email, $name);
  121. $mail->Subject = "Verification Code";
  122. $mail->isHTML(true);
  123. $mail->Body = "
  124. Please follow the link below to verify:<br /><br />
  125.  
  126. <a href="http://www.XXX.XXX/confirm?email=$email&token=$token
  127. ">Verify</a>
  128.  
  129. If you cant click the link please copy and paste this link in the browser:<br /><br />
  130.  
  131. http://www.XXX.XXX/confirm?email=$email&token=$token
  132. ";
  133.  
  134. if($mail->send()){
  135. echo "You have been registered! Please visit your email";
  136. }
  137.  
  138.  
  139. // Attempt to execute the prepared statement
  140. if(mysqli_stmt_execute($stmt)){
  141. // Redirect to login page
  142. } else{
  143. echo "Something went wrong. Please try again later.";
  144. }
  145.  
  146. // Close statement
  147. mysqli_stmt_close($stmt);
  148. }
  149. }
  150.  
  151. // Close connection
  152. mysqli_close($link);
  153. }
  154. ?>
  155.  
  156. <!DOCTYPE html>
  157. <html lang="en">
  158. <head>
  159. <meta charset="UTF-8">
  160. <title>Sign Up</title>
  161. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
  162. <style type="text/css">
  163. body{ font: 14px sans-serif; }
  164. .wrapper{ width: 350px; padding: 20px; }
  165. </style>
  166. </head>
  167. <body>
  168. <div class="wrapper">
  169. <h2>Sign Up</h2>
  170. <p>Please fill this form to create an account.</p>
  171. <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
  172. <div class="form-group <?php echo (!empty($username_err)) ? 'has-error' : ''; ?>">
  173. <label>Username</label>
  174. <input type="text" name="username" class="form-control" value="<?php echo $username; ?>">
  175. <span class="help-block"><?php echo $username_err; ?></span>
  176. </div>
  177.  
  178. <div class="form-group <?php echo (!empty($email_err)) ? 'has-error' : ''; ?>">
  179. <label>Email</label>
  180. <input type="text" name="email" class="form-control" value="<?php echo $email; ?>">
  181. <span class="help-block"><?php echo $email_err; ?></span>
  182. </div>
  183.  
  184. <div class="form-group <?php echo (!empty($password_err)) ? 'has-error' : ''; ?>">
  185. <label>Password</label>
  186. <input type="password" name="password" class="form-control" value="<?php echo $password; ?>">
  187. <span class="help-block"><?php echo $password_err; ?></span>
  188. </div>
  189. <div class="form-group <?php echo (!empty($confirm_password_err)) ? 'has-error' : ''; ?>">
  190. <label>Confirm Password</label>
  191. <input type="password" name="confirm_password" class="form-control" value="<?php echo $confirm_password; ?>">
  192. <span class="help-block"><?php echo $confirm_password_err; ?></span>
  193. </div>
  194. <div class="form-group">
  195. <input type="submit" class="btn btn-primary" value="Submit">
  196. <input type="reset" class="btn btn-default" value="Reset">
  197. </div>
  198. <p>Already have an account? <a href="../login">Login here</a>.</p>
  199. </form>
  200. </div>
  201. </body>
  202. </html>
Add Comment
Please, Sign In to add comment