Creating a PHP signup and login system using object-oriented programming (OOP) is a good practice for building scalable and maintainable applications. Below is a simplified example of a signup and login system using OOP principles. This code demonstrates the basic structure and concepts, but in a real-world scenario, you should implement additional security measures and validation.

User.php (User Class)

<?php
class User {
    private $db;

    public function __construct($db) {
        $this->db = $db;
    }

    public function createUser($username, $password) {
        $hashedPassword = password_hash($password, PASSWORD_DEFAULT);
        $sql = "INSERT INTO users (username, password) VALUES (?, ?)";
        $stmt = $this->db->prepare($sql);
        return $stmt->execute([$username, $hashedPassword]);
    }

    public function login($username, $password) {
        $sql = "SELECT * FROM users WHERE username = ?";
        $stmt = $this->db->prepare($sql);
        $stmt->execute([$username]);
        $user = $stmt->fetch();

        if ($user && password_verify($password, $user['password'])) {
            return true;
        } else {
            return false;
        }
    }
}
?>

index.php (Signup and Login Page)

<?php
session_start();

require_once 'User.php';

// Database connection (you should configure this)
$db = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');

$user = new User($db);

if (isset($_POST['signup'])) {
    $username = $_POST['username'];
    $password = $_POST['password'];

    if ($user->createUser($username, $password)) {
        echo "User created successfully. <a href='index.php'>Login</a>";
    } else {
        echo "Error: User creation failed.";
    }
}

if (isset($_POST['login'])) {
    $username = $_POST['username'];
    $password = $_POST['password'];

    if ($user->login($username, $password)) {
        $_SESSION['username'] = $username;
        header('Location: dashboard.php'); // Redirect to dashboard or another page
    } else {
        echo "Login failed. Please try again.";
    }
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Signup and Login</title>
</head>
<body>
    <h2>Signup</h2>
    <form method="POST">
        <input type="text" name="username" placeholder="Username" required>
        <input type="password" name="password" placeholder="Password" required>
        <button type="submit" name="signup">Signup</button>
    </form>

    <h2>Login</h2>
    <form method="POST">
        <input type="text" name="username" placeholder="Username" required>
        <input type="password" name="password" placeholder="Password" required>
        <button type="submit" name="login">Login</button>
    </form>
</body>
</html>

This code defines a User class that encapsulates user-related functions like creating a user and logging in. It uses PDO for database access and password hashing for security. The index.php file includes both the signup and login forms, and it handles form submissions and user sessions.

Remember to configure the database connection details (host, dbname, username, and password) to match your database setup. Additionally, in a real-world scenario, you would implement more security features like CSRF protection, user input validation, and password reset functionality.

By admin

Leave a Reply

Your email address will not be published. Required fields are marked *