GeoIP Service for geoip.coffermedia.com
======================================

This package contains a simple self-hosted IP-to-location API with:
- IP2Location LITE database integration
- API key authentication
- Rate limiting
- IP result caching
- Admin dashboard for API keys and logs

1. File Structure
-----------------
Upload the `geoip` folder to your Namecheap hosting under:

  /home/USERNAME/public_html/geoip/

Then point the subdomain:

  geoip.coffermedia.com -> /public_html/geoip

Main folders:
- config/db.php          : MySQL connection configuration
- api/index.php          : Public API endpoint
- lib/GeoIP.php          : GeoIP lookup wrapper (cache + IP2Location)
- lib/Auth.php           : API key authentication
- lib/RateLimiter.php    : Rate limiting logic
- admin/                 : Admin dashboard (login, API keys, logs)
- geo/                   : IP2Location BIN file + PHP library
- cron/update_db.php     : Cron script to update the IP DB

2. Database Setup
-----------------
Create a new MySQL database and user in Namecheap cPanel, then import:

  geoip_db.sql

into that database.

Update `config/db.php` with your actual:
- DB_USER
- DB_PASS
- DB_NAME

3. Admin User
-------------
After importing `geoip_db.sql`, insert an admin user:

- Run a small PHP script locally or via a temporary file on the server:

    <?php
    echo password_hash("YourStrongPassword123", PASSWORD_DEFAULT);

- Copy the generated hash and insert into `admin_users`:

    INSERT INTO admin_users (username, password_hash)
    VALUES ('admin', 'PASTE_HASH_HERE');

4. IP2Location Library & Database
---------------------------------
You MUST download:

- IP2Location LITE DB3 IPv6 BIN file
- IP2Location PHP library

Steps:
- Register at: https://lite.ip2location.com/
- Download the LITE DB3 IPv6 BIN (ZIP)
- Extract `IP2LOCATION-LITE-DB3.IPV6.BIN` into:

    geoip/geo/IP2LOCATION-LITE-DB3.IPV6.BIN

- Download the PHP library from:
    https://github.com/ip2location/ip2location-php

- Place/include it in: geoip/geo/ip2location.php
  so that the class \IP2Location\Database is available.

5. Cron Job for DB Updates
--------------------------
In Namecheap cPanel -> Cron Jobs, add a monthly (or weekly) cron:

  php -q /home/USERNAME/public_html/geoip/cron/update_db.php

This will download and extract the latest LITE database.

6. Using the API
----------------
1) Log into the admin dashboard:

   https://geoip.coffermedia.com/admin/

2) Create an API key.

3) Call the API from any website:

   https://geoip.coffermedia.com/api/?key=YOUR_API_KEY&ip=1.2.3.4

If `ip` is omitted, the service uses the caller's IP.

Response JSON example:

{
  "success": true,
  "ip": "1.2.3.4",
  "country": "India",
  "region": "Kerala",
  "city": "Ernakulam",
  "latitude": 10.000000,
  "longitude": 76.000000,
  "timezone": "Asia/Kolkata",
  "source": "db"
}

7. Security Notes
-----------------
- Ensure your subdomain uses HTTPS (via cPanel SSL).
- Keep the admin panel protected with strong passwords.
- You may additionally protect /admin using cPanel "Directory Privacy".

