In this tutorial, you’ll create a user registration form that consists of the following some input fields. Registration form is a list of fields in which a user will input data and submit it. It is useful in every situation where a registration is necessary. For Example: Various companies use registration forms to sign up customers for services, or other programs. Here’s an example PHP script for user registration with input validation and secure processing:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
<?php // Start session session_start(); // Define variables and set to empty values $nameErr = $emailErr = $passwordErr = ""; $name = $email = $password = ""; // Function to sanitize input data function sanitize_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } // If form is submitted if ($_SERVER["REQUEST_METHOD"] == "POST") { // Validate name input if (empty($_POST["name"])) { $nameErr = "Name is required"; } else { $name = sanitize_input($_POST["name"]); // Check if name contains only letters and whitespace if (!preg_match("/^[a-zA-Z ]*$/",$name)) { $nameErr = "Only letters and white space allowed"; } } // Validate email input if (empty($_POST["email"])) { $emailErr = "Email is required"; } else { $email = sanitize_input($_POST["email"]); // Check if email is valid if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { $emailErr = "Invalid email format"; } } // Validate password input if (empty($_POST["password"])) { $passwordErr = "Password is required"; } else { $password = sanitize_input($_POST["password"]); // Check if password is strong (contains at least one uppercase letter, one lowercase letter, one number, and one special character) if (!preg_match("/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/",$password)) { $passwordErr = "Password must contain at least one uppercase letter, one lowercase letter, one number, and one special character"; } } // If no errors, proceed with registration if ($nameErr == "" && $emailErr == "" && $passwordErr == "") { // Connect to database (replace placeholders with actual values) $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDB"; $conn = mysqli_connect($servername, $username, $password, $dbname); // Check connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } // Prepare and bind statement to insert data into database $stmt = $conn->prepare("INSERT INTO users (name, email, password) VALUES (?, ?, ?)"); $stmt->bind_param("sss", $name, $email, $password); // Execute statement if ($stmt->execute()) { // Registration successful, redirect to login page header("Location: login.php"); exit(); } else { // Registration failed, display error message echo "Error: " . $stmt->error; } // Close statement and connection $stmt->close(); mysqli_close($conn); } } ?> <!-- Display registration form with error messages (if any) --> <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> <label for="name">Name:</label> <input type="text" id="name" name="name" value="<?php echo $name;?>"> <span class="error"><?php echo $nameErr;?></span> <br><br> |