Advertisement
blogsport06

register.php (Check username taken)

Feb 13th, 2016
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.60 KB | None | 0 0
  1. if (isset($_POST['register'])) {
  2.     if (empty($_POST['username']) || empty($_POST['password']) || empty($_POST['repassword'])) {
  3.         $error = "Please fill in all the information";
  4.     }
  5.    
  6.     else {
  7.         $query = mysql_query("SELECT username FROM login WHERE username='$username'");
  8.         $num = mysql_num_rows($query);
  9.                
  10.         if ($num > 0) {
  11.             $error = "Username already taken";
  12.         }
  13.        
  14.         else {
  15.             // Define $username and $password
  16.             $username=$_POST['username'];
  17.             $password=$_POST['password'];
  18.             $repassword=$_POST['repassword'];
  19.                
  20.             // Create connection
  21.             $conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
  22.  
  23.             // Check connection
  24.             if ($conn->connect_error) {
  25.                 die("Connection failed: " . $conn->connect_error);
  26.             }
  27.            
  28.             // Checking Username Length
  29.             if (strlen($username) < 4) {
  30.                 $error = "Short username are easy to guess. Try one with at least 4 characters.";
  31.             }
  32.            
  33.             // Checking Password Length
  34.             elseif (strlen($password) < 6) {
  35.                 $error =  "Short passwords are easy to guess. Try one with at least 12 characters.";
  36.             }
  37.        
  38.             else {
  39.                 // Checking Re-enter Password
  40.                 if ($password != $repassword) {
  41.                     $error = "These passwords don't match. Try again?";
  42.                 }
  43.                
  44.                 else {
  45.                     // Encryption Password
  46.                     $enpassword = md5($password);
  47.                            
  48.                     $sql = "INSERT INTO login (username, password) VALUES ('$username', '$enpassword')";
  49.  
  50.                     if ($conn->query($sql) === TRUE) {
  51.                         header("location: index.php");
  52.                     }
  53.                     else {
  54.                         echo "Error: " . $sql . "<br>" . $conn->error;
  55.                     }
  56.                            
  57.                     $conn->close();
  58.                 }
  59.             }
  60.         }
  61.        
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement