Notice: Undefined index: Why does this error mark me?

1

I am generating a login log I am integrating session_start(); I have an inconvenience, it marks me an error which is the following: Notice: Undefined index: tiempo in C:\xampp\htdocs\revista\rev\administrador\consultar_registros.php on line 26 , line 26 is the following: if(time() - $_SESSION['tiempo'] > 18000) { I leave the code that I am using, I hope help me to solve this problem because I do not find that it should be I'm going crazy with this and I do not know how to solve it.

funcional.php

public function verificar_login($nombre_usuario, $password){

        $sql = "SELECT * FROM usuario where nombre_usuario ='$nombre_usuario' and password = '$password' ";
        $result = $this->conecta()->query($sql);

        if($result->num_rows > 0){
            while ($row = $result->fetch_assoc()) {
                session_start();
                $_SESSION['nombre_usuario'] = $row['nombre_usuario'];
                $_SESSION['password'] = $row['password'];
                $_SESSION["autentificado"] = "SI";
                $_SESSION['start'] = date("Y-n-j H:i:s");
                $_SESSION['tiempo'] = time();
                if ($result->num_rows > 0)
                    header("Location:consultar_registros.php");
                }
            }else{
                echo '<script language="javascript">
                alert("Los datos son incorrectos vuelve a intentarlo");
                window.location.href="../administrador/index.php";
                </script>';
        }
    }

consult_registers.php

<?php
session_start();
if(time() - $_SESSION['tiempo'] > 18000) { ---Aquí es donde me indica que esta el error---
  echo '<script language="javascript">
      alert("Debes de registrarte para poder ingresar");
      window.location.href="index.php";
    </script>';
  session_destroy();
}
?>
    
asked by R.C. Ana 04.01.2018 в 19:31
source

1 answer

3

This error is typical of PHP and occurs when you try to access a value of an array using a nonexistent index, in your case:

$_SESSION['tiempo']

There is no where you try to access it. You can do a debug of $ _ SESSION at any time to see what it contains:

var_dump($_SESSION);

However, and as a general rule, since its content may vary, to avoid errors like the one you get, it is controlled that it contains a certain index before trying to access it:

if (isset($_SESSION['tiempo']) {
    // acciones
} else {
    // otras acciones
}

In your case, surely when you try to access the value of $ _SESSION, the function that initializes the value was not called or maybe the value or all the values contained in $ _SESSION were erased in another point of the program, before try to access it.

You can change your code to get information about the failure and that the failure itself is not generated, for example:

<?php
session_start();

if (! isset($_SESSION['tiempo'])) {
    //
    // Aquí puedes mostrar un mensaje que te avise durante el desarrollo 
    // o bien generar un log de error
    //

} elseif(time() - $_SESSION['tiempo'] > 18000) { ---Aquí es donde me indica que esta el error---
  echo '<script language="javascript">
      alert("Debes de registrarte para poder ingresar");
      window.location.href="index.php";
    </script>';
  session_destroy();
}

However, you must find the cause of the problem, if it is. If you are only checking if you are logged in and it is perfectly normal that the function verify_login () has not been invoked, you will avoid the error just by changing your problematic line:

if(isset($_SESSION['tiempo']) && ((time() - $_SESSION['tiempo']) > 18000)) {
    
answered by 04.01.2018 / 20:07
source