Home > Software design >  ERR_TOO_MANY_REDIRECTS PHP [duplicate]
ERR_TOO_MANY_REDIRECTS PHP [duplicate]

Time:10-03

have a drawback is that when placing the private pages in PHP I get the error ERR_TOO_MANY_REDIRECTS I add the image of the error that appears to me enter image description here this is the code of the login.php

        <?php
    session_start();
      if(!empty($_SESSION['active'])){
        header('location: index.php');
      }else{
 
        $alert='';
    
        if(!empty($_POST)){
    
          if(empty($_POST['usuario']) || empty($_POST['clave'])){
            $alert=' Ingrese su usuario y su clave';
          }else{
    
            require_once "conexion.php";
    
            $user = mysqli_real_escape_string($conection,$_POST['usuario']);
            $pass = md5(mysqli_real_escape_string($conection,$_POST['clave']));
    
            $query = mysqli_query($conection, "SELECT * FROM usuario WHERE usuario = '$user' AND clave = '$pass'");
            $result = mysqli_num_rows($query);
    
            if($result > 0){
              $data = mysqli_fetch_array($query);
              $_SESSION['active'] = true;
              $_SESSION['idUser'] = $data['id'];
              $_SESSION['nombre'] = $data['nombre'];
              $_SESSION['email'] = $data['correo'];
              $_SESSION['user'] = $data['usuario'];
              $_SESSION['rol'] = $data['id_rol'];
    
              header('location: inicio.php');
    
            }else{
              $alert=' El usuario y la clave son incorrectas ';
              session_destroy();
            }
    
          }
    
        }
    
      }
    ?>
    
    <?php include_once('./layout/head.php');?>
    
    <title>Login</title>
    
        <main>
            <div class="wrapper fadeInDown">
                <div id="formContent">
                  <!-- Tabs Titles -->
                  <h2 class="active"> Iniciar sesión </h2>
                  <h2 class="inactive underlineHover"> Registrarse </h2>
              
                  <!-- Icon -->
                  <div class="fadeIn first">
                    <img src="./images/sistema.jpg" id="icon" alt="User Icon" />
                  </div>
              
                  <!-- Login Form -->
                  <form action="" method="post">
                    <input type="text" id="login" class="fadeIn second" name="usuario" placeholder="Usuario">
                    <input type="text" id="password" class="fadeIn third" name="clave" placeholder="Contraseña">                
                    <div class="alert"><?php echo isset ($alert)? $alert: ''; ?></div>
                    <input type="submit" class="fadeIn fourth" value="Iniciar sesión">
                  </form>
              
                  <!-- Remind Passowrd -->
                  <div id="formFooter">
                    <a class="underlineHover" >¿Has olvidado tu contraseña? </a>
                  </div>
              
                </div>
              </div>
        </main>
    
    <?php include_once('./layout/footer.php');?>

this is the start inicio.php. Well, as I said, when I run I get that this page does not work The localhost page has redirected you too many times.

<?php 

    include_once('./layout/head.php');

    if(empty($_SESSION['active'])){

        header('location: login.php');

    }

?>


<header>

    <header>
        <div class="icon__menu">
            <i class="fas fa-bars" id="btn_open"></i>
        </div>
    </header>

    <div class="menu__side" id="menu_side">

        <div class="name__page">
            <i class="fa fa-map-marker"></i>
            <h4>Supermercado Leona</h4>
        </div>

        <div class="options__menu">
                

            <ul class="nav">

                <li>
                    <a href="index.php"  class="selected">
                        <div class="option">
                            <i class="fas fa-home" title="Inicio"></i>
                            <h4>Inicio</h4>
                        </div>
                    </a>
                </li>
                    
                <li>
                    <a> 
                        <div class="option">
                            <i class="fa fa-archive" title="Inventario"></i>
                            <h4>Inventario</h4>
                        </div>
                    </a>
                    <ul style="position: relative;">

                        <li>
                            <a href="producto.php">
                                <div class="option">
                                    <i class="fa fa-product-hunt" title="producto"></i>
                                    <h4>producto</h4>
                                </div>
                            </a>
                        </li>
                        
                    </ul> 
                </li>

                <li>
                    <a href="contactos.php">
                        <div class="option">
                            <i class="fa fa-address-book-o" title="Contactos"></i>
                            <h4>Contactos</h4>
                        </div>
                    </a>
                </li>

                <li>
                    <a href="egresos.php">
                        <div class="option">
                            <i class="fa fa-university" title="Egresos"></i>
                            <h4>Egresos</h4>
                        </div>
                    </a>
                </li>

                <li>
                    <a> 
                        <div class="option">
                            <i class="fa fa-usd" title="Ingresos"></i>
                            <h4>Ingresos</h4>
                        </div>
                    </a>
                    <ul style="position: relative;">

                        <li>
                            <a href="factura_de_venta.php">
                                <div class="option">
                                    <i class="fa fa-file-pdf-o" title="Factura_de_venta"></i>
                                    <h4>Factura de Venta</h4>
                                </div>
                            </a>
                        </li>

                        <li>
                            <a href="cotizacion.php">
                                <div class="option">
                                    <i class="fa fa-file-o" title="Cotizacion"></i>
                                    <h4>Cotización</h4>
                                </div>
                            </a>
                        </li>
                        
                    </ul> 
                </li>

                <li>
                    <a href="ventas.php">
                        <div class="option">
                            <i class="fa fa-shopping-cart" title="Ventas"></i>
                            <h4>Ventas</h4>
                        </div>
                    </a>
                </li>

                <li>
                    <a href="contabilidad.php">
                        <div class="option">
                            <i class="fa fa-bar-chart" title="Contabilidad"></i>
                            <h4>Contabilidad</h4>
                        </div>
                    </a>
                </li>

                <li>
                    <a href="configuracion.php">
                        <div class="option">
                            <i class="fa fa-cog" title="Configuración"></i>
                            <h4>Configuración</h4>
                        </div>
                    </a>  
                </li>

                <li>
                    <a href="salir.php">
                        <div class="option">
                            <i class="fa fa-sign-out" title="Salir"></i>
                            <h4>Salir</h4>
                        </div>
                    </a>
                </li>

            </ul>
                    
        </div>
        
    </div>

    </header>

    <main>
        <h1>Bienvenido</h1><br>
    </main>


<?php include_once('./layout/footer.php');?>

CodePudding user response:

Your problem is once your user have $_SESSION['active'], he can't go out of this condition :

  if(!empty($_SESSION['active'])){
    header('location: index.php');

Script ask "do you have $_SESSION['active'] ? Yes ok then redirect to index, once in index, script ask do you have $_SESSION['active'] ? Yes ok then redirect to index..." = ERR_TOO_MANY_REDIRECTS !

There is many solution, as an exemple you could add another condition checking if the current page is your index ?

 if(!empty($_SESSION['active'])){
if (basename($_SERVER['PHP_SELF'])!= 'index.php')){
    header('location: index.php');
}
}
  •  Tags:  
  • php
  • Related