PHP Projects With Source Code

Expense Tracker App Project In Php


Creating an Expense Tracker App involves several components, including a MySQL database schema, file structure, layout design using Bootstrap 5, and complete PHP scripts for user authentication and management. Below is a comprehensive guide to help you set up your system.

Project Introduction

The Expense Tracker is a web application designed to help users manage their finances effectively. This platform allows users to track their income sources, expenses, budgets, and savings goals in a user-friendly interface. With features for notifications and reporting, the system aims to provide users with insights into their financial habits and help them achieve their financial objectives.

Project Objectives

  • To develop a secure and intuitive platform for users to manage their personal finances.
  • To implement a comprehensive database schema that supports user management, income tracking, expense logging, and budget planning.
  • To provide functionalities for setting and tracking savings goals to encourage financial discipline.
  • To facilitate notifications for important financial reminders and updates.
  • To create a responsive design that enhances user experience across various devices.
  • To generate reports that provide insights into spending patterns and financial health.

Project Modules

  1. User Management: Handles user registration, authentication, and profile management.
  2. Income Management: Allows users to add, edit, and track their income sources, including amounts and frequencies.
  3. Expense Management: Facilitates the logging and categorization of expenses, including transaction dates and notes.
  4. Budget Management: Enables users to set budgets for different categories and track their spending against these budgets.
  5. Savings Goals Management: Allows users to set savings goals, track progress, and manage target amounts and due dates.
  6. Notification System: Sends alerts and reminders to users regarding important financial activities and deadlines.
  7. Reporting: Generates reports that provide insights into income, expenses, budgets, and savings to help users make informed financial decisions.

1. MySQL Database Schema


CREATE DATABASE expense_tracker;
USE expense_tracker;
-- Table for users
CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(50) NOT NULL UNIQUE,
    password VARCHAR(255) NOT NULL,
    email VARCHAR(100) NOT NULL UNIQUE,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Table for income sources
CREATE TABLE income_sources (
    id INT AUTO_INCREMENT PRIMARY KEY,
    user_id `INT` NOT NULL,
    source_name VARCHAR(100) NOT NULL,
    amount DECIMAL(10, 2) NOT NULL,
    frequency ENUM('one-time', 'monthly', 'weekly', 'yearly') DEFAULT 'one-time',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);
-- Table for expenses
CREATE TABLE expenses (
    id INT AUTO_INCREMENT PRIMARY KEY,
    user_id `INT` NOT NULL,
    category VARCHAR(100) NOT NULL,
    amount DECIMAL(10, 2) NOT NULL,
    transaction_date `DATE` NOT NULL,
    notes VARCHAR(255),
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);
-- Table for budgets
CREATE TABLE budgets (
    id INT AUTO_INCREMENT PRIMARY KEY,
    user_id `INT` NOT NULL,
    category VARCHAR(100) NOT NULL,
    budget_amount DECIMAL(10, 2) NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);
-- Table for savings goals
CREATE TABLE savings_goals (
    id INT AUTO_INCREMENT PRIMARY KEY,
    user_id `INT` NOT NULL,
    goal_name VARCHAR(100) NOT NULL,
    target_amount DECIMAL(10, 2) NOT NULL,
    current_amount DECIMAL(10, 2) DEFAULT 0,
    due_date `DATE` NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);
-- Table for notifications
CREATE TABLE notifications (
    id INT AUTO_INCREMENT PRIMARY KEY,
    user_id `INT` NOT NULL,
    message VARCHAR(255) NOT NULL,
    is_read TINYINT(1) DEFAULT FALSE,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);

2. File and Folder Structure


expense_tracker/
│
├── config/
│   └── db.php
│
├── public/
│   ├── index.php
│   ├── login.php
│   ├── register.php
│   ├── dashboard.php
│   ├── income.php
│   ├── expenses.php
│   ├── budgets.php
│   ├── savings_goals.php
│   ├── notifications.php
│   └── reports.php
│
├── includes/
│   ├── header.php
│   ├── footer.php
│   └── functions.php
│
├── assets/
│   ├── css/
│   │   └── styles.css
│   ├── js/
│   │   └── scripts.js
│   └── images/
│
└── vendor/
    └── (Bootstrap and other libraries)

3. Layout Based Design with Bootstrap 5

Header (includes/header.php)


<!DOCTYPE html>
<html lang=`en`>
<head>
    <meta charset=`UTF-8`>
    <meta name=`viewport` content=`width=device-width, initial-scale=1.0`>
    <link href=`https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css` rel=`stylesheet`>
    <link rel=`stylesheet` href=`assets/css/styles.css`>
    <title>Expense Tracker</title>
</head>
<body>
    <div class=`container`>
        <header class=`my-4`>
            <h1>Expense Tracker</h1>
        </header>
        <nav class=`navbar navbar-expand-lg navbar-light bg-light`>
            <div class=`container-fluid`>
                <a class=`navbar-brand` href=`index.php`>Home</a>
                <button class=`navbar-toggler` type=`button` data-bs-toggle=`collapse` data-bs-target=`#navbarNav` aria-controls=`navbarNav` aria-expanded=`false` aria-label=`Toggle navigation`>
                    <span class=`navbar-toggler-icon`></span>
                </button>
                <div class=`collapse navbar-collapse` id=`navbarNav`>
                    <ul class=`navbar-nav`>
                        <li class=`nav-item`>
                            <a class=`nav-link` href=`income.php`>Income</a>
                        </li>
                        <li class=`nav-item`>
                            <a class=`nav-link` href=`expenses.php`>Expenses</a>
                        </li>
                        <li class=`nav-item`>
                            <a class=`nav-link` href=`budgets.php`>Budgets</a>
                        </li>
                        <li class=`nav-item`>
                            <a class=`nav-link` href=`savings_goals.php`>Savings Goals</a>
                        </li>
                        <li class=`nav-item`>
                            <a class=`nav-link` href=`notifications.php`>Notifications</a>
                        </li>
                        <li class=`nav-item`>
                            <a class=`nav-link` href=`reports.php`>Reports</a>
                        </li>
                        <li class=`nav-item`>
                            <a class=`nav-link` href=`login.php`>Login</a>
                        </li>
                        <li class=`nav-item`>
                            <a class=`nav-link` href=`register.php`>Register</a>
                        </li>
                    </ul>
                </div>
            </div>
        </nav>
        <main class=`my-4`>

Footer (includes/footer.php)


        </main>
        <footer class=`text-center my-4`>
            <p>© 2023 Expense Tracker. All rights reserved.</p>
        </footer>
    </div>
    <script src=`https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js`></script>
</body>
</html>

4. PHP Code for User Authentication

Registration (public/register.php)


<?php
require '../config/db.php';
require '../includes/header.php';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $username = $_POST['username'];
    $email = $_POST['email'];
    $password = password_hash($_POST['password'], PASSWORD_DEFAULT);
    $stmt = $conn->prepare(`INSERT INTO users (username, email, password) VALUES (?, ?, ?)`);
    $stmt->bind_param(`sss`, $username, $email, $password);
    $stmt->execute();
    $stmt->close();
    header(`Location: login.php`);
}
?>
<form method=`POST` action=``>
    <div class=`mb-3`>
        <label for=`username` class=`form-label`>Username</label>
        <input type=`text` class=`form-control` id=`username` name=`username` required>
    </div>
    <div class=`mb-3`>
        <label for=`email` class=`form-label`>Email</label>
        <input type=`email` class=`form-control` id=`email` name=`email` required>
    </div>
    <div class=`mb-3`>
        <label for=`password` class=`form-label`>Password</label>
        <input type=`password` class=`form-control` id=`password` name=`password` required>
    </div>
    <button type=`submit` class=`btn btn-primary`>Register</button>
</form>
<?php require '../includes/footer.php'; ?>

Login (public/login.php)


<?php
session_start();
require '../config/db.php';
require '../includes/header.php';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $username = $_POST['username'];
    $password = $_POST['password'];
    $stmt = $conn->prepare(`SELECT * FROM users WHERE username = ?`);
    $stmt->bind_param(`s`, $username);
    $stmt->execute();
    $result = $stmt->get_result();
    $user = $result->fetch_assoc();
    if ($user && password_verify($password, $user['password'])) {
        $_SESSION['user_id'] = $user['id'];
        header(`Location: dashboard.php`);
    } else {
        echo `Invalid credentials.`;
    }
}
?>
<form method=`POST` action=``>
    <div class=`mb-3`>
        <label for=`username` class=`form-label`>Username</label>
        <input type=`text` class=`form-control` id=`username` name=`username` required>
    </div>
    <div class=`mb-3`>
        <label for=`password` class=`form-label`>Password</label>
        <input type=`password` class=`form-control` id=`password` name=`password` required>
    </div>
    <button type=`submit` class=`btn btn-primary`>Login</button>
</form>
<?php require '../includes/footer.php'; ?>

5. Additional PHP Scripts

Dashboard (public/dashboard.php)


<?php
session_start();
require '../config/db.php';
require '../includes/header.php';
if (!isset($_SESSION['user_id'])) {
    header(`Location: login.php`);
    exit();
}
$user_id = $_SESSION['user_id'];
$stmt = $conn->prepare(`SELECT * FROM users WHERE id = ?`);
$stmt->bind_param(`i`, $user_id);
$stmt->execute();
$result = $stmt->get_result();
$user = $result->fetch_assoc();
?>
<h2>Welcome, <?php echo $user['username']; ?></h2>
<p>Your email: <?php echo $user['email']; ?></p>
<?php require '../includes/footer.php'; ?>

Income Management (public/income.php)


<?php
session_start();
require '../config/db.php';
require '../includes/header.php';
if (!isset($_SESSION['user_id'])) {
    header(`Location: login.php`);
    exit();
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $source_name = $_POST['source_name'];
    $amount = $_POST['amount'];
    $frequency = $_POST['frequency'];
    $stmt = $conn->prepare(`INSERT INTO income_sources (user_id, source_name, amount, frequency) VALUES (?, ?, ?, ?)`);
    $stmt->bind_param(`isss`, $_SESSION['user_id'], $source_name, $amount, $frequency);
    $stmt->execute();
    $stmt->close();
    header(`Location: income.php`);
}
?>
<h2>Manage Income</h2>
<form method=`POST` action=``>
    <div class=`mb-3`>
        <label for=`source_name` class=`form-label`>Source Name</label>
        <input type=`text` class=`form-control` id=`source_name` name=`source_name` required>
    </div>
    <div class=`mb-3`>
        <label for=`amount` class=`form-label`>Amount</label>
        <input type=`number` class=`form-control` id=`amount` name=`amount` required>
    </div>
    <div class=`mb-3`>
        <label for=`frequency` class=`form-label`>Frequency</label>
        <select class=`form-select` id=`frequency` name=`frequency` required>
            <option value=`one-time`>One-time</option>
            <option value=`monthly`>Monthly</option>
            <option value=`weekly`>Weekly</option>
            <option value=`yearly`>Yearly</option>
        </select>
    </div>
    <button type=`submit` class=`btn btn-primary`>Add Income</button>
</form>
<h3>Existing Income Sources</h3>
<table class=`table`>
    <thead>
        <tr>
            <th>ID</th>
            <th>Source Name</th>
            <th>Amount</th>
            <th>Frequency</th>
        </tr>
    </thead>
    <tbody>
        <?php
        $stmt = $conn->prepare(`SELECT * FROM income_sources WHERE user_id = ?`);
        $stmt->bind_param(`i`, $_SESSION['user_id']);
        $stmt->execute();
        $result = $stmt->get_result();
        while ($income = $result->fetch_assoc()): ?>
        <tr>
            <td><?php echo $income['id']; ?></td>
            <td><?php echo $income['source_name']; ?></td>
            <td><?php echo $income['amount']; ?></td>
            <td><?php echo $income['frequency']; ?></td>
        </tr>
        <?php endwhile; ?>
    </tbody>
</table>
<?php require '../includes/footer.php'; ?>

Expense Management (public/expenses.php)


<?php
session_start();
require '../config/db.php';
require '../includes/header.php';
if (!isset($_SESSION['user_id'])) {
    header(`Location: login.php`);
    exit();
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $category = $_POST['category'];
    $amount = $_POST['amount'];
    $transaction_date = $_POST['transaction_date'];
    $notes = $_POST['notes'];
    $stmt = $conn->prepare(`INSERT INTO expenses (user_id, category, amount, transaction_date, notes) VALUES (?, ?, ?, ?, ?)`);
    $stmt->bind_param(`issss`, $_SESSION['user_id'], $category, $amount, $transaction_date, $notes);
    $stmt->execute();
    $stmt->close();
    header(`Location: expenses.php`);
}
?>
<h2>Manage Expenses</h2>
<form method=`POST` action=``>
    <div class=`mb-3`>
        <label for=`category` class=`form-label`>Category</label>
        <input type=`text` class=`form-control` id=`category` name=`category` required>
    </div>
    <div class=`mb-3`>
        <label for=`amount` class=`form-label`>Amount</label>
        <input type=`number` class=`form-control` id=`amount` name=`amount` required>
    </div>
    <div class=`mb-3`>
        <label for=`transaction_date` class=`form-label`>Transaction Date</label>
        <input type=`date` class=`form-control` id=`transaction_date` name=`transaction_date` required>
    </div>
    <div class=`mb-3`>
        <label for=`notes` class=`form-label`>Notes</label>
        <textarea class=`form-control` id=`notes` name=`notes`></textarea>
    </div>
    <button type=`submit` class=`btn btn-primary`>Add Expense</button>
</form>
<h3>Existing Expenses</h3>
<table class=`table`>
    <thead>
        <tr>
            <th>ID</th>
            <th>Category</th>
            <th>Amount</th>
            <th>Transaction Date</th>
            <th>Notes</th>
        </tr>
    </thead>
    <tbody>
        <?php
        $stmt = $conn->prepare(`SELECT * FROM expenses WHERE user_id = ?`);
        $stmt->bind_param(`i`, $_SESSION['user_id']);
        $stmt->execute();
        $result = $stmt->get_result();
        while ($expense = $result->fetch_assoc()): ?>
        <tr>
            <td><?php echo $expense['id']; ?></td>
            <td><?php echo $expense['category']; ?></td>
            <td><?php echo $expense['amount']; ?></td>
            <td><?php echo $expense['transaction_date']; ?></td>
            <td><?php echo $expense['notes']; ?></td>
        </tr>
        <?php endwhile; ?>
    </tbody>
</table>
<?php require '../includes/footer.php'; ?>

6. Additional Features to Consider

User Profiles: Allow users to edit their profiles and manage their settings.

Advanced Reporting: Implement more sophisticated reporting features for better insights.

Mobile Responsiveness: Ensure the application is fully responsive for mobile users.

7. Security Measures

Input Validation: Ensure all user inputs are validated to prevent SQL injection and XSS attacks.

Password Security: Use strong hashing algorithms for storing passwords.

Session Security: Implement secure session management practices to protect user sessions.

8. Testing and Deployment

Unit Testing: Conduct unit tests for individual components to ensure they function correctly.

Integration Testing: Test the integration of different modules to ensure they work together seamlessly.

Deployment: Choose a reliable hosting provider and deploy the application, ensuring all configurations are optimized for performance.

9. Documentation

User Documentation: Create a user manual to guide users through the platform's features and functionalities.

Developer Documentation: Document the codebase and architecture for future reference and maintenance.

10. Future Enhancements

AI-Powered Insights: Implement machine learning algorithms to provide insights into spending habits and financial trends.

Mobile Application: Develop a mobile application for users to access the platform on the go.

Integration with Other Systems: Consider integrating with existing banking or financial systems for a more comprehensive solution.

This structured approach will help you build a comprehensive Expense Tracker App that meets user needs and adapts to future requirements.

Written by Surfside Media

Senior Full Stack Developer specializing in Web Technologies.