0

First of all, sorry for the title, I didn't know what to name the problem I'm facing. I will try to explain. I have a php file which which handles the user login like so:

session_start();
if(isset($_POST['do_login'])) {
    require_once '../inc/mysql_connect.php';
    $uname = $_POST['username'];
    $pass = $_POST['password'];
    $uname = strip_tags(mysqli_real_escape_string($con,trim($uname)));
    $pass = strip_tags(mysqli_real_escape_string($con, trim($pass)));
    $sql = "SELECT * from users where username='".$uname."'";
    $select_data = mysqli_query($con,$sql)or die(mysqli_error());

    if (mysqli_num_rows($select_data)>0) {
        $row = mysqli_fetch_array($select_data);
        $password_hash = $row['password'];
        $token = $row['token'];
        $email = $row['email'];
        $email_verified = $row['email_verified'];

        if (password_verify($pass,$password_hash)) {
            if ($email_verified > 0) {
                setcookie('token', $token, time()+31556926 , "/", "example.com", true, true);
                echo "success";
            } else {
                echo "email_not_verified";
                $_SESSION['email_not_verified'] = "true";
                $_SESSION['username'] = $uname;
                $_SESSION['email'] = $email;
                $_SESSION['token'] = $token;
            }
        } else {
            echo "fail";
        }

        exit();
    }
}

and I have a js file which sends a request to the file:

function do_login()
{
    $("#btn-login").addClass('disabled');
    var username=$("#emailid").val();
    var pass=$("#password").val();

    if(username!="" && pass!="") {
        $.ajax({
            type:'post',
            url:'https://example.com/core/auth/do_login.php',
            data:{
                do_login:"do_login",
                username:username,
                password:pass
            },
            success: function (response) {
                if (response =="success") {
                    $('#login').modal('hide');
                    window.location.href="https://example.com/account/";
                } else if (response=="email_not_verified") {
                    window.location.href="https://example.com/login"
                } else {
                    shakeModal();
                    $("#btn-login").removeClass('disabled');
                }
            }
        });
    }

    return false;
}

It works, however if I instantiate a class like $MyClass = new MyClass();, the PHP code works, but the Ajax success: function (response) { is not being called.

Rasclatt
  • 12,498
  • 3
  • 25
  • 33
floreich
  • 187
  • 2
  • 16
  • 1
    where and how you "instantiate a class like $MyClass = new MyClass(); " ? – Kamil Kiełczewski Apr 30 '18 at 19:29
  • at the top. I didn't add it to the code – floreich Apr 30 '18 at 19:30
  • At the top of what? And why? What are you trying to do with the class? – Dave Apr 30 '18 at 19:40
  • at the top of my login file. I'm trying to call a method from the class – floreich Apr 30 '18 at 19:42
  • Shouldn't really matter, I'm just confused why the success function in ajax stops working then, all the other stuff is working perfectly, the php is completely being executed -> the user is logged in, but the code in the ajax success function is not being called – floreich Apr 30 '18 at 19:43
  • how do you know that after add $MyClass = new MyClass(); your PHP code works correctly? – Kamil Kiełczewski Apr 30 '18 at 19:57
  • because everything the php should be doing, is done – floreich Apr 30 '18 at 20:14
  • So you're saying if you do `console.log(response);` or `alert(response);` before the `if` in the `success: function(response)`, they are empty? – Rasclatt Apr 30 '18 at 21:22
  • @floreich "because everything the php should be doing, is done" Could you be more specific? What does it do, and how did you check? Does the browser receive `success`, with a new cookie? Also, what does the constructor `$MyClass` do? – David Knipe Apr 30 '18 at 22:06
  • @Rasclatt no, they are not empty. Both return "Success" – floreich Apr 30 '18 at 22:43
  • If they both return `success`, what are you expecting to return there? I thought you said the `success` function isn't being called? it has to be called if you get data back from php via `response` – Rasclatt Apr 30 '18 at 22:46
  • I tried them before the method, if I add alert() and console.log inside the success: method, it doesn't work – floreich Apr 30 '18 at 22:54
  • So the class you add, which seems to effect the success in the ajax, is on the `/core/auth/do_login.php` page? So you can get a success from that if there is no class created on it? Is that what you are saying? – Rasclatt Apr 30 '18 at 22:56
  • yes, that's right – floreich Apr 30 '18 at 23:09
  • But I don't know why. My class has neither syntax errors, nor anything else what could be causing this. – floreich Apr 30 '18 at 23:41
  • also the code of the class I'm adding is being executed, everything works, except the success function – floreich Apr 30 '18 at 23:48
  • Make sure to add error reporting at the top of the page: `ini_set('display_errors',1);error_reporting(E_ALL);` – Rasclatt May 01 '18 at 04:09
  • still not returning any errors – floreich May 01 '18 at 04:19
  • @floreich - tell me what do you see in chrome console> network tab> in request before and after add $myClass to php - is the responses IDENTICAL or different? Http error codes are 200 or other? – Kamil Kiełczewski May 03 '18 at 09:12

1 Answers1

2

Your code is likely failing, hence your success: block is never being called. Try adding this below your success: block:

error: function(xhr, error){
     console.log(xhr); 
     console.error(error);
}

I used this as a base for the error block.

Rasclatt
  • 12,498
  • 3
  • 25
  • 33
dbradley
  • 36
  • 1