dimanche 24 avril 2016

if statement not working - javascript

I'm unable to identify the problem in this javascript code. I'm returning code 154, 155 and 1 from the server on certain logic. these numbers are returns but if statement is not working properly if it return a value which is true in if statement it still runs else (Default). Don't know why it cannot check if the value is same or not.

`$("#btnlogin").click(function (){

   // Create our XMLHttpRequest object
    var hr = new XMLHttpRequest();
    // Create some variables we need to send to our PHP file
    var url = "phpfiles/login.php";
    var username = document.getElementById("username").value;
    var password = document.getElementById("password").value;    

    var vars = "username="+username+"&password="+password;
    hr.open("POST", url, true);
    // Set content type header information for sending url encoded variables in the request
    hr.setRequestHeader("Content-type", "plain/text");

    // Access the onreadystatechange event for the XMLHttpRequest object
    hr.onreadystatechange = function() {
        if(hr.readyState == 4 && hr.status == 200) {
            var return_data = hr.responseText;


            if (return_data == "154"){
                $.Notification.notify('error','top left', 'Wrong Information', 'Username/Password is incorrect');
            }else if (return_data=== "155")
            {
                $.Notification.notify('error','top left', 'User Does not exist', 'This user Does not exist. Please Check and try again.');
            }
            else if (return_data == "1")
            {
                $.Notification.notify('success','top left', 'Success', '');
                GenToken();
            }else{
                alert();
            }
        };
    };
    // Send the data to PHP now... and wait for response to update the status div
    hr.send(vars); // Actually execute the request




});`

it always go down to else no matter if the condition is true or false. It is pointing towards this code

`<?php

if(isset($_POST['username']))
{
    $username = FILTER_INPUT(INPUT_POST, "username", FILTER_SANITIZE_STRING);
    $password = FILTER_INPUT(INPUT_POST, "password", FILTER_SANITIZE_STRING);

    require_once("../class/class.login.php");
    $lh = new LoginHander;

    $lh->LogUserIn($username, $password, 'admin');
}
?>`

and this is calling this function

`    public function LogUserIn($username, $password)
    {
        $username = $this->es($username);
        $password = $this->es($password);
        $ip = $this->getClientIP();
        $sql = "SELECT username, password FROM admin WHERE username = ?";

        try{
            $query = $this->con->prepare($sql);
            $query->execute(array($username));
            $result = $query->fetchall(PDO::FETCH_ASSOC);
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }
        $this->addlog($username, $ip);
        if(count($result) > 0)
        {
            header("Content-type: plain/text");
           $pass = $result[0]['password'];
           if(password_verify($password, $pass)){
               $this->SetVars($username);
               echo "1";
           }else{
                echo "154";
                exit();
           }
        }
        else
        {
           echo "155";
                exit();
        }
    }`

I don't know where is the problems i have tried === instead of == but it doesn't help. Please tell me where i made the mistake. Thank you in advance

Aucun commentaire:

Enregistrer un commentaire