Skip to content

amirtar/php-counter-redirect

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PHP Page Counter & Forwarder

A lightweight PHP application that counts page visits while redirecting users to a configured URL. Designed for easy deployment on shared hosting environments.

Features

  • Simple URL Forwarding: Redirects visitors to a configured URL
  • Visitor Logging: Logs visitor data (IP, user agent, referrer, location) to YAML format
  • Single-File Deployment: Packages into one PHP file for easy upload
  • Shared Hosting Compatible: Works on basic LAMP stack (Linux, Apache, MySQL, PHP) hosting
  • No Database Required: Uses file-based logging to work on minimal hosting plans
  • Budget-Friendly: Designed for the least expensive hosting servers
  • Simple File Upload Deployment: No complex server configuration needed
  • Concurrent Safe: Uses file locking for safe concurrent access
  • Configurable Paths: Supports custom log file locations

Quick Start

1. Initial Setup

# Run the setup script (handles dependencies + configuration)
scripts/setup.sh

This will:

  • Install Composer dependencies
  • Create src/config.php from template
  • Prompt you for your redirect URL

Note: src/config.php is git-ignored, so your personal URL won't be accidentally committed.

2. Test Locally

# Test the source code directly
php -S localhost:8000 -t src/

Visit http://localhost:8000 to test the redirect and logging.

3. Package for Deployment

# Create single-file deployment
php scripts/package.php

# Test the packaged deployment
php scripts/test_deploy.php

This creates deploy/index.php - a single file containing your entire application.

4. Deploy

Upload deploy/index.php to your shared hosting server using:

  • cPanel File Manager - Upload directly through web interface
  • FTP client - Upload to your public_html or www directory
  • Hosting provider's file upload tool

The application will:

  • Extract its dependencies automatically on first run
  • Create visitors.yml and observer.log in the same directory
  • Start redirecting and logging visitors immediately
  • Work without any database setup or server configuration

Development

Project Structure

src/                    # Source code
├── config.php         # Configuration
├── index.php          # Main application
└── log_visitor.php    # Logging functionality

scripts/               # Build and test scripts
├── package.php        # Creates single-file deployment
└── test_deploy.php    # Tests the deployment

tests/                 # Unit tests

Build Process

  1. Setup: scripts/setup.sh - Install dependencies and configure
  2. Package: php scripts/package.php - Create deployable file
  3. Test: php scripts/test_deploy.php - Validate deployment
  4. Deploy: Upload deploy/index.php to your server

Debug Mode

For troubleshooting deployment issues:

php scripts/package.php debug

This adds debug output to help diagnose extraction and path issues.

Configuration Options

Environment Variables

  • VISITOR_YML_PATH: Custom path for visitor log file
  • OBSERVER_LOG_PATH: Custom path for debug log file

Runtime Paths

By default, log files are created in the same directory as the deployed index.php. You can customize this by setting the environment variables before the application runs.

Hosting Requirements

Target Environment

This application is specifically designed for shared hosting environments running a standard LAMP stack (Linux, Apache, MySQL, PHP). It's perfect for:

  • Budget hosting plans - Works on the least expensive shared hosting servers
  • Basic cPanel hosting - Simple file upload deployment via File Manager
  • Minimal configuration - No server setup or database configuration required
  • Shared hosting restrictions - No shell access or special permissions needed

Technical Requirements

  • PHP 7.4+ (most shared hosts provide this)
  • Apache web server (standard on most shared hosting)
  • File write permissions (for logging - usually available by default)
  • cURL extension (for URL validation - commonly available)

Development Requirements

  • Composer (only needed for development, not on the hosting server)

Disclaimer

This code was generated with the help of AI assistance. The original author had no prior PHP development experience before creating this project. Use at your own risk.

While the code has been tested and includes safety measures like file locking and input validation, please review the code thoroughly before deploying to production environments. Consider having an experienced PHP developer review the implementation if you're using this for critical applications.

License

MIT License - see LICENSE file for details.

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Run tests: php scripts/test_deploy.php
  5. Submit a pull request

About

A lightweight PHP application for counting page visits while redirecting users. Designed for shared hosting environments with minimal configuration.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors