PHP signup code with class
PHP signup code example using a class-based approach. In this example, we'll create a simple User class for user registration. This code assumes you have a database connection in place for storing user information.
<?php
// User class to handle user registration
class User {
private $db; // Database connection
// Constructor to initialize the database connection
public function __construct() {
$this->db = new mysqli("localhost", "username", "password", "database_name");
if ($this->db->connect_error) {
die("Connection failed: " . $this->db->connect_error);
}
}
// Function to register a new user
public function registerUser($username, $password, $email) {
// Hash the password
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
// Insert user data into the database
$query = "INSERT INTO users (username, password, email) VALUES (?, ?, ?)";
$stmt = $this->db->prepare($query);
$stmt->bind_param("sss", $username, $hashedPassword, $email);
if ($stmt->execute()) {
return true;
} else {
return false;
}
}
// Function to check if a username is already taken
public function isUsernameTaken($username) {
$query = "SELECT * FROM users WHERE username=?";
$stmt = $this->db->prepare($query);
$stmt->bind_param("s", $username);
$stmt->execute();
$result = $stmt->get_result();
return $result->num_rows > 0;
}
}
// Handle form submission
if (isset($_POST['submit'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];
// Create an instance of the User class
$user = new User();
// Check if the username is already taken
if ($user->isUsernameTaken($username)) {
echo "Username is already taken.";
} else {
// Register the user
if ($user->registerUser($username, $password, $email)) {
echo "Registration successful!";
} else {
echo "Registration failed.";
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Signup</title>
</head>
<body>
<h1>Signup</h1>
<form method="POST" action="">
<label for="username">Username:</label>
<input type="text" name="username" required><br>
<label for="password">Password:</label>
<input type="password" name="password" required><br>
<label for="email">Email:</label>
<input type="email" name="email" required><br>
<input type="submit" name="submit" value="Signup">
</form>
</body>
</html>
In this code:
We create a User class with a constructor that takes a database connection as an argument.
The register method takes a username, password, and email as parameters. It first checks if the username or email already exists in the database using the isUserExists method.
If the user does not exist, it hashes the password and inserts the new user into the database.
The isUserExists method checks if a user with the given username or email already exists in the database.
In the usage example at the bottom, we create an instance of the User class, connect to the database, and call the register method to register a new user.
Please make sure to replace the database connection details with your own. Additionally, consider using prepared statements to prevent SQL injection, as shown in the code above.
Comments
Post a Comment