This commit is contained in:
2024-09-03 02:42:27 +05:30
parent bde2d88ddf
commit a021bfa479
16 changed files with 518 additions and 155 deletions

BIN
htdocs/core/.DS_Store vendored

Binary file not shown.

View File

@@ -0,0 +1,41 @@
<?php
// core/get_statistics.php
require 'config.php'; // Assuming config.php contains your database connection setup
// Initialize variables in case the queries fail
$total_links = 0;
$total_clicks = 0;
$active_users = 0;
// Calculate total links
$sql_total_links = "SELECT COUNT(*) as total_links FROM url";
$result_total_links = mysqli_query($conn, $sql_total_links);
if ($result_total_links) {
$total_links_row = mysqli_fetch_assoc($result_total_links);
$total_links = $total_links_row['total_links'];
} else {
echo "Error fetching total links: " . mysqli_error($conn);
}
// Calculate total clicks
$sql_total_clicks = "SELECT SUM(clicks) as total_clicks FROM url";
$result_total_clicks = mysqli_query($conn, $sql_total_clicks);
if ($result_total_clicks) {
$total_clicks_row = mysqli_fetch_assoc($result_total_clicks);
$total_clicks = $total_clicks_row['total_clicks'];
} else {
echo "Error fetching total clicks: " . mysqli_error($conn);
}
// Calculate active users (assuming there's a 'user_sessions' table or similar)
$sql_active_users = "SELECT COUNT(DISTINCT user_id) as active_users FROM user_sessions WHERE last_active > DATE_SUB(NOW(), INTERVAL 30 DAY)";
$result_active_users = mysqli_query($conn, $sql_active_users);
if ($result_active_users) {
$active_users_row = mysqli_fetch_assoc($result_active_users);
$active_users = $active_users_row['active_users'];
} else {
//echo "Error fetching active users: " . mysqli_error($conn);
}
?>

75
htdocs/core/process.php Normal file
View File

@@ -0,0 +1,75 @@
<?php
// Enable error reporting
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// Include the configuration file
include "core/config.php";
// Start session management
session_start();
// Initialize dotenv and load environment variables
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__ . '/../');
$dotenv->load();
// Retrieve environment variables
$domain = $_ENV['DOMAIN'];
$host = $_ENV['DB_HOST'];
$user = $_ENV['DB_USER'];
$pass = $_ENV['DB_PASS'];
$db = $_ENV['DB_NAME'];
// Establish database connection
$conn = mysqli_connect($host, $user, $pass, $db);
if (!$conn) {
die("Database connection error: " . mysqli_connect_error());
}
// Set a cookie to track user visits or preferences
$cookie_name = "user_visited";
$cookie_value = "true";
$cookie_expire_time = time() + (86400 * 30); // Cookie expires in 30 days
setcookie($cookie_name, $cookie_value, $cookie_expire_time, "/"); // The "/" makes the cookie available across the entire website
// Check if the cookie exists
if (isset($_COOKIE[$cookie_name])) {
// Cookie exists, you can execute specific logic like tracking the visit
} else {
// Cookie does not exist, handle the first-time visit
}
// Set session data for the user
$_SESSION['user'] = "unique_user_id"; // Store unique user ID in session
// Retrieve and use session data
if (isset($_SESSION['user'])) {
$user_id = $_SESSION['user'];
// Do something with $user_id, like loading user-specific data
}
// Initialize the shortened URL variable
$new_url = "";
// Check if there's a GET request and process the shortened URL
if (isset($_GET)) {
foreach ($_GET as $key => $val) {
$u = mysqli_real_escape_string($conn, $key);
$new_url = str_replace('/', '', $u);
}
// Query the database for the full URL associated with the shortened URL
$sql = mysqli_query($conn, "SELECT full_url FROM url WHERE shorten_url = '{$new_url}'");
if (mysqli_num_rows($sql) > 0) {
// Increment the click count for the shortened URL
$sql2 = mysqli_query($conn, "UPDATE url SET clicks = clicks + 1 WHERE shorten_url = '{$new_url}'");
if ($sql2) {
// Fetch the full URL and redirect to it
$full_url = mysqli_fetch_assoc($sql);
header("Location:" . $full_url['full_url']);
exit(); // Stop further script execution after redirection
}
}
}
?>