How do I compare a Session?

Asked

Viewed 432 times

-1

would like to know how do I compare a session one-page php with the ajax for the simple reason of displaying a alert most beautiful to the client the comparison I have and the following he compares not part in php if there is a logged-in user if there is no logged-in user alert has how I do it with the ajax ? to simply change the alert?

this and the code php checking this at the top of the page which is called when I click to 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>";  
}

Upshot:

 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");
        })
      });
  • ? I know he sure want to know how I do by requisicao ajax this to Voce read my question? there is written that I want to know how to do for when click on the button buy it send the ajax to that page that contains this code snippet and AJAX do the check if you are logged in

  • Alert is never "cute". If you are using PHP, show the message on the screen in a non-intrusive way. The user experience gets better, and your work gets a more professional face. In fact, almost every time someone calls JS from inside PHP, it is a sign of problem in the use of the tools.

  • know la cara not like very much the standard Alert gets este site diz not cool. but has how to do what I want @Bacco ?

  • instead of Alert, you can display the data in a DIV on the page, and with CSS make it very presentable.

2 answers

2


First create the AJAX call jQuery:

$.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 compare to SESSION

session_start();
if ( !isset( $_SESSION['clienteSession'] ) || $_SESSION['clienteSession'] != true )
{
  echo "NAO LOGADO";
  exit;
}
  • I had thought about doing this but I thought it wouldn’t work and I didn’t even try to vlw

  • With this logic you will have to think a little about the time to redirect to the login, maybe give a setTimeout for the user to see the message, more at the beginning is this

  • it’s not good now it was just that

  • I gave you a +1, because it’s a good complement to the idea. The part of the ISSET can dry a little, because you have to give a SESSION START before, so I believe that the 1st isset is not necessary.

  • Thanks, it makes sense not to have the first isset anyway... I’ll edit...

  • It’s not doing much good if he’s letting go

  • I will edit my Perg the way I did

  • give a look at @Jhonatansimões I edited the question with the answer I did

  • Damn, it must have been show now bro... usability is all

  • does not work not for in my if it passes does not return the Alert as I wanted

  • Look now.... I put the exit to stop inside the if

  • before without Exit he was returning me the whole page with echo at the top of the agr page with Exit not changed nd the only thing and that it no longer shows the whole page he shows a page not logged in

  • well I solved how I had to pass a get I needed to declare type get and also needed to get my href out of html otherwise would not go

Show 8 more comments

2

Follow a refactoring suggestion:

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 gets complicated to give details. The important thing is that you take the return of this code and show it in a DIV or appropriate place of the page, without forcing the user to click OK, etc.

Nothing prevents you in the error message already put the link to the login form, or return more than one variable, one with the message and another with a status, indicating logged in or not. It’s just a starting point.

The important thing would be to make the AJAX that calls take a provision with the information, not the page called.

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

Browser other questions tagged

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