Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Include config file
- require_once "config.php";
- // Define variables and initialize with empty values
- $username = $password = $confirm_password = "";
- $username_err = $password_err = $confirm_password_err = "";
- // Processing form data when form is submitted
- if($_SERVER["REQUEST_METHOD"] == "POST"){
- // Validate username
- if(empty(trim($_POST["username"]))){
- $username_err = "Please enter a username.";
- } else{
- // Prepare a select statement
- $sql = "SELECT id FROM users WHERE username = ?";
- if($stmt = $mysqli->prepare($sql)){
- // Bind variables to the prepared statement as parameters
- $stmt->bind_param("s", $param_username);
- // Set parameters
- $param_username = trim($_POST["username"]);
- // Attempt to execute the prepared statement
- if($stmt->execute()){
- // store result
- $stmt->store_result();
- if($stmt->num_rows == 1){
- $username_err = "This username is already taken.";
- } else{
- $username = trim($_POST["username"]);
- }
- } else{
- echo "Oops! Something went wrong. Please try again later.";
- }
- }
- // Close statement
- $stmt->close();
- }
- // Validate password
- if(empty(trim($_POST["password"]))){
- $password_err = "Please enter a password.";
- } elseif(strlen(trim($_POST["password"])) < 6){
- $password_err = "Password must have atleast 6 characters.";
- } else{
- $password = trim($_POST["password"]);
- }
- // Validate confirm password
- if(empty(trim($_POST["confirm_password"]))){
- $confirm_password_err = "Please confirm password.";
- } else{
- $confirm_password = trim($_POST["confirm_password"]);
- if(empty($password_err) && ($password != $confirm_password)){
- $confirm_password_err = "Password did not match.";
- }
- }
- // Check input errors before inserting in database
- if(empty($username_err) && empty($password_err) && empty($confirm_password_err)){
- // Prepare an insert statement
- $sql = "INSERT INTO users (username, password) VALUES (?, ?)";
- if($stmt = $mysqli->prepare($sql)){
- // Bind variables to the prepared statement as parameters
- $stmt->bind_param("ss", $param_username, $param_password);
- // Set parameters
- $param_username = $username;
- $param_password = password_hash($password, PASSWORD_DEFAULT); // Creates a password hash
- // Attempt to execute the prepared statement
- if($stmt->execute()){
- // Redirect to login page
- header("location: login.php");
- } else{
- echo "Something went wrong. Please try again later.";
- }
- }
- // Close statement
- $stmt->close();
- }
- // Close connection
- $mysqli->close();
- }
- ?>
- <html>
- <head>
- <link rel="icon" href="https://github.com/jchernin4/HWIDS/raw/master/Infinium%20Logo.png">
- <link href="style.css" rel="stylesheet" type="text/css" />
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width">
- </head>
- <body>
- <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post" style="border:1px solid #ccc">
- <div class="container">
- <h1>Sign Up</h1>
- <p>Please fill in this form to create an account.</p>
- <hr>
- <label for="email"><b>Email</b></label>
- <input type="email" placeholder="Enter Email" name="email" required>
- <label for="username"><b>Username</b></label>
- <input type="text" placeholder="Enter Username" name="username" required>
- <label for="password"><b>Password</b></label>
- <input type="password" placeholder="Enter Password" name="password" required>
- <label for="confirm_password"><b>Confirm Password</b></label>
- <input type="password" placeholder="Confirm Password" name="confirm_password" required>
- <label>
- <input type="checkbox" checked="checked" name="remember" style="margin-bottom:15px"> Remember me
- </label>
- <p>By creating an account you agree to our <a href="#" style="color:dodgerblue">Terms & Privacy</a>.</p>
- <div class="clearfix">
- <button type="button" class="cancelbtn">Cancel</button>
- <button type="submit" class="signupbtn">Sign Up</button>
- </div>
- </div>
- </form>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement