i have a php code but when i run it on xampp localhost it errors err_too_many_redirects. here is the codes:
the login.php that when an user comes :
<?php
session_start();
<The php codes that checks from mysql...>
if (!isset($_SESSION['ip_allowed'])) {
if ($count === 1) {
$_SESSION['ip_allowed'] = true;
header('Location: index.php');
exit;
} else {
header('Location: access_denied.html');
exit;
}
} else {
header('Location: index.php');
exit;
}
?>
access_denied.html is not an important file because it is a normal html file no php codes
and the index .php
<?php
session_start();
include 'login.php';
if (!isset($_SESSION['ip_allowed'])) {
header('Location: access_denied.html');
exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Welcome!</title>
</head>
<body>
<h1>
Your IP Is Allowed!✅
</h1>
<a href="login.php">
Back To Login
</a>
</body>
</html>
but when if i login.php and it allows my ip then it redirects me to index.php and in index.php i coded a code that checks if it has the requirements like $_SERVER[‘ip_allowed’] if not it redirects to access_denied.html
i hope someone can help me!. thanks!
i did try changing session_start()’s line and adding exit(); or die(); after headers but nothing worked
2
Answers
i did delete include command and it worked
Because you included login.php to index.php it redirects to your index forever.
Try this: