validate user

0

Good I have a doubt I have made a login but the problem is that if a user has not logged in you can enter the page by putting the extension; How can I verify if that person has logged in and if he has not already sent him to login??

<?php

  session_start();

  if (isset($_SESSION['user_id'])) {
    header('Location: ./logueado/index.php');
  }
  require 'database.php';

  if (!empty($_POST['email']) && !empty($_POST['password'])) {
    $records = $conn->prepare('SELECT id, email, password FROM users WHERE email = :email');
    $records->bindParam(':email', $_POST['email']);
    $records->execute();
    $results = $records->fetch(PDO::FETCH_ASSOC);

    $message = 'Usuario o contraseña incorrectos';

    if (count($results) > 0 && password_verify($_POST['password'], $results['password'])) {
      $_SESSION['user_id'] = $results['id'];
      header("Location: ./index.php");
    } else {
      $message = 'Lo siento el usuario o contraseña no existe';
    }
  }

?>
    
asked by Brouse_13 05.11.2018 в 16:38
source

1 answer

0

If I have not misunderstood, you want that, if someone visits /logueado/index.php and is not logged in, return to the login page, for which you have to do again is to check again on the pages where the user You have to be registered:

session_start();

if (!isset($_SESSION['user_id'])) {
  header('Location: <ruta al login.php>');
}
    
answered by 05.11.2018 в 16:56