Advertisement
Guest User

Untitled

a guest
Jun 18th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. <?php
  2. // Include config file
  3. require_once 'config.php';
  4.  
  5. // Define variables and initialize with empty values
  6. $username = $password = $confirm_password = "";
  7. $username_err = $password_err = $confirm_password_err = "";
  8.  
  9. // Processing form data when form is submitted
  10. if($_SERVER["REQUEST_METHOD"] == "POST"){
  11.  
  12. // Validate username
  13. if(empty(trim($_POST["username"]))){
  14. $username_err = "Wpisz nazwę użytkownika";
  15. } else{
  16. // Prepare a select statement
  17. $sql = "SELECT id FROM users WHERE username = ?";
  18.  
  19. if($stmt = mysqli_prepare($link, $sql)){
  20. // Bind variables to the prepared statement as parameters
  21. mysqli_stmt_bind_param($stmt, "s", $param_username);
  22.  
  23. // Set parameters
  24. $param_username = trim($_POST["username"]);
  25.  
  26. // Attempt to execute the prepared statement
  27. if(mysqli_stmt_execute($stmt)){
  28. /* store result */
  29. mysqli_stmt_store_result($stmt);
  30.  
  31. if(mysqli_stmt_num_rows($stmt) == 1){
  32. $username_err = "Ta nazwa użytkownika jest już zajęta.";
  33. } else{
  34. $username = trim($_POST["username"]);
  35. }
  36. } else{
  37. echo "Ups! Coś poszło nie tak. Spróbuj ponownie później.";
  38. }
  39. }
  40.  
  41. // Close statement
  42. mysqli_stmt_close($stmt);
  43. }
  44.  
  45. // Validate password
  46. if(empty(trim($_POST['password']))){
  47. $password_err = "Proszę wprowadzić hasło.";
  48. } elseif(strlen(trim($_POST['password'])) < 6){
  49. $password_err = "Hasło musi mieć co najmniej 6 znaków.";
  50. } else{
  51. $password = trim($_POST['password']);
  52. }
  53.  
  54. // Validate confirm password
  55. if(empty(trim($_POST["confirm_password"]))){
  56. $confirm_password_err = 'Potwierdź hasło.';
  57. } else{
  58. $confirm_password = trim($_POST['confirm_password']);
  59. if($password != $confirm_password){
  60. $confirm_password_err = 'Hasło nie pasuje.';
  61. }
  62. }
  63.  
  64. // Check input errors before inserting in database
  65. if(empty($username_err) && empty($password_err) && empty($confirm_password_err)){
  66.  
  67. // Prepare an insert statement
  68. $sql = "INSERT INTO users (username, password) VALUES (?, ?)";
  69.  
  70. if($stmt = mysqli_prepare($link, $sql)){
  71. // Bind variables to the prepared statement as parameters
  72. mysqli_stmt_bind_param($stmt, "ss", $param_username, $param_password);
  73.  
  74. // Set parameters
  75. $param_username = $username;
  76. $param_password = password_hash($password, PASSWORD_DEFAULT); // Creates a password hash
  77.  
  78. // Attempt to execute the prepared statement
  79. if(mysqli_stmt_execute($stmt)){
  80. // Redirect to login page
  81. header("location: login.php");
  82. } else{
  83. echo "Coś poszło nie tak. Spróbuj ponownie później.";
  84. }
  85. }
  86.  
  87. // Close statement
  88. mysqli_stmt_close($stmt);
  89. }
  90.  
  91. // Close connection
  92. mysqli_close($link);
  93. }
  94. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement