Advertisement
Guest User

Untitled

a guest
Jun 4th, 2017
554
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.09 KB | None | 0 0
  1. <?php
  2. //Database connect.
  3. $user_db = ""; //Enter SQL username
  4. $password_db = ""; //Enter SQL password
  5. $database = ""; //Enter SQL database
  6. mysql_connect("localhost", $user_db, $password_db);
  7. mysql_select_db($database) or die ("Unable to select database");
  8.  
  9. //Echo the register form.
  10. echo '
  11.     <h2>Register an account here!</h2>
  12.     <form method="post">
  13.         Username: <br />
  14.         <input type="text" name="username" value="' . (isset($_POST['username']) ? $_POST['username'] : null) . '" maxlength="32" /><br />
  15.         Password: <br />
  16.         <input type="password" name="password" maxlength="32" /> <input type="password" name="password2" maxlength="32" /><br />
  17.         Email: <br />
  18.         <input type="text" name="email" value="' . (isset($_POST['email']) ? $_POST['email'] : null) . '" maxlength="64" /><br />
  19.         <input type="submit" name="submit" value="Register!" />
  20.     <form>
  21. ';
  22.  
  23. if(isset($_POST['submit'])){
  24.     //Basic registering variables.
  25.     $errors = array();
  26.     $username = isset($_POST['username']) ? $_POST['username'] : null;
  27.     $password = isset($_POST['password']) ? $_POST['password'] : null;
  28.     $password2 = isset($_POST['password2']) ? $_POST['password2'] : null;
  29.     $email = isset($_POST['email']) ? $_POST['email'] : null;
  30.     $user_check = mysql_num_rows(mysql_query("SELECT 1 FROM accounts WHERE login = '" . mysql_real_escape_string($username) . "'"));
  31.     $check_email = mysql_num_rows(mysql_query("SELECT 1 FROM accounts WHERE email = '" . mysql_real_escape_string($email) . "'"));
  32.    
  33.     //Error checking.
  34.     if(strlen($username) > 32){
  35.         $errors[] = 'Username can not be over 32 characters.';
  36.     }
  37.     if(strlen($password) > 32){
  38.         $errors[] = 'Username can not be over 32 characters.';
  39.     }
  40.     if(strlen($username) < 4){
  41.         $errors[] = 'Username must be 4 characters long or more.';
  42.     }
  43.     if(strlen($password) < 4){
  44.         $errors[] = 'Password must be 4 characters long or more.';
  45.     }
  46.     if($password != $password2){
  47.         $errors[] = 'Passwords entered do not match.';
  48.     }
  49.     if(!preg_match("/^\b[A-Z0-9._-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}$\b/i", $email)){
  50.         $errors[] = 'Your e-mail follows an invalid format. Please use example@example.com';
  51.     }
  52.     if (!preg_match("/^[a-zA-Z0-9_-\s]+?$/i", $username)){
  53.         $errors[] = 'Your display name may only contain Letters, numbers, spaces, underscores, and dashes.';
  54.     }
  55.     if (!preg_match("/^[a-zA-Z0-9_-\s]+?$/i", $password)){
  56.         $errors[] = 'Your password may only contain Letters, numbers, spaces, underscores, and dashes.';
  57.     }
  58.     if($user_check > 0){
  59.         $errors [] = 'The username ' . $username . ' is already in use.';
  60.     }
  61.     if($check_email > 0){
  62.         $errors [] = 'The email ' . $email . ' is already in use.';
  63.     }
  64.    
  65.     if(count($errors) == 0){
  66.         //User has registered successfully!
  67.         echo '<br /><br /><b>Register complete!</b>';
  68.         mysql_query("INSERT INTO accounts (login, password, gm, email)
  69.         VALUES ('" . $username . "', '" . $password . "', 'a', '" . $email . "')");
  70.     } else {
  71.         //User has entered something incorrectly!
  72.         echo '<br /><br /><b>Please correct the following ' . count($errors) . ' errors:</b><br />';
  73.         for($i = 0; $i < count($errors); $i++){
  74.             echo $errors[$i] . '<br />';
  75.         }
  76.     }
  77. }
  78. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement