PHP login verification error

Asked

Viewed 51 times

0

People I am developing a PHP application for a camp of my Church, then I need to check if the user is logged in to be able to redirect him if he is logged in and try to reload the login page and request access, if not logged in, basically I want to redirect who has logged in so that it does not enter user and password again and, if not logged in, display the login page normally. That’s the whole problem. I made a file for the functions I will need and the login file, the paths of the files are correct, but is giving this error here when access the login.php:

Notice : Undefined index: logged_in in /home/vol15_6/epizy.com/epiz_22599397/htdocs/application/controller/functions.php on line 16

On lines 14 - 21 of the archive functions.php I got it right here:

function isLoggedIn()
{
  if (isset($_SESSION['logged_in']) || $_SESSION['logged_in'] == true)
  {
    return true;
  }
  return false;
}

In my file login.php I have this call here:

<?php
if (isLoggedIn()) {
  // Redireciona para dashboard
  header("Location: /dashboard");
} else {
  // Num faz nada
}
?>
<html>(muito html e css)</html>

What is causing the error and how can I correct it? If someone knows I will be very grateful.

  • it is not finding the, $_Session, it may be that it has started, if it was, check if you assigned a value $_SESSION['logged_in'] = true;

  • In my case the variable is initialized with session_start() and I just attribute true for ['logged_in'] when the login is done, however, in my verification method, q sees if the user is logged in, I check if the variable is set or if it is true. I don’t know what else to do.

  • put a comment on if /* if until the end of it */ and put a print_r($_SESSION) in place, and note if it was set correctly when you have the login, because the error is acknowledging that there is no variable , and according to its condition it would be more correct to put && in place of || "&&" = a AND, and "||" = a OR, so I guess the most correct one would be IF (if there is a variable) it is true, then it answers truth or false

  • and to set it to be $_SESSION['logged_in'] = true;

  • When I log in, actually the value is correctly assigned and I have corrected my function as you indicated. After the correction and checking if the value was being correctly assigned, I took the comments from if and when I log in and try to access the login page (which should check the login and redirect to /Dashboard) I am redirected to a 404 error. That is, if you are logged in the login page stops working and returns a 404 instead of redirecting, but if n is logged in the login page works normal (except for checking).

  • then comments the header and puts an echo "I’m logged in"; if it works and the header Location is wrong... more to my mind it’s already wrong because you didn’t get the id of which native path is

  • http://php.net/manual/en/function.header.php take a look at the 6th note, there are several ways to check it.. or pd setar simpler with http://site/Dashboard

  • $_Server and more automatic, you will have no problem when publishing your system if it is local, you will have to remember to modify it correctly when publishing to the server

  • Remembering that you have to start in both files... Example you start when the guy logs in to a page called login.php, but then when he’s redirected to the other page for example is_logged.php you have to sign in again, unless you are doing a include of some file that always initializes the session

  • I solved using JS. When I get home I send the solution, but thanks to all.

Show 5 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.