Skip to content

MFAIZAN20/php-voting-system

Repository files navigation

Voting App - PHP Assignment

A simple voting application built with PHP that allows users to create topics, vote on them, and manage their profiles.

Features

  • 🔐 User Authentication

    • User registration and login
    • Session management
    • Secure password handling
  • 📝 Topic Management

    • Create new topics with titles and descriptions
    • Browse all available topics
    • View topic details
  • 🗳️ Voting System

    • Upvote or downvote topics
    • One vote per user per topic
    • Real-time vote count display
    • View voting history in profile
  • 🎨 Theme Support

    • Light and Dark themes
    • Instant theme switching
    • Theme persistence across sessions
  • 👤 User Profile

    • View personal voting history
    • Track all votes cast

Technologies Used

  • PHP 8.4+ - Server-side logic
  • File-based Storage - Text files for data persistence
  • PHPUnit - Unit testing framework
  • CSS3 - Responsive styling with theme support
  • Composer - Dependency management

Project Structure

assignment_2/
├── config.php              # Configuration and session management
├── functions.php           # Core application functions
├── index.php              # Login page (main entry point)
├── register.php           # User registration page
├── dashboard.php          # User dashboard
├── topics.php             # Browse all topics
├── profile.php            # User profile and voting history
├── vote.php               # Vote processing script
├── login.php              # Login redirect
├── logout.php             # Logout handler
├── css/
│   └── style.css          # Application styles
├── tests/
│   ├── VotingAppTest1.php # Unit tests - basic functionality
│   ├── VotingAppTest2.php # Unit tests - edge cases
│   └── phpunit.xml        # PHPUnit configuration
├── composer.json          # Composer dependencies
└── .gitignore            # Git ignore rules

Installation

Prerequisites

  • PHP 8.0 or higher
  • Composer
  • Git

Setup Instructions

  1. Clone the repository

    git clone https://github.com/MFAIZAN20/php-voting-system.git
    cd php-voting-system
  2. Install dependencies

    composer install
  3. Create data files

    touch users.txt topics.txt votes.txt
  4. Start PHP development server

    php -S localhost:8000
  5. Open in browser

    http://localhost:8000
    

Running Tests

The project includes comprehensive unit tests using PHPUnit.

Run all tests

vendor/bin/phpunit --testdox tests/

Run specific test file

vendor/bin/phpunit --testdox tests/VotingAppTest1.php
vendor/bin/phpunit --testdox tests/VotingAppTest2.php

Test Coverage

  • ✅ User registration (success/failure cases)
  • ✅ User authentication (valid/invalid credentials)
  • ✅ Topic creation and retrieval
  • ✅ Voting system (upvote/downvote)
  • ✅ Vote validation (duplicate votes, invalid topics)
  • ✅ Session management
  • ✅ Cookie management

Usage

Register a New Account

  1. Navigate to the registration page
  2. Enter a unique username and password
  3. Click "Register"

Login

  1. Enter your username and password
  2. Click "Login"

Create a Topic

  1. From the dashboard, enter a topic title and description
  2. Click "Create Topic"

Vote on Topics

  1. Browse topics from the "Topics" page
  2. Click "Upvote" or "Downvote" on any topic
  3. Each user can only vote once per topic

Change Theme

  1. Click the ☀️ Light or 🌙 Dark button in the header
  2. Theme preference is saved automatically

View Profile

  1. Click "Profile" in the navigation
  2. See all your voting history

Data Storage

The application uses text files for data persistence:

  • users.txt - User credentials (format: username:password)
  • topics.txt - Topics (format: id|creator|title|description)
  • votes.txt - Votes (format: username|topicID|voteType)

Security Features

  • Session-based authentication
  • Input validation and sanitization
  • XSS protection with htmlspecialchars()
  • File locking for concurrent access
  • CSRF protection (session-based)

Development

Code Structure

  • config.php - Centralizes configuration and session initialization
  • functions.php - Contains all core functions:
    • User management (registerUser, authenticateUser)
    • Topic management (createTopic, getTopics)
    • Voting system (vote, hasVoted, getVoteResults)
    • Session/Cookie management
    • Theme management

Adding New Features

  1. Add functions to functions.php
  2. Create corresponding UI in relevant PHP pages
  3. Add tests in tests/ directory
  4. Update this README

Testing Environment

Tests use separate data files in the tests/ directory to avoid affecting production data:

  • tests/users.txt
  • tests/topics.txt
  • tests/votes.txt

Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

This project is created as an educational assignment.

Author

MFAIZAN20

Acknowledgments

  • PHP Documentation
  • PHPUnit Documentation
  • Web Development Best Practices

About

A PHP-based voting application with user authentication, topic management, upvote/downvote system, theme switching, and comprehensive PHPUnit tests. Features file-based storage and responsive design.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors