skip to Main Content

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


  1. Chosen as BEST ANSWER

    i did delete include command and it worked


  2. Because you included login.php to index.php it redirects to your index forever.

    Try this:

    <?php
    session_start();
    
    $page = substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);  
    
    <The php codes that checks from mysql...>
    
    if (!isset($_SESSION['ip_allowed'])) {
        if ($count === 1 && $page !== "index.php") {
            $_SESSION['ip_allowed'] = true;
            header('Location: index.php');
            exit;
        } else {
            header('Location: access_denied.html');
            exit;
        }
    } else {
        header('Location: index.php');
        exit;
    }
    ?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search