Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Include config file
- require_once 'config.php';
- // Define variables and initialize with empty values
- $username = $password = $confirm_password = "";
- $username_err = $password_err = $confirm_password_err = "";
- // Processing form data when form is submitted
- if($_SERVER["REQUEST_METHOD"] == "POST"){
- // Validate username
- if(empty(trim($_POST["username"]))){
- $username_err = "Wpisz nazwę użytkownika";
- } else{
- // Prepare a select statement
- $sql = "SELECT id FROM users WHERE username = ?";
- if($stmt = mysqli_prepare($link, $sql)){
- // Bind variables to the prepared statement as parameters
- mysqli_stmt_bind_param($stmt, "s", $param_username);
- // Set parameters
- $param_username = trim($_POST["username"]);
- // Attempt to execute the prepared statement
- if(mysqli_stmt_execute($stmt)){
- /* store result */
- mysqli_stmt_store_result($stmt);
- if(mysqli_stmt_num_rows($stmt) == 1){
- $username_err = "Ta nazwa użytkownika jest już zajęta.";
- } else{
- $username = trim($_POST["username"]);
- }
- } else{
- echo "Ups! Coś poszło nie tak. Spróbuj ponownie później.";
- }
- }
- // Close statement
- mysqli_stmt_close($stmt);
- }
- // Validate password
- if(empty(trim($_POST['password']))){
- $password_err = "Proszę wprowadzić hasło.";
- } elseif(strlen(trim($_POST['password'])) < 6){
- $password_err = "Hasło musi mieć co najmniej 6 znaków.";
- } else{
- $password = trim($_POST['password']);
- }
- // Validate confirm password
- if(empty(trim($_POST["confirm_password"]))){
- $confirm_password_err = 'Potwierdź hasło.';
- } else{
- $confirm_password = trim($_POST['confirm_password']);
- if($password != $confirm_password){
- $confirm_password_err = 'Hasło nie pasuje.';
- }
- }
- // Check input errors before inserting in database
- if(empty($username_err) && empty($password_err) && empty($confirm_password_err)){
- // Prepare an insert statement
- $sql = "INSERT INTO users (username, password) VALUES (?, ?)";
- if($stmt = mysqli_prepare($link, $sql)){
- // Bind variables to the prepared statement as parameters
- mysqli_stmt_bind_param($stmt, "ss", $param_username, $param_password);
- // Set parameters
- $param_username = $username;
- $param_password = password_hash($password, PASSWORD_DEFAULT); // Creates a password hash
- // Attempt to execute the prepared statement
- if(mysqli_stmt_execute($stmt)){
- // Redirect to login page
- header("location: login.php");
- } else{
- echo "Coś poszło nie tak. Spróbuj ponownie później.";
- }
- }
- // Close statement
- mysqli_stmt_close($stmt);
- }
- // Close connection
- mysqli_close($link);
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement