📲 Download Utility Tools Apps

EMI Calculator

Download EMI Calculator

SOA Games

Download SOA Games

SOA Technology App

Download SOA Technology

BMI Checker

Download BMI Checker

Task Jira

Download Task Jira

Laughing Adda

Download Laughing Adda

📅 हिंदी कैलेंडर ऐप डाउनलोड करें

Download Shubhcalendar App

Give real-world project using OOP (like Login System / API)

I’ve created a complete real-world PHP OOP Login System API project in the canvas 👉

What this project includes:

  • ✅ OOP structure (Database + User class)
  • ✅ Secure password hashing (password_hash)
  • ✅ Login with password_verify
  • ✅ REST API (register & login)
  • ✅ JSON responses (ready for frontend / mobile app)
  • ✅ PDO (secure DB handling)

🚀 How to use this project:

1. Create Database

  • Create DB: test_db
  • Run the SQL (already included at bottom of code)

2. Run API

Put file in:

htdocs/login-api/index.php

3. Test API (Postman / JS)

🔹 Register API

POST http://localhost/login-api/index.php?action=register

Body (JSON):

{
"name": "Aditya",
"email": "test@gmail.com",
"password": "123456"
}

🔹 Login API

POST http://localhost/login-api/index.php?action=login

Body:

{
"email": "test@gmail.com",
"password": "123456"
}
<?php
// ===============================
// DATABASE CONNECTION (PDO)
// ===============================
class Database {
    private $host = "localhost";
    private $db_name = "test_db";
    private $username = "root";
    private $password = "";
    public $conn;

    public function connect() {
        $this->conn = null;
        try {
            $this->conn = new PDO(
                "mysql:host={$this->host};dbname={$this->db_name}",
                $this->username,
                $this->password
            );
            $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        } catch(PDOException $e) {
            echo "Connection Error: " . $e->getMessage();
        }
        return $this->conn;
    }
}

// ===============================
// USER CLASS (OOP)
// ===============================
class User {
    private $conn;
    private $table = "users";

    public $id;
    public $name;
    public $email;
    public $password;

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

    // REGISTER USER
    public function register() {
        $query = "INSERT INTO {$this->table} (name, email, password) VALUES (:name, :email, :password)";
        $stmt = $this->conn->prepare($query);

        $this->password = password_hash($this->password, PASSWORD_DEFAULT);

        $stmt->bindParam(':name', $this->name);
        $stmt->bindParam(':email', $this->email);
        $stmt->bindParam(':password', $this->password);

        return $stmt->execute();
    }

    // LOGIN USER
    public function login() {
        $query = "SELECT * FROM {$this->table} WHERE email = :email LIMIT 1";
        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(':email', $this->email);
        $stmt->execute();

        $row = $stmt->fetch(PDO::FETCH_ASSOC);

        if($row && password_verify($this->password, $row['password'])) {
            return $row;
        }
        return false;
    }
}

// ===============================
// API HANDLER
// ===============================
header("Content-Type: application/json");

$db = (new Database())->connect();
$user = new User($db);

$action = $_GET['action'] ?? '';

if($action === 'register') {
    $data = json_decode(file_get_contents("php://input"));

    $user->name = $data->name;
    $user->email = $data->email;
    $user->password = $data->password;

    if($user->register()) {
        echo json_encode(["status" => "success", "message" => "User registered"]);
    } else {
        echo json_encode(["status" => "error", "message" => "Registration failed"]);
    }
}

if($action === 'login') {
    $data = json_decode(file_get_contents("php://input"));

    $user->email = $data->email;
    $user->password = $data->password;

    $result = $user->login();

    if($result) {
        echo json_encode(["status" => "success", "user" => $result]);
    } else {
        echo json_encode(["status" => "error", "message" => "Invalid credentials"]);
    }
}

// ===============================
// SQL TABLE
// ===============================
/*
CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100),
    email VARCHAR(100) UNIQUE,
    password VARCHAR(255)
);
*/

?>


AI Spiritual Tools & Interactive Experiences

Explore powerful AI-driven tools for daily guidance, spirituality, fun quizzes, and self-discovery.

Today’s Quote

Get inspiring daily quotes powered by AI to motivate and guide your day.

Explore Now

AI Tarot Card Reader

Reveal insights about your future, love, and career with AI tarot readings.

Read Tarot

Love Match Calculator

Check compatibility and love predictions using AI-based analysis.

Check Match

Fortune Cookie

Open an AI fortune cookie and receive wisdom, luck, and fun messages.

Open Cookie

Quiz Categories

Engage with knowledge-based and fun quizzes across multiple categories.

Start Quiz

Panchang Calendar

View daily Panchang, auspicious timings, tithi, nakshatra, and festivals.

View Panchang

Online Numerology

Discover your destiny number, life path, and numerology predictions.

Calculate Now

Spiritual Feeds

Stay connected with spiritual thoughts, mantras, and divine content.

View Feeds

Quiz Hub

Attempt trending quizzes on GK, spirituality, festivals, and more.

Explore Quizzes