0

I'm trying to login into Joomla from a form in an external angular app and be redirected to the appropriate page in Joomla website for logged users. For that, I used a php script in the root of the Joomla app. I finally can send the credentials of the userX to that script and using cURL I get the token and cookies.

However I'm not redirected to the next page after the successful login.

Note: if I go to the admin panel, it shows that the userX is logged in to site.

I need help, I don't know what is the next step, here is the script I used :

<?php

header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Credentials: true');
header("Access-Control-Allow-Methods: PUT, GET, POST, DELETE");
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
header("Content-Type: application/json; charset=UTF-8");

$postdata = file_get_contents("php://input");
$request = json_decode($postdata);

$uname = $request->username;
$upswd= $request->password;
$url = "http://localhost/joomla/index.php";

$cookie_jar = tempnam('tmp','cookie');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url );
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE );
curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE );
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar);
curl_setopt($ch, CURLOPT_HEADER, TRUE );
$page = curl_exec($ch);

// Catch the cookie and send it to user's browser
preg_match('/^Set-Cookie: (.*?);/m', $page, $m);
$cookie=explode('=',$m[1]);
setcookie($cookie[0], $cookie[1], time() + 86400 * 30 , "/");
print_r($cookie);

// Get data from the form
preg_match_all("(&lt;input type=\"hidden\" name=\"return\" value=\"(.*)\" /&gt;)siU", $page, $matches1);
preg_match_all("(&lt;input type=\"hidden\" name=\"(.*)\" value=\"1\" /&gt;)iU", $page, $matches2);
$return = trim($matches1[1][0]);
$key = trim($matches2[1][0]);
// Send data to the POST request
$postfields = array();
$postfields['username'] = $uname;
$postfields['password'] = $upswd;
$postfields['option'] = 'com_users';
$postfields['task'] = 'user.login';
$postfields['remember'] = "yes";
$postfields['return'] = $return;
$postfields[$key] = '1';
print_r($postfields);

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
$ret = curl_exec($ch);
print_r($ret);

// Catch cookie and send it to user's browser
preg_match('/^Set-Cookie: (.*?);/m', $ret, $m);
$cookie=explode('=',$m[1]);
setcookie($cookie[0], $cookie[1], time() + 86400 * 30 , "/");
curl_close($ch);
print_r($cookie);

if(isset($cookie))
{
    header("Location: http://localhost/joomla/index.php");
    //exit;
}
?>
1
  • It's not clear if you're using Joomla on the server or your own script. If it's Joomla then in response to the user login POST you should get a response which is a redirect, with the redirect URL in the header. The 'return' parameter which you send is the base64-encoded URL of where you should get redirected. If you log in to Joomla on a browser and use the devtools you'll see the messages which go each way. If instead of using Joomla you're communicating with your own script, then it's up to it to send the redirect back. Feb 16 at 20:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.