A simple voting application built with PHP that allows users to create topics, vote on them, and manage their profiles.
-
🔐 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
- 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
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
- PHP 8.0 or higher
- Composer
- Git
-
Clone the repository
git clone https://github.com/MFAIZAN20/php-voting-system.git cd php-voting-system -
Install dependencies
composer install
-
Create data files
touch users.txt topics.txt votes.txt
-
Start PHP development server
php -S localhost:8000
-
Open in browser
http://localhost:8000
The project includes comprehensive unit tests using PHPUnit.
vendor/bin/phpunit --testdox tests/vendor/bin/phpunit --testdox tests/VotingAppTest1.php
vendor/bin/phpunit --testdox tests/VotingAppTest2.php- ✅ 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
- Navigate to the registration page
- Enter a unique username and password
- Click "Register"
- Enter your username and password
- Click "Login"
- From the dashboard, enter a topic title and description
- Click "Create Topic"
- Browse topics from the "Topics" page
- Click "Upvote" or "Downvote" on any topic
- Each user can only vote once per topic
- Click the ☀️ Light or 🌙 Dark button in the header
- Theme preference is saved automatically
- Click "Profile" in the navigation
- See all your voting history
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)
- Session-based authentication
- Input validation and sanitization
- XSS protection with
htmlspecialchars() - File locking for concurrent access
- CSRF protection (session-based)
- 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
- User management (
- Add functions to
functions.php - Create corresponding UI in relevant PHP pages
- Add tests in
tests/directory - Update this README
Tests use separate data files in the tests/ directory to avoid affecting production data:
tests/users.txttests/topics.txttests/votes.txt
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is created as an educational assignment.
MFAIZAN20
- PHP Documentation
- PHPUnit Documentation
- Web Development Best Practices