I need to know if a session is active or not. I have three pages .php
:
-Administration.php
-Comparador.php
-App.php
The App.php page has an access form that connects to the application through a AJAX
request. So far I can see the three pages with the route of each without problem. What I want to do is check in Administracion.php and in Comparador.php if you have logged in before on the App.php page
In the case that there is an active session if you can enter the secondary pages but if there is no active session, you do not have to let me enter the secondary pages. When trying to enter the secondary pages, you must redirect me to the main page.
When I write the code if (session_status() !== PHP_SESSION_ACTIVE) {window.location='App.php'}
in the secondary pages I get the following error:
Any ideas?
$app->post('/login', function ($request, $response) {
$em = getEntityManager();
$args = $request->getParsedBody() ?? json_decode($request->getBody(), true);
$user = $em->getRepository(Usuario::class)->findOneByUsername($args['username']);
if (null == $user) {
echo "<script language='javascript'>alert('No existe ese usuario'); window.location='index.html'</script>" ;
} else {
if ($user->getPassword() == $args['password']){
$_SESSION['id'] = $user->getId();
$_SESSION['username'] = $user->getUsername();
if($user->getAdmin() && $user->getEnabled()){
echo "<script language='javascript'>window.location='Administracion.php'</script>" ;
} else if($user->getEnabled()){
echo "<script language='javascript'>window.location='Comparador.php'</script>" ;
} else {
echo "<script language='javascript'>alert('Su cuenta no está activada'); window.location=''</script>" ;
}
} else {
echo "<script language='javascript'>alert('Contraseña incorrecta'); window.location='App.php'</script>" ;
}
}
});
The User class has an attribute called username, I have been using it without problem before adding the response code to each secondary page.