Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- include($_SERVER['DOCUMENT_ROOT'] . '/db.php');
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- <title>Login or SignUp</title>
- </head>
- <body>
- <h2>Signup</h2>
- <form action="" method="POST">
- Email:
- <input type="text" name="email"><br>
- Username:
- <input type="text" name="username"><br>
- Name:
- <input type="text" name="name"><br>
- Password:
- <input type="password" name="password"><br>
- Confirm Password:
- <input type="password" name="confirm_password"><br>
- <input type="submit" name="user_register" value="Register">
- </form>
- <?php
- if(isset($_POST['user_register'])){
- // Validate username
- if(empty(trim($_POST['username']))){
- $username_err = "Please Enter a Username.";
- } else {
- $sql = "SELECT user_id FROM user WHERE username = :username";
- if($statement = $connect->prepare($sql)){
- $statement->bindParam(':username', $username);
- $param_username = trim($_POST['username']);
- if($statement->execute()){
- if($statement->rowCount() == 1){
- $username_err = "This username is already taken.";
- } else {
- $username = trim($_POST['username']);
- }
- }
- unset($statement);
- }
- }
- // 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($password != $confirm_password){
- $confirm_password_err = "Password did not match.";
- }
- }
- // Validate email
- if(empty($_POST['email'])){
- $email_err = "Email is Required.";
- } else {
- $email = $_POST['email'];
- if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
- $email_err = "Invalid email format.";
- }
- else {
- $email = filter_var($email, FILTER_VALIDATE_EMAIL);
- }
- }
- // Check input errors before inserting in database
- if(empty($username_err) && empty($password_err) && empty($confirm_password_err) && empty($email_err)){
- $sql = "INSERT INTO user (username, user_name, user_password, user_email) VALUES (:username, :user_name, :password, :email)";
- if($statement = $connect->prepare($sql)){
- $statement->bindParam(':username', $param_username);
- $statement->bindParam(':password', $param_password);
- $statement->bindParam(':user_name', $param_user_name);
- $statement->bindParam(':email', $param_user_email);
- $param_username = $username;
- $param_password = password_hash($password, PASSWORD_DEFAULT);
- $param_user_email = $email;
- $param_user_name = $_POST['name'];
- if($statement->execute()){
- header("location: login.php");
- } else {
- echo "Something went wrong. Please try again later.";
- }
- }
- unset($statement);
- }
- unset($connect);
- }
- ?>
- </body>
- </html>
Add Comment
Please, Sign In to add comment