Advertisement
Guest User

Untitled

a guest
Mar 8th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.34 KB | None | 0 0
  1. <?php
  2. // Include config file
  3. require_once 'connect.php';
  4.  
  5. // Define variables and initialize with empty values
  6. $username = $password = $confirm_password = "";
  7. $username_err = $password_err = $confirm_password_err = "";
  8.  
  9. // Processing form data when form is submitted
  10. if($_SERVER["REQUEST_METHOD"] == "POST"){
  11.  
  12.     // Validate username
  13.     if(empty(trim($_POST["username"]))){
  14.         $username_err = "Vul een gebruikersnaam in.";
  15.     } else{
  16.         // Prepare a select statement
  17.         $sql = "SELECT id FROM users WHERE username = ?";
  18.        
  19.         if($stmt = mysqli_prepare($link, $sql)){
  20.             // Bind variables to the prepared statement as parameters
  21.             mysqli_stmt_bind_param($stmt, "s", $param_username);
  22.            
  23.             // Set parameters
  24.             $param_username = trim($_POST["username"]);
  25.            
  26.             // Attempt to execute the prepared statement
  27.             if(mysqli_stmt_execute($stmt)){
  28.                 /* store result */
  29.                 mysqli_stmt_store_result($stmt);
  30.                
  31.                 if(mysqli_stmt_num_rows($stmt) == 1){
  32.                     $username_err = "Dit gebruikersnaam is al in gebruik.";
  33.                 } else{
  34.                     $username = trim($_POST["username"]);
  35.                 }
  36.             } else{
  37.                 echo "Oops! Something went wrong. Please try again later.";
  38.             }
  39.         }
  40.          
  41.         // Close statement
  42.         mysqli_stmt_close($stmt);
  43.     }
  44.    
  45.     // Validate password
  46.     if(empty(trim($_POST['password']))){
  47.         $password_err = "Vul een wachtwoord in.";    
  48.     } elseif(strlen(trim($_POST['password'])) < 6){
  49.         $password_err = "Wachtwoord moet minstens 6 letters bevatten.";
  50.     } else{
  51.         $password = trim($_POST['password']);
  52.     }
  53.    
  54.     // Validate confirm password
  55.     if(empty(trim($_POST["confirm_password"]))){
  56.         $confirm_password_err = 'Vul wachtwoord nogmaals in.';    
  57.     } else{
  58.         $confirm_password = trim($_POST['confirm_password']);
  59.         if($password != $confirm_password){
  60.             $confirm_password_err = 'Wachtwoorden komt niet overeen.';
  61.         }
  62.     }
  63.    
  64.     // Check input errors before inserting in database
  65.     if(empty($username_err) && empty($password_err) && empty($confirm_password_err)){
  66.        
  67.         // Prepare an insert statement
  68.         $sql = "INSERT INTO users (username, password) VALUES (?, ?)";
  69.          
  70.         if($stmt = mysqli_prepare($link, $sql)){
  71.             // Bind variables to the prepared statement as parameters
  72.             mysqli_stmt_bind_param($stmt, "ss", $param_username, $param_password);
  73.            
  74.             // Set parameters
  75.             $param_username = $username;
  76.             $param_password = password_hash($password, PASSWORD_DEFAULT); // Creates a password hash
  77.            
  78.             // Attempt to execute the prepared statement
  79.             if(mysqli_stmt_execute($stmt)){
  80.                 // Redirect to login page
  81.                 header("location: login.php");
  82.             } else{
  83.                 echo "Something went wrong. Please try again later.";
  84.             }
  85.         }
  86.          
  87.         // Close statement
  88.         mysqli_stmt_close($stmt);
  89.     }
  90.    
  91.     // Close connection
  92.     mysqli_close($link);
  93. }
  94. ?>
  95.  
  96. <!DOCTYPE html>
  97. <html lang="en">
  98. <head>
  99.     <meta charset="UTF-8">
  100.     <title>Sign Up</title>
  101.     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
  102.     <style type="text/css">
  103.         html, body { height: 100%;}
  104.         html {display: table; margin: auto; background-image: url("background.jpg")}
  105.         body{ font: 14px sans-serif; }
  106.         .wrapper{ width: 350px; padding: 20px; }
  107.     </style>
  108. </head>
  109. <body>
  110.     <div class="wrapper">
  111.         <h2>Registreer Nu</h2>
  112.         <p>Vul dit in om een account aan te maken.</p>
  113.         <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
  114.             <div class="form-group <?php echo (!empty($username_err)) ? 'has-error' : ''; ?>">
  115.                 <label>Gebruikersnaam</label>
  116.                 <input type="text" name="username"class="form-control" value="<?php echo $username; ?>">
  117.                 <span class="help-block"><?php echo $username_err; ?></span>
  118.             </div>    
  119.             <div class="form-group <?php echo (!empty($password_err)) ? 'has-error' : ''; ?>">
  120.                 <label>Password</label>
  121.                 <input type="password" name="password" class="form-control" value="<?php echo $password; ?>">
  122.                 <span class="help-block"><?php echo $password_err; ?></span>
  123.             </div>
  124.             <div class="form-group <?php echo (!empty($confirm_password_err)) ? 'has-error' : ''; ?>">
  125.                 <label>Confirm Password</label>
  126.                 <input type="password" name="confirm_password" class="form-control" value="<?php echo $confirm_password; ?>">
  127.                 <span class="help-block"><?php echo $confirm_password_err; ?></span>
  128.             </div>
  129.             <div class="form-group">
  130.                 <input type="submit" class="btn btn-primary" value="Submit">
  131.                 <input type="reset" class="btn btn-default" value="Reset">
  132.             </div>
  133.             <p>Heeft u al een account? <a href="login.php">Log hier in</a>.</p>
  134.         </form>
  135.     </div>    
  136. </body>
  137. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement