Guest User

Untitled

a guest
May 23rd, 2019
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 6.64 KB | None | 0 0
  1. <meta charset="UTF-8">
  2. <?php
  3. // Include config file
  4. require_once "config.php";
  5.  
  6. // Define variables and initialize with empty values
  7. $email = $fnavn = $enavn = $password = $confirm_password =$Chipnummer = $hunden = $DHLnummer = $avl = "";
  8. $email_err = $fnavn_err = $enavn_err = $password_err = $confirm_password_err =$Chipnummer_err = $hunden_err = $DHLnummer_err = $avl_err = "";
  9.  
  10. // Processing form data when form is submitted
  11. if($_SERVER["REQUEST_METHOD"] == "POST"){
  12.  
  13.    // Validate email
  14.    if(empty(trim($_POST["email"]))){
  15.        $email_err = "Please enter a email.";
  16.    } else{
  17.        // Prepare a select statement
  18.        $sql = "SELECT id FROM users WHERE email = ?";
  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_email);
  23.            
  24.            // Set parameters
  25.            $param_email = trim($_POST["email"]);
  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.                    $email_err = "This email is already taken.";
  34.                } else{
  35.                    $email = trim($_POST["email"]);
  36.                }
  37.            } else{
  38.                echo "Oops! Something went wrong. Please try again later.";
  39.            }
  40.        }
  41.        
  42.        // Close statement
  43.        mysqli_stmt_close($stmt);
  44.    }
  45.    
  46.    // Validate password
  47.    if(empty(trim($_POST["password"]))){
  48.        $password_err = "Please enter a password.";    
  49.    } elseif(strlen(trim($_POST["password"])) < 6){
  50.        $password_err = "Password must have atleast 6 characters.";
  51.    } else{
  52.        $password = trim($_POST["password"]);
  53.    }
  54.    
  55.    // Validate confirm password
  56.    if(empty(trim($_POST["confirm_password"]))){
  57.        $confirm_password_err = "Please confirm password.";    
  58.    } else{
  59.        $confirm_password = trim($_POST["confirm_password"]);
  60.        if(empty($password_err) && ($password != $confirm_password)){
  61.            $confirm_password_err = "Password did not match.";
  62.        }
  63.    }
  64.    
  65.    // Check input errors before inserting in database
  66.    if(empty($email_err) && empty($password_err) && empty($confirm_password_err)){
  67.        
  68.        // Prepare an insert statement
  69.        $sql = "INSERT INTO users (email, fnavn, enavn, password, Chipnummer, hunden, DHLnummer, avl) VALUES (?, ?, ?, ?, ?, ?, ?, ? )";
  70.        
  71.        if($stmt = mysqli_prepare($link, $sql)){
  72.            // Bind variables to the prepared statement as parameters
  73.            mysqli_stmt_bind_param($stmt, "ssssssss", $param_email, $param_fnavn, $param_enavn, $param_password, $param_Chipnummer, $param_hunden, $param_DHLnummer, $param_avl);
  74.            
  75.            // Set parameters
  76.            $param_email = $email;
  77.             $param_fnavn = $fnavn;
  78.             $param_enavn = $enavn;
  79.             $param_Chipnummer = $Chipnummer;
  80.             $param_hunden = $hunden;
  81.             $param_DHLnummer = $DHLnummer;
  82.             $param_avl = $avl;
  83.            $param_password = password_hash($password, PASSWORD_DEFAULT); // Creates a password hash
  84.            
  85.            // Attempt to execute the prepared statement
  86.            if(mysqli_stmt_execute($stmt)){
  87.                // Redirect to login page
  88.                header("location: login.php");
  89.            } else{
  90.                echo "Something went wrong. Please try again later.";
  91.            }
  92.        }
  93.        
  94.        // Close statement
  95.        mysqli_stmt_close($stmt);
  96.    }
  97.    
  98.    // Close connection
  99.    mysqli_close($link);
  100. }
  101. ?>
  102.  
  103. <!DOCTYPE html>
  104. <html lang="en">
  105. <head>
  106.     <meta charset="UTF-8">
  107.     <title>Sign Up</title>
  108.     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
  109.     <style type="text/css">
  110.         body{ font: 14px sans-serif; }
  111.         .wrapper{ width: 350px; padding: 20px; }
  112.     </style>
  113. </head>
  114. <body>
  115.     <div class="wrapper">
  116.         <h2>Sign Up</h2>
  117.         <p>Please fill this form to create an account.</p>
  118.         <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
  119.             <div class="form-group <?php echo (!empty($email_err)) ? 'has-error' : ''; ?>">
  120.                 <label>Email</label>
  121.                 <input type="text" name="email" class="form-control" value="<?php echo $email; ?>">
  122.                 <span class="help-block"><?php echo $email_err; ?></span>
  123.             </div>
  124.             <div class="form-group <?php echo (!empty($fnavn_err)) ? 'has-error' : ''; ?>">
  125.                 <label>Fornavn</label>
  126.                 <input type="text" name="fnavn" class="form-control" value="<?php echo $fnavn; ?>">
  127.                 <span class="help-block"><?php echo $fnavn_err; ?></span>
  128.             </div>
  129.             <div class="form-group <?php echo (!empty($enavn_err)) ? 'has-error' : ''; ?>">
  130.                 <label>Eftername</label>
  131.                 <input type="text" name="enavn" class="form-control" value="<?php echo $enavn; ?>">
  132.                 <span class="help-block"><?php echo $enavn_err; ?></span>
  133.             </div>
  134.             <div class="form-group <?php echo (!empty($DHLnummer_err)) ? 'has-error' : ''; ?>">
  135.                 <label>DHLnummer</label>
  136.                 <input type="text" name="DHLnummer" class="form-control" value="<?php echo $DHLnummer; ?>">
  137.                 <span class="help-block"><?php echo $DHLnummer_err; ?></span>
  138.             </div>         
  139.             <div class="form-group <?php echo (!empty($password_err)) ? 'has-error' : ''; ?>">
  140.                 <label>Password</label>
  141.                 <input type="password" name="password" class="form-control" value="<?php echo $password; ?>">
  142.                 <span class="help-block"><?php echo $password_err; ?></span>
  143.             </div>
  144.             <div class="form-group <?php echo (!empty($confirm_password_err)) ? 'has-error' : ''; ?>">
  145.                 <label>Confirm Password</label>
  146.                 <input type="password" name="confirm_password" class="form-control" value="<?php echo $confirm_password; ?>">
  147.                 <span class="help-block"><?php echo $confirm_password_err; ?></span>
  148.             </div>
  149.             <div class="form-group">
  150.                 <input type="submit" class="btn btn-primary" value="Submit">
  151.                 <input type="reset" class="btn btn-default" value="Reset">
  152.             </div>
  153.             <p>Already have an account? <a href="login.php">Login here</a>.</p>
  154.         </form>
  155.     </div>    
  156. </body>
  157. </html>
Advertisement
Add Comment
Please, Sign In to add comment