Created
December 4, 2024 18:11
-
-
Save muath-ye/7d20ee774c202ef76086ff8c1853e83a to your computer and use it in GitHub Desktop.
Track visitors of your website by including this script on your index.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // This is a laravel example | |
| include 'track-image.php'; | |
| use Illuminate\Contracts\Http\Kernel; | |
| use Illuminate\Http\Request; | |
| define('LARAVEL_START', microtime(true)); | |
| /* | |
| |-------------------------------------------------------------------------- | |
| | Check If The Application Is Under Maintenance | |
| |-------------------------------------------------------------------------- | |
| | | |
| | If the application is in maintenance / demo mode via the "down" command | |
| | we will load this file so that any pre-rendered content can be shown | |
| | instead of starting the framework, which could cause an exception. | |
| | | |
| */ | |
| if (file_exists($maintenance = __DIR__.'/../storage/framework/maintenance.php')) { | |
| require $maintenance; | |
| } | |
| /* | |
| |-------------------------------------------------------------------------- | |
| | Register The Auto Loader | |
| |-------------------------------------------------------------------------- | |
| | | |
| | Composer provides a convenient, automatically generated class loader for | |
| | this application. We just need to utilize it! We'll simply require it | |
| | into the script here so we don't need to manually load our classes. | |
| | | |
| */ | |
| require __DIR__.'/../vendor/autoload.php'; | |
| /* | |
| |-------------------------------------------------------------------------- | |
| | Run The Application | |
| |-------------------------------------------------------------------------- | |
| | | |
| | Once we have the application, we can handle the incoming request using | |
| | the application's HTTP kernel. Then, we will send the response back | |
| | to this client's browser, allowing them to enjoy our application. | |
| | | |
| */ | |
| $app = require_once __DIR__.'/../bootstrap/app.php'; | |
| $kernel = $app->make(Kernel::class); | |
| $response = $kernel->handle( | |
| $request = Request::capture() | |
| )->send(); | |
| $kernel->terminate($request, $response); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /* track-image.php */ | |
| // Function to get the location of the visitor based on IP | |
| function getVisitorLocation($ip = null) { | |
| // If no IP is provided, use the visitor's IP address | |
| if ($ip === null) { | |
| $ip = $_SERVER['REMOTE_ADDR']; | |
| } | |
| // API endpoint | |
| $apiUrl = "http://ip-api.com/json/{$ip}"; | |
| // Fetch data from the API | |
| $response = file_get_contents($apiUrl); | |
| if ($response === false) { | |
| return null; // Handle API error | |
| } | |
| // Decode the JSON response | |
| $locationData = json_decode($response, true); | |
| // Check for success | |
| if ($locationData['status'] === 'success') { | |
| return $locationData; | |
| } | |
| return null; // Handle failure | |
| } | |
| // Function to detect device type based on the User-Agent string | |
| function getDeviceType() { | |
| $userAgent = $_SERVER['HTTP_USER_AGENT']; | |
| // Check for mobile devices | |
| if (preg_match('/(android|iphone|ipad|ipod|mobile)/i', $userAgent)) { | |
| return 'Mobile'; | |
| } | |
| // Check for tablet devices | |
| elseif (preg_match('/(tablet|kindle|nexus 7)/i', $userAgent)) { | |
| return 'Tablet'; | |
| } | |
| // Default is Desktop | |
| else { | |
| return 'Desktop'; | |
| } | |
| } | |
| // Get the visitor's location | |
| $location = getVisitorLocation(); | |
| // Get the visitor's Device Type | |
| $device = getDeviceType(); | |
| // If location data is retrieved, process it | |
| if ($location || $device) { | |
| // Prepare the filename based on current time and country | |
| $timestamp = date("Y-m-d_H-i-s"); | |
| $country = preg_replace('/\s+/', '_', strtolower($location['country'])); // Replace spaces with underscores | |
| $filename = "{$timestamp}_{$country}_{$device}.txt"; | |
| // Prepare the content to write to the file | |
| $content = "IP: " . $location['query'] . "\n"; | |
| $content .= "Country: " . $location['country'] . "\n"; | |
| $content .= "Region: " . $location['regionName'] . "\n"; | |
| $content .= "City: " . $location['city'] . "\n"; | |
| $content .= "ZIP Code: " . $location['zip'] . "\n"; | |
| $content .= "Latitude: " . $location['lat'] . "\n"; | |
| $content .= "Longitude: " . $location['lon'] . "\n"; | |
| $content .= "ISP: " . $location['isp'] . "\n"; | |
| $content .= "\n"; | |
| $content .= "Device: " . $device . "\n"; | |
| $content .= "User Agent: " . $_SERVER['HTTP_USER_AGENT'] . "\n"; | |
| // Create the file and write the location data | |
| file_put_contents($filename, $content); | |
| //echo "Location data has been written to: " . $filename; | |
| } else { | |
| //echo "Unable to fetch location."; | |
| } | |
Author
Author
Add this lines of code if you want to use it as og:image
// Serve the image (e.g., a 1x1 pixel transparent image)
header('Content-Type: image/png');
// Create a 1x1 transparent PNG image
$image = imagecreatetruecolor(1, 1);
$color = imagecolorallocatealpha($image, 0, 0, 0, 127); // Fully transparent color
imagefill($image, 0, 0, $color);
imagesavealpha($image, true);
// Output the image
imagepng($image);
imagedestroy($image);so the full code will be
<?php
// Function to get the location of the visitor based on IP
function getVisitorLocation($ip = null) {
// If no IP is provided, use the visitor's IP address
if ($ip === null) {
$ip = $_SERVER['REMOTE_ADDR'];
}
// API endpoint
$apiUrl = "http://ip-api.com/json/{$ip}";
// Fetch data from the API
$response = file_get_contents($apiUrl);
if ($response === false) {
return null; // Handle API error
}
// Decode the JSON response
$locationData = json_decode($response, true);
// Check for success
if ($locationData['status'] === 'success') {
return $locationData;
}
return null; // Handle failure
}
// Function to detect device type based on the User-Agent string
function getDeviceType() {
$userAgent = $_SERVER['HTTP_USER_AGENT'];
// Check for mobile devices
if (preg_match('/(android|iphone|ipad|ipod|mobile)/i', $userAgent)) {
return 'Mobile';
}
// Check for tablet devices
elseif (preg_match('/(tablet|kindle|nexus 7)/i', $userAgent)) {
return 'Tablet';
}
// Default is Desktop
else {
return 'Desktop';
}
}
// Get the visitor's location
$location = getVisitorLocation();
// Get the visitor's Device Type
$device = getDeviceType();
// If location data is retrieved, process it
if ($location || $device) {
// Prepare the filename based on current time and country
$timestamp = date("Y-m-d_H-i-s");
$country = preg_replace('/\s+/', '_', strtolower($location['country'])); // Replace spaces with underscores
$filename = "{$timestamp}_{$country}_{$device}.txt";
// Prepare the content to write to the file
$content = "IP: " . $location['query'] . "\n";
$content .= "Country: " . $location['country'] . "\n";
$content .= "Region: " . $location['regionName'] . "\n";
$content .= "City: " . $location['city'] . "\n";
$content .= "ZIP Code: " . $location['zip'] . "\n";
$content .= "Latitude: " . $location['lat'] . "\n";
$content .= "Longitude: " . $location['lon'] . "\n";
$content .= "ISP: " . $location['isp'] . "\n";
$content .= "\n";
$content .= "Device: " . $device . "\n";
$content .= "User Agent: " . $_SERVER['HTTP_USER_AGENT'] . "\n";
// Create the file and write the location data
file_put_contents($filename, $content);
//echo "Location data has been written to: " . $filename;
} else {
//echo "Unable to fetch location.";
}
// Serve the image (e.g., a 1x1 pixel transparent image)
header('Content-Type: image/png');
// Create a 1x1 transparent PNG image
$image = imagecreatetruecolor(1, 1);
$color = imagecolorallocatealpha($image, 0, 0, 0, 127); // Fully transparent color
imagefill($image, 0, 0, $color);
imagesavealpha($image, true);
// Output the image
imagepng($image);
imagedestroy($image);
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can use it on your index
og:image