I have an ajax login function that worked locally, but now that it's on a live server it returns false for any user information.
for example. I am sending back an array to the device with something like
[["TRUE",1,"username","user@email.com"]]
when posting via ajax I get the following returned.
[["TRUE",0,false,false]]
I have tried various methods of POST and GET to no avail. As I said, the code as is worked locally, but not on a remote server. I was considering a cross domain issue, but I test with cordova which inherently has crossdomain enabled as well as a modified developer version of google chrome that allows cross domain.
I have inserted the code below. Any help would be greatly appreciated.
PHP
function already_logged_in()
{
global $current_user;
get_currentuserinfo();
$creds = array();
$auth_resp = "TRUE";
$creds_id = $current_user->ID;
$img_url_temp = $current_user->ID;
$creds_login = $current_user->user_login;
$creds_pass = 'correct';
$creds_email = $current_user->user_email;
$creds_role = $current_user->user_level;
$creds_fname = $current_user->user_firstname;
$creds_lname = $current_user->user_lastname;
$creds_dispname = $current_user->display_name;
$creds_imgurl = get_av_url($creds_id);
$creds_coverurl = get_cover_url($creds_id);
$creds_array = array($auth_resp,$creds_id,$creds_login,$creds_pass,$creds_email,$creds_role,$creds_fname,$creds_lname,$creds_dispname,$creds_imgurl,$creds_coverurl);
header("Content-Type: application/json");
array_push($creds, $creds_array);
echo json_encode($creds);
die();
}
function login()
{
header("Content-Type: application/json");
$creds = array();
$creds_fail = array();
$creds['user_login'] = $_GET["username"];
$creds['user_password'] = $_GET["password"];
$user_stat = wp_signon($creds, false);
if (is_wp_error($user_stat))
{
$auth_resp = "FALSE";
$creds_array = array($auth_resp,"0","0","0","0","0","0","0","0");
array_push($creds_fail, $creds_array);
echo json_encode($creds_fail);
die();
}else{
already_logged_in();
}
}
JQUERY
function login()
{
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
if(username == "")
{
alert("Please enter username", null, "Username Missing", "OK");
return;
}
if(password == "")
{
alert("Please enter password", null, "Password Missing", "OK");
return;
}
$('#login_loader').css('display', 'block');
$('#login_loader').fadeTo('fast', 1, function () {
// Animation complete.
});
$('#login_form').css('display', 'none');
var xhr = new XMLHttpRequest();
xhr.open("GET", ajaxurl + "?action=login&username=" + encodeURIComponent(username) + "&password=" + encodeURIComponent(password));
xhr.onload = function(){
var creds = [];
var user_info_storage = "";
var log = "";
var id = "";
var login_array = JSON.parse(xhr.responseText);
var creds_login = "";
var creds_pass = "";
var creds_email = "";
var creds_role = "";
var creds_fname = "";
var creds_lname = "";
var creds_dispname = "";
var creds_imgurl = "";
var creds_coverurl = "";
for(var count = 0; count < login_array.length; count++)
{
log = login_array[count][0];
id = login_array[count][1];
creds_login = login_array[count][2];
creds_pass = login_array[count][3];
creds_email = login_array[count][4];
creds_role = login_array[count][5];
creds_fname = login_array[count][6];
creds_lname = login_array[count][7];
creds_dispname = login_array[count][8];
creds_imgurl = login_array[count][9];
creds_coverurl = login_array[count][10];
}
creds.push(log);
creds.push(id);
creds.push(creds_login);
creds.push(password);
creds.push(creds_email);
creds.push(creds_role);
creds.push(creds_fname);
creds.push(creds_lname);
creds.push(creds_dispname);
creds.push(creds_imgurl);
creds.push(creds_coverurl);
if(log == "FALSE")
{
// alert("Wrong Username and Password", null, "Wrong Creds", "Try Again");
myApp.addNotification({
title: 'Login Error',
subtitle: '',
message: 'Wrong login or password',
media: '<i class="trekicons trek-trekfind"></i>'
});
$('#login_loader').css('display', 'none');
$('#login_form').fadeTo('fast', 1, function () {
// Animation complete.
});
}
else if(log == "TRUE")
{
if(id == 0){
//alert("Incorrect feedback");
window.setTimeout(login, 1000);
} else {
//fetch_and_display_posts();
//$("#page_two_link").click();
//$("#testing").html(creds);
window.localStorage.setItem("user.info", JSON.stringify([creds]));
$('#user_disp_name').html(creds_dispname);
$('#login_loader').css('display', 'none');
//$('#login_form').fadeTo('fast', 1, function () {
// Animation complete.
//});
myApp.closeModal('.popup-login');
user_check();
}
}
}
xhr.send();
}