0

I have this code in my login.hmtl in s section with ajax method:

$.ajax ({
            type:'post',
            url:'Controladores/login.php',
            data:{do_login:"do_login", nick:nick, pass:pass },
            success:function(response) {
                alert($.trim(response));
                if ($.trim(response) ==='Correcto'){
                    window.location.href="intranet.php?ruta=inicio";
                    return false;
                }
                else{
                    alert('Datos incorrectos. Volver a intentar.');
                }   
            }
        });

This redirects me to login.php where I connect to the database and then check if the username and password are correct. If they are, they return an answer Correct or Incorrect. Here is the main part where I start my session, and also if username and password match, set the username 'NICK' to the session.

session_start();
    $db = new Conexion();
  
    $conn = $db->connect( '00.00.00.00', 'Seguridad');
    $param = [$nick, $pass];
    $query = "exec Seguridad.dbo.autenticacion ?, ?";
    $result= $db->rows($conn, $query, $param);
    $obj = json_decode($result);
    //echo $obj[0]->resultado;
    
    if( $obj[0]->resultado ='Correcto' ){
        $_SESSION['nick']= $nick;
        $_SESSION['nombreusuario'] = $obj[0]->nombreusuario;
      $_SESSION['agencia'] = $obj[0]->CodigoAgencia;  
        echo ("Correcto");

    }else{
        if ($obj[0]->resultado =='Incorrecto'){
             echo ("Incorrecto");
        }
    }  

Then, In my home page called inicio.php I called again the session but the message is: Notice: Undefined index: nick in C:\xampp\htdocs\intranetv3\Vistas\modulos\inicio.php on line 3

<?php
session_start();
$userName = $_SESSION['nick'];
echo "$userName";
?>

I have read other article but have not found anything. I checked if the answer Correct or Incorrect returns the answer and it returns. I printed the user name and password. It worked. I also printed the $_SESSION['nick'], I see it has the username. Apparently when I go to my inicio.hmtl the session just unssets or something, I have no idea what might be wrong. Please HELP!!

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • 1
    Also relevant dupe is https://stackoverflow.com/questions/2063480/the-3-different-equals because of `if( $obj[0]->resultado ='Correcto' ){` – Qirel Aug 18 '20 at 15:07
  • Yeap! I have double == in my code, I just wrote wrong in the question. Thank you for that tip! @Qirel but I still don't know why I get the undefined index – Lorena Acosta Aug 18 '20 at 15:11
  • @Qirel Ah yes... I thought there was something that looked a bit wonky earlier, glad you caught that and I've added that to the other duplicate; *thanks*. – Funk Forty Niner Aug 18 '20 at 15:19

0 Answers0