Active session in REST API

0

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.

    
asked by Lara 12.06.2018 в 13:50
source

2 answers

0

On the one hand, session_status() can also return a numeric value.

  

PHP_SESSION_DISABLED if sessions are disabled.

     

PHP_SESSION_NONE if sessions are enabled, but none exist.

     

PHP_SESSION_ACTIVE if sessions are enabled, and one exists.

     

_DISABLED = 0   _NONE = 1   _ACTIVE = 2

link

And you must also call the change of location properly if you are going to use javascript .

    <?php if (session_status() != "PHP_SESSION_ACTIVE" && session_status() != 2) { ?> 
         <script>window.location='App.php'</script>
    <?php }?>
    
answered by 12.06.2018 в 14:47
0

If you start all the php script with the following line?

Administracion.php
require "..haysesion.php";
...

-Comparador.php
require "..haysesion.php";
...

-App.php
require "..haysesion.php";
...


haysesion.php
Si no hay sesion -> die;

pseudocodigo:

haysesion.php

<?php
if !hayunasesioniniciada() {
   echo "no hay";
   die:
   (en ajax)
  json_enconde(array('error' => 1));
  die;
  o redirigir...
  header("Location: dom.com");
  die;
}

here the program ends if it does not find a die; the program continues  normally after the call of the required.

    
answered by 12.06.2018 в 15:48