2

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

smart developer
  • 224
  • 2
  • 14
  • 1
    Authentication and login are two different steps. You need to authenticate first, which you are doing. Then you need to do the login work. Look at the Joomla user plugin. – Elin Oct 24 '13 at 07:32
  • But in first code i have authenticate and then login . But its not working from another server file which is 2nd code file.code-1 is for site for which i have to login from remote site. code-2 for remote site. – smart developer Oct 25 '13 at 06:25
  • Are you doing everything else the the Joomla user plugin is doing https://github.com/joomla/joomla-cms/blob/master/plugins/user/joomla/joomla.php#L172? I don't think so, for example i don't don't see you putting the data into the session, which is something that the plugin does. Or are you using the joomla plugin? – Elin Oct 26 '13 at 10:16
  • please have a look to this http://stackoverflow.com/questions/5176142/logging-in-to-joomla-1-5-using-external-form-not-within-joomla-folder-but-on-s/5207806#5207806 – Esam Naas Nov 04 '13 at 12:18

2 Answers2

1

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

Bikash Ranjan
  • 136
  • 1
  • 12
1

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.