Advertisement
Guest User

Untitled

a guest
Jun 13th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.51 KB | None | 0 0
  1. <?php
  2. require('inc/db.php');
  3. // If form submitted, insert values into the database.
  4. // Define variables and initialize with empty values
  5. $username = $password = $confirm_password = "";
  6. $username_err = $password_err = $confirm_password_err = "";
  7.  
  8. // Processing form data when form is submitted
  9. if($_SERVER["REQUEST_METHOD"] == "POST"){
  10.  
  11. // Validate username
  12. if(empty(trim($_POST["username"]))){
  13. $username_err = "Please enter a username.";
  14. } else{
  15. // Prepare a select statement
  16. $sql = "SELECT id FROM users WHERE username = :username";
  17.  
  18. if($stmt = $pdo->prepare($sql)){
  19. // Bind variables to the prepared statement as parameters
  20. $stmt->bindParam(':username', $param_username, PDO::PARAM_STR);
  21.  
  22. // Set parameters
  23. $param_username = trim($_POST["username"]);
  24.  
  25. // Attempt to execute the prepared statement
  26. if($stmt->execute()){
  27. if($stmt->rowCount() == 1){
  28. $username_err = "This username is already taken.";
  29. } else{
  30. $username = trim($_POST["username"]);
  31. }
  32. } else{
  33. echo "Oops! Something went wrong. Please try again later.";
  34. }
  35. }
  36.  
  37. // Close statement
  38. unset($stmt);
  39. }
  40.  
  41. // Validate password
  42. if(empty(trim($_POST['password']))){
  43. $password_err = "Please enter a password.";
  44. } elseif(strlen(trim($_POST['password'])) < 6){
  45. $password_err = "Password must have atleast 6 characters.";
  46. } else{
  47. $password = trim($_POST['password']);
  48. }
  49.  
  50. // Validate confirm password
  51. if(empty(trim($_POST["confirm_password"]))){
  52. $confirm_password_err = 'Please confirm password.';
  53. } else{
  54. $confirm_password = trim($_POST['confirm_password']);
  55. if($password != $confirm_password){
  56. $confirm_password_err = 'Password did not match.';
  57. }
  58. }
  59.  
  60. // Check input errors before inserting in database
  61. if(empty($username_err) && empty($password_err) && empty($confirm_password_err)){
  62.  
  63. // Prepare an insert statement
  64. $sql = "INSERT INTO users (username, password) VALUES (:username, :password)";
  65.  
  66. if($stmt = $pdo->prepare($sql)){
  67. // Bind variables to the prepared statement as parameters
  68. $stmt->bindParam(':username', $param_username, PDO::PARAM_STR);
  69. $stmt->bindParam(':password', $param_password, PDO::PARAM_STR);
  70.  
  71. // Set parameters
  72. $param_username = $username;
  73. $param_password = password_hash($password, PASSWORD_DEFAULT); // Creates a password hash
  74.  
  75. // Attempt to execute the prepared statement
  76. if($stmt->execute()){
  77. // Redirect to login page
  78. header("location: login.php");
  79. } else{
  80. echo "Something went wrong. Please try again later.";
  81. }
  82. }
  83.  
  84. // Close statement
  85. unset($stmt);
  86. }
  87.  
  88. // Close connection
  89. unset($pdo);
  90. }
  91. ?><!DOCTYPE html>
  92. <html>
  93. <head>
  94. <meta charset="utf-8" />
  95. <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
  96. <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
  97. <title>Registration</title>
  98. <link rel="icon" href="img/logo3.png">
  99. <link href="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
  100. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.1/css/font-awesome.min.css">
  101. <link rel="stylesheet" href="css/style.css" />
  102. </head>
  103. <body>
  104. <?php include("inc/nav.php"); ?>
  105. <div class="container">
  106. <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
  107. <div class="form-group <?php echo (!empty($username_err)) ? 'has-error' : ''; ?>">
  108. <div class="row">
  109. <div class="col-md-3"></div>
  110. <div class="col-md-6">
  111. <h2>Register New User</h2>
  112. <hr>
  113. </div>
  114. </div>
  115. <div class="row">
  116. <div class="col-md-3 field-label-responsive">
  117. <label for="name">Username</label>
  118. </div>
  119. <input type="text" name="username"class="form-control" value="<?php echo $username; ?>">
  120. <span class="help-block"><?php echo $username_err; ?></span>
  121. </div>
  122. <div class="row">
  123. <div class="col-md-3 field-label-responsive">
  124. <label for="email">Password</label>
  125. </div>
  126. <input type="password" name="password" class="form-control" value="<?php echo $password; ?>">
  127. <span class="help-block"><?php echo $password_err; ?></span>
  128. </div>
  129. <div class="row">
  130. <div class="col-md-3 field-label-responsive">
  131. <label for="password">Confirm Password</label>
  132. </div>
  133. <input type="password" name="confirm_password" class="form-control" value="<?php echo $confirm_password; ?>">
  134. <span class="help-block"><?php echo $confirm_password_err; ?></span>
  135. </div>
  136. <div class="row">
  137. <div class="col-md-3"></div>
  138. <div class="col-md-6">
  139. <button type="submit" name="submit" value="Register" class="btn btn-dark"><i class="fa fa-user-plus"></i> Register</button>
  140. </div>
  141. </div>
  142. </form>
  143. </div>
  144. <?php include("footer/footerhome.php"); ?>
  145. </body>
  146. </html>
  147. <script src="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
  148. <script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement