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.