how do I compare a session?

-1

I would like to know how to compare a session of a page php with ajax for the simple reason to display a alert more beautiful to the client the comparison that I have and the following it compares not part in php if there is a user logged in if it does not exist the message of alert has how do I do this with ajax ? to simply change the alert ?

This and the code php that does the verification is at the top of the page that is called when I click on Buy:

if ( ! isset( $_SESSION ) || ! isset( $_SESSION['clienteSession'] ) || $_SESSION['clienteSession'] != true )
{
  echo "<script type=\"text/javascript\">
       alert(\"Você Precisa estar Logado para Efetuar a Compra!\");
       window.location='login.php';
    </script>";  
}

Result:

 if (!isset( $_SESSION ) || !isset( $_SESSION['clienteSession'] ) || $_SESSION['clienteSession'] != true ){
        echo "nao logado";

      }

part js:

$("#comprar").click(function(){
        $.ajax("carrinho.php",{

        }).done(function(r){
            if(r == "nao logado"){
                alertify.error("Você Precisa estar Logado para ter Acesso a Esta Página");
                setTimeout("window.location=login.php",2000);
                return;
            }
        }).fail(function(){
            alertify.error("Opss Algo deu Errado");
        })
      });
    
asked by anonymous 29.12.2015 / 19:59

2 answers

2

First create the jQuery AJAX call:

$.ajax({
    url: "verificar.php"
    , success: function(result){            
        if(result == "NAO LOGADO"){
            // Colocar mensagem bonitinha aqui...
            // Faz a logica para ir pro login
        }           
    }
});

Then in the file verificar.php we compared the SESSION

session_start();
if ( !isset( $_SESSION['clienteSession'] ) || $_SESSION['clienteSession'] != true )
{
  echo "NAO LOGADO";
  exit;
}
    
29.12.2015 / 20:15
2

Here is a suggested refactoring:

session_start();
if ( isset( $_SESSION['clienteId'] ) && $_SESSION['clienteId'] > 0 ) {
  echo '<div class="verdinho">você está logado</div>';
  // no lugar do echo, retorne no ajax a mensagem
} else {
  echo '<div class="vermelhao">você precisa se logar</div>';
  // no lugar do echo, retorne no ajax a mensagem
}

Without seeing the rest of your code, it's hard to give details. The important thing is that you get the return of this code and show it in a DIV or appropriate place on the page, without forcing the user to click OK, etc.

Nothing prevents you in the error message from putting the link to the login form already, or returning more than one variable, one with the message and another with a status, indicating whether or not it is logged. It's just a starting point.

The important thing would be to make AJAX calling take action with the information, not the page called.

Also note that instead of testing whether the client is logged in or not, we are testing whether the client ID is greater than zero. So, in the future, you can customize the messages according to the client's authorization level, depending on which part of the site you call.

    
29.12.2015 / 20:17