I have a custom page that checks if cUser->id is zero and, if it is, I pop up a login form. Now I can't seem to find any documentation on how to remotely log in to Joomla. Any ideas?
1 Answer
Found a solution that works for 3.x and up:
in PHP file that gets called through $.ajax:
define('_JEXEC', 1);
define('JPATH_BASE', '../../../');
defined('DS') or define('DS', DIRECTORY_SEPARATOR);
require_once ( JPATH_BASE . 'includes' . DS . 'defines.php' );
require_once ( JPATH_BASE . 'includes' . DS . 'framework.php' );
// connect to Joomla
$mainframe = & JFactory::getApplication('site');
jimport('joomla.plugin.helper');
$error = $mainframe->login([
'username' => $login_username,
'password' => $login_password,
]);
$user = JFactory::getUser();
echo $user->id
In Javascript file:
$("#submit-button").click(login);
function login(){
$.ajax({
method: "POST",
url: "/my/path/to/php",
data: {
login_username: $("input[name=login_username]").val(),
login_password: $("input[name=login_password]").val()
}
}).done(function(data){
if(data != "0"){
// success
} else {
// failed
}
});
}