A simple web application that counts HTML elements on any webpage and provides statistics about requests.
- Takes a URL and HTML element name (like 'img', 'div', 'p')
- Fetches the webpage and counts how many times that element appears
- Stores results in MySQL database or MariaDB
- Shows statistics about all requests made
- Caches results for 5 minutes to avoid repeated requests
- Element Counting: Uses PHP DOMDocument to parse HTML and count elements
- 5-minute Caching: Same URL+element requests return cached results
- Statistics: Shows domain stats, fetch times, and global element counts
- Database Storage: Normalized MySQL database with separate tables
- Error Handling: Graceful handling of failed requests and invalid URLs
- AJAX Interface: No page reload needed
- Backend: PHP 8+ with PDO
- Database: MySQL
- Frontend: Vanilla JavaScript (no frameworks)
- Styling: Simple CSS
- Dependencies: Composer for autoloading
- Clone or download this project
- Install dependencies:
composer install
- Create database in MySQL
- Create .env file in project root:
DB_HOST=localhost DB_DATABASE=your_database_name DB_USERNAME=your_username DB_PASSWORD=your_password DB_CHARSET=utf8mb4 - Import database tables from
docs/SQL/:- Run
domain.sql - Run
elements.sql - Run
urls.sql - Run
requests.sql
- Run
- Open
index.phpin your web browser - Enter a website URL
- Enter HTML element name (e.g., img, div, p, a)
- Click "Count Elements"
- View results and statistics
βββ index.php # Main page with form
βββ api/count.php # API endpoint for AJAX requests
βββ assets/ # CSS and JavaScript files
βββ src/ # PHP classes
β βββ Interface/ # Interfaces for services
β βββ Database/ # Database connection
β βββ Models/ # Domain, URL, Element, Request models
β βββ Services/ # Counter, Fetcher, ErrorHandler
β βββ Utils/ # Environment loader
β βββ Validators/ # Input validation
βββ docs/SQL/ # Database schema files
βββ vendor/ # Composer dependencies
- User submits form β JavaScript sends AJAX request to
api/count.php - API validates input β URL and element name checked
- Check cache β Look for the same request made within 5 minutes
- If not cached:
- Fetch webpage using cURL
- Parse HTML with DOMDocument
- Count specified elements
- Save to the database
- Generate statistics from the database
- Return JSON response with count and stats
- domains: Stores unique domain names
- urls: Stores full URLs linked to domains
- elements: Stores HTML element names (img, div, etc.)
- requests: Stores each counting request with results
- PHP 8.0+
- MySQL 5.7+
- Web server (Apache/Nginx)
- cURL extension enabled
- DOM extension enabled
This application has been tested and works correctly in:
- β Google Chrome
- β Chromium
- β Mozilla Firefox
- β Opera
- β Brave
This project uses PHPStan for static code analysis. PHPStan checks the code without running it and finds potential bugs.
./vendor/bin/phpstan analyseCurrent level: 5 (optimal balance between strictness and practicality)
See docs/PHPSTAN.md for detailed documentation about static analysis.
- Some websites may block automated requests
- Large pages may take longer to fetch and parse
- No authentication or rate limiting implemented
- Basic error messages (could be more user-friendly)
"Database connection failed": Check .env file and MySQL credentials "Invalid JSON": Check if API endpoint is accessible "Element count 0": Website might be blocking requests or element doesn't exist "Something went wrong": Check browser console and server error logs
- Planning and architecture: 2 hours
- Database design and setup: 1 hour
- Backend PHP development: 4 hours
- Frontend JavaScript/CSS: 2 hours
- Testing and debugging: 3 hours
- Documentation: 1 hour
Total: ~13 hours