How to use web services to share Joomla login sess

2019-07-09 00:21发布

This is file which is use to login on joomla site.

// a super-stripped down 2-leg oauth server/client example
function getLogin($userid, $psswrd) {
    $app = JFactory::getApplication();
    jimport('joomla.user.authentication');
    jimport('joomla.session.session');
    $auth = &JAuthentication::getInstance();
    $session = &JFactory::getSession();
    $session->set('name', "value");
    $ssn_name = $session->get('name');
    $sessionDetails = array(
            'State' => $session->getState(),
            'Expire' => session->getExpire(),
            'Token' => $session->getToken(),
            'FormToken' => $session->getFormToken(),
            'Name' => $session->getName(),
            'Id' => $session->getId(),
            'getStores' => $session->getStores(),
            'isNew' => $session->isNew());

    $username = $userid;
    $password = $psswrd;
    $credentials = array(
            'username' => $username,
            'password' => $password);
    $options = array();
    $response = $auth->authenticate($credentials, $options);

    if ($response->status == JAUTHENTICATE_STATUS_SUCCESS) {
        $response->status = true;

        $sessionDetails['loginStatus'] = $loginStatus = $app->login($credentials, $options);

        return $sessionDetails;
    }
    else {
        $response->status = false;
        return 'testFalse';
    }
}

Now we are calling this using localhost by

$client = new nusoap_client("http://domain-name/site-name/nusoap/remoteLogin.php");

$error = $client->getError();
if ($error) {
    echo "<h2>Constructor error</h2><pre>" . $error . "</pre>";
}

$result = $client->call("getLogin", array(
        "userid" => "admin",
        "password" => "test"));

After this session is created at above project and update it in database with new entry. But still not login on site. Can anyone help me.

Thanks

2条回答
来,给爷笑一个
2楼-- · 2019-07-09 00:39

Ok, as Brent Friar posted and elin cited, you need to

  • Create a new session and get the associated token
  • Pass the username, password, and token to create a logged in session
  • Get the new cookie values for logged in session
  • Transfer cookie to the browser

Here is the code needed to accomplish all of this:

<?php
$uname = $_POST['username'];
$upswd = $_POST['password'];
$url = "http://joomla website.com";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE );
curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE );
curl_setopt($ch, CURLOPT_COOKIEJAR, realpath('./cookie.txt'));
curl_setopt($ch, CURLOPT_COOKIEFILE, realpath('./cookie.txt'));
curl_setopt($ch, CURLOPT_HEADER, TRUE );
$ret = curl_exec($ch);
if (!preg_match('/name="([a-zA-z0-9]{32})"/', $ret, $spoof)) {
    preg_match("/name='([a-zA-z0-9]{32})'/", $ret, $spoof);
}

// POST fields
$postfields = array();
$postfields['username'] = urlencode($uname);
$postfields['passwd'] = urlencode($upswd);
$postfields['option'] = 'com_user';
$postfields['task'] = 'login';
$postfields[$spoof[1]] = '1';
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
$ret = curl_exec($ch);

// Get logged in cookie and pass it to the browser
preg_match('/^Set-Cookie: (.*?);/m', $ret, $m);
$cookie=explode('=',$m[1]);
setcookie($cookie[0], $cookie[1]);
?>

This should work on any Joomla website as long as the URL used in the script has a login form on the page. Once you run this script you should then be able to access the website and be logged in.

查看更多
贼婆χ
3楼-- · 2019-07-09 00:43

Hi @Devjohn please check my url you can test your local simple how to soap access remote.

you can find here my bolg to you problem i think it may help you

http://phptechnicalgroups.blogspot.in/2012/10/json-return-using-soap-server-and-soap.html

查看更多
登录 后发表回答