Advertisement
Guest User

aaaa

a guest
Oct 24th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.73 KB | None | 0 0
  1. <?php
  2.  
  3. require('testdb.php');
  4.  
  5. session_start();
  6.  
  7.  
  8.  
  9. if (isset($_POST['registersubm'])) {
  10.  
  11.     $fName = !empty($_POST['fName']) ? trim($_POST['fName']) : null;
  12.     $username = !empty($_POST['username']) ? trim($_POST['username']) : null;
  13.     $pass = !empty($_POST['password']) ? trim($_POST['password']) : null;
  14.     $passcon = !empty($_POST['passwordcon']) ? trim($_POST['passwordcon']) : null;
  15.     $email = !empty($_POST['email']) ? trim($_POST['email']) : null;
  16.    // $avatar = $_POST['avatar'];
  17.  
  18.     $sql = "SELECT COUNT(username) AS num FROM registered_users WHERE username = :username";
  19.     $stmt = $pdo->prepare($sql);
  20.  
  21.     $stmt->bindValue(':username', $username);
  22.     $stmt->execute();
  23.  
  24.     $row = $stmt->fetch(PDO::FETCH_ASSOC);
  25.     if($row['num'] > 0){
  26.            //die('That username already exists!');
  27.         $message = "Username already taken, Please choose another one";
  28.         echo "<script type='text/javascript'>alert('$message');</script>";
  29.  
  30.     }
  31.  
  32.     else {
  33.         $sql = "SELECT COUNT(email) AS num FROM registered_users WHERE email = :email";
  34.         $stmt = $pdo->prepare($sql);
  35.  
  36.         $stmt->bindValue(':email', $email);
  37.         $stmt->execute();
  38.  
  39.         $row = $stmt->fetch(PDO::FETCH_ASSOC);
  40.         if($row['num'] > 0){
  41.  
  42.             $message = "That email is already registered to an account. Please use another one";
  43.             echo "<script type='text/javascript'>alert('$message');</script>";
  44.         }
  45.  
  46.         else {
  47.  
  48.             if ($_POST['password']!= $_POST['passwordcon']) {
  49.                 $message = "The passwords do not match";
  50.                 echo "<script type='text/javascript'>alert('$message');</script>";
  51.  
  52.             }
  53.  
  54.             else {
  55.                 $passwordHash = password_hash($pass, PASSWORD_BCRYPT, array("cost" => 12));
  56.  
  57.                 $sql = "INSERT INTO registered_users (`fName`, `username`, `password`, `email`) VALUES (:fName, :username, :password, :email)";
  58.                 $stmt = $pdo->prepare($sql);
  59.  
  60.                 $stmt->bindValue(':fName', $fName);
  61.                 $stmt->bindValue(':username', $username);
  62.                 $stmt->bindValue(':password', $passwordHash);
  63.                 $stmt->bindValue(':email', $email);
  64.                // $stmt->bindValue(':avatar', $avatar);
  65.                 $result = $stmt->execute();
  66.  
  67.  
  68.  
  69.  
  70.                 $target_path = "user_avatars/" . $username;
  71.                 $target_path = $target_path.basename( $_FILES['avatar']['name']);
  72.  
  73.                 if(move_uploaded_file($_FILES['avatar']['tmp_name'], $target_path)) {
  74.                     echo "File uploaded successfully!";
  75.                 } else{
  76.                     echo "Sorry, file not uploaded, please try again!";
  77.                 }
  78.  
  79.  
  80.  
  81.  
  82.                 //header("location: login.php?registered=true");
  83.  
  84.  
  85.             }
  86.  
  87.         }
  88.  
  89.  
  90.  
  91.     }
  92.     }
  93.  
  94.  
  95.  
  96. ?>
  97.  
  98. <!DOCTYPE html>
  99. <html>
  100. <head>
  101.     <meta charset="utf-8">
  102.     <title>Registration</title>
  103.     <link rel="stylesheet" href="css/style.css" />
  104. </head>
  105.  
  106. <body>
  107. <div class="form">
  108.     <h1>User Registration Form</h1>
  109.     <form name="registration" action="" method="post" enctype="multipart/form-data">
  110.  
  111.         <div class="formdes">
  112.             <label for="fName"><b>Full name:</b></label>
  113.             <input type="text" name="fName" placeholder="Enter Full name here" autocomplete="off" required
  114.                    Value = "<?php if (isset($fName)) {echo $fName;}?>" />
  115.         </div>
  116.         <div class="formdes">
  117.             <label for="username"><b>Username:</b></label>
  118.             <input type="text" name="username" placeholder="Enter Username here" autocomplete="off" required
  119.                    Value = "<?php if (isset($username)) {echo $username;}?>" />
  120.         </div>
  121.         <div class="formdes">
  122.             <label for="email"><b>Email:</b></label>
  123.             <input type="email" name="email" placeholder="Enter Email here" autocomplete="off" required
  124.                    Value = "<?php if (isset($email)) {echo $email;}?>"/>
  125.         </div>
  126.         <div class="formdes">
  127.             <label for="password"><b>Password:</b></label>
  128.             <input type="password" name="password" placeholder="Enter Password here" required />
  129.         </div>
  130.         <div class="formdes">
  131.             <label for="passwordcon"><b>Confirm password:</b></label>
  132.             <input type="password" name="passwordcon" placeholder="Repeat Password" required />
  133.         </div>
  134.  
  135.  
  136.         <div class="formdes">
  137.             <label for="avatar"><b>Choose Avatar:</b></label>
  138.             /<input type="file" name="avatar" accept="image/*" required />
  139.         </div>
  140.         <input type="submit" name="registersubm" value="Register" class="btnregistersubm" />
  141.  
  142.     </form>
  143. </div>
  144. </body>
  145. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement