Guest User

Untitled

a guest
Oct 23rd, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. ?php
  2.  
  3. //This function will display the registration form
  4. function register_form(){
  5.  
  6. $date = date('D, M, Y');
  7. echo "<form action='?act=register' method='post'>"
  8. ."Username: <input type='text' name='username' size='30'><br>"
  9. ."Password: <input type='password' name='password' size='30'><br>"
  10. ."Confirm your password: <input type='password' name='password_conf' size='30'><br>"
  11. ."Email: <input type='text' name='email' size='30'><br>"
  12. ."<input type='hidden' name='date' value='$date'>"
  13. ."<input type='submit' value='Register'>"
  14. ."</form>";
  15.  
  16. }
  17.  
  18. //This function will register users data
  19. function register(){
  20.  
  21. //Connecting to database
  22. $connect = mysql_connect("host", "username", "password");
  23. if(!$connect){
  24. die(mysql_error());
  25. }
  26.  
  27. //Selecting database
  28. $select_db = mysql_select_db("database", $connect);
  29. if(!$select_db){
  30. die(mysql_error());
  31. }
  32.  
  33. //Collecting info
  34. $username = $_REQUEST['username'];
  35. $password = $_REQUEST['password'];
  36. $pass_conf = $_REQUEST['password_conf'];
  37. $email = $_REQUEST['email'];
  38. $date = $_REQUEST['date'];
  39.  
  40. //Here we will check do we have all inputs filled
  41.  
  42. if(empty($username)){
  43. die("Please enter your username!<br>");
  44. }
  45.  
  46. if(empty($password)){
  47. die("Please enter your password!<br>");
  48. }
  49.  
  50. if(empty($pass_conf)){
  51. die("Please confirm your password!<br>");
  52. }
  53.  
  54. if(empty($email)){
  55. die("Please enter your email!");
  56. }
  57.  
  58. //Let's check if this username is already in use
  59.  
  60. $user_check = mysql_query("SELECT username FROM users WHERE username='$username'");
  61. $do_user_check = mysql_num_rows($user_check);
  62.  
  63. //Now if email is already in use
  64.  
  65. $email_check = mysql_query("SELECT email FROM users WHERE email='$email'");
  66. $do_email_check = mysql_num_rows($email_check);
  67.  
  68. //Now display errors
  69.  
  70. if($do_user_check > 0){
  71. die("Username is already in use!<br>");
  72. }
  73.  
  74. if($do_email_check > 0){
  75. die("Email is already in use!");
  76. }
  77.  
  78. //Now let's check does passwords match
  79.  
  80. if($password != $pass_conf){
  81. die("Passwords don't match!");
  82. }
  83.  
  84.  
  85. //If everything is okay let's register this user
  86.  
  87. $insert = mysql_query("INSERT INTO users (username, password, email) VALUES ('$username', '$password', '$email')");
  88. if(!$insert){
  89. die("There's little problem: ".mysql_error());
  90. }
  91.  
  92. echo $username.", you are now registered. Thank you!<br><a href=login.php>Login</a> | <a href=index.php>Index</a>";
  93.  
  94. }
  95.  
  96. switch($act){
  97.  
  98. default;
  99. register_form();
  100. break;
  101.  
  102. case "register";
  103. register();
  104. break;
  105.  
  106. }
  107.  
  108. ?>
Add Comment
Please, Sign In to add comment