2010-01-15 2 views
8

우리 사이트에는 Joomla 1.5 인스톨레이션 옆에 독립 실행 형 스크립트가 있습니다. 우리는 Joomla 인증을 사용하여 스크립트에 대한 액세스를 제한하고 있습니다. 이 시점에서 승인되지 않은 사용자를 Joomla 사이트로 리디렉션하여 로그인합니다. 스크립트에 로그인 기능을 추가하려고합니다. 누구든지 외부 스크립트에서 사용자 이름/암호를 사용하여 joomla에 로그인하는 방법을 알고 있습니까? 감사!외부 스크립트를 통해 joomla에 로그인하는 방법?

답변

13
<?php 
//http://domain.com/script/script.php?username=username&passwd=password 

define('_JEXEC', 1); 
define('JPATH_BASE', '../'); 
define('DS', DIRECTORY_SEPARATOR); 
require_once('../configuration.php'); 
require_once (JPATH_BASE .DS.'includes'.DS.'defines.php'); 
require_once (JPATH_BASE .DS.'includes'.DS.'framework.php'); 
require_once (JPATH_BASE .DS.'libraries'.DS.'joomla'.DS.'factory.php'); 

/* Create the Application */ 
$mainframe =& JFactory::getApplication('site'); 
jimport('joomla.plugin.helper'); 

$credentials = array(); 
$credentials['username'] = JRequest::getVar('username', '', 'method', 'username'); 
$credentials['password'] = JRequest::getVar('passwd', '', 'method', 'passwd'); 

//perform the login action 
$error = $mainframe->login($credentials); 
$user = JFactory::getUser(); 
//now you are logged in 

$mainframe->logout(); 
//now you are logged out 
+0

완벽! 그게 바로 제가 찾던 것입니다. – user77413

+0

'$ error = $ mainframe-> login ($ credentials);은 잘못되었습니다; '$ 결과 = $ 메인 프레임> 로그인 ($ 자격 증명)과 같이한다 자격 증명 한 후 $, 줌라 3.4.1 사용자가 이미 다른 방법이 응용 프로그램에 인증 된 경우는 어떻게 – simon

1

나는 다음과 같은 솔루션 중 하나를 제안 :

  • login plugin 특정에 스크립트를 작성합니다. (. CURL도 쿠키를 대처할 수) 정상적인 로그인 폼에서 POST-요청을 수행하는 스크립트에서 CURL을 사용
  • (간단한) : 줌라하여 인증!하지만 htaccess로하여하지 마십시오. 줌라 3.x를 들어
6

은 아래에서 더 깨끗하고 도움이됩니다. 아래의 코드는 하드 코딩 된 사용자 이름과 비밀번호를 확인합니다. 사용자가 존재하면 index.php 페이지로 리디렉션됩니다.

<?php 
/** 
* Joomla! External authentication script 
* 
* @author vdespa 
* Version 1.0 
* 
* Code adapted from /index.php 
* 
* @package Joomla.Site 
* 
* @copyright Copyright (C) 2005 - 2014 Open Source Matters, Inc. All rights reserved. 
* @license GNU General Public License version 2 or later; see LICENSE.txt 
*/ 

if (version_compare(PHP_VERSION, '5.3.1', '<')) 
{ 
    die('Your host needs to use PHP 5.3.1 or higher to run this version of Joomla!'); 
} 

/** 
* Constant that is checked in included files to prevent direct access. 
* define() is used in the installation folder rather than "const" to not error for PHP 5.2 and lower 
*/ 
define('_JEXEC', 1); 

if (file_exists(__DIR__ . '/defines.php')) 
{ 
    include_once __DIR__ . '/defines.php'; 
} 

if (!defined('_JDEFINES')) 
{ 
    define('JPATH_BASE', __DIR__); 
    require_once JPATH_BASE . '/includes/defines.php'; 
} 

require_once JPATH_BASE . '/includes/framework.php'; 

// Instantiate the application. 
$app = JFactory::getApplication('site'); 
jimport('joomla.plugin.helper'); 

// JFactory 
require_once (JPATH_BASE .'/libraries/joomla/factory.php'); 


// Hardcoded for now 
$credentials['username'] = 'admin'; 
$credentials['password'] = 'admin'; 


// Get a database object 
$db = JFactory::getDbo(); 
$query = $db->getQuery(true) 
    ->select('id, password') 
    ->from('#__users') 
    ->where('username=' . $db->quote($credentials['username'])); 

$db->setQuery($query); 
$result = $db->loadObject(); 

if ($result) 
{ 
    $match = JUserHelper::verifyPassword($credentials['password'], $result->password, $result->id); 

    if ($match === true) 
    { 
     // Bring this in line with the rest of the system 
     $user = JUser::getInstance($result->id); 

     echo 'Joomla! Authentication was successful!' . '<br>'; 
     echo 'Joomla! Token is:' . JHTML::_('form.token'); 

    //perform the login action 
    $error = $app->login($credentials); 
    $logged_user = JFactory::getUser(); 
    var_dump($logged_user); 
    //redirect logged in user 
    $app->redirect('index.php'); 
    } 
    else 
    { 
     // Invalid password 
     // Prmitive error handling 
     echo 'Joomla! Token is:' . JHTML::_('form.token') . '<br>'; 
     die('Invalid password'); 
    } 
} else { 
    // Invalid user 
    // Prmitive error handling 
    die('Cound not find user in the database'); 
} 
+0

을 사용할 수 없습니다. joomla 세션을 시작할 수 있습니까? –

+0

에서 근무' – frthjf

1

나는 이것을 잘 수행했다. 가정 사용자 정의 로그인 스크립트는 경우에 true를 돌려줍니다 비밀번호를 확인 --- 당신이 거기의 PHP 코드

$phpass = new PasswordHash(10, true); 
$password= "unhashed user password"; 
$db_password = 'Your hashed password in the database'; 
$ok= $phpass->CheckPassword($password, $db_password); 

?>

그리고 여기

-Go to Joomla installation directory 
-Copy PasswordHash.php from this directory /root/libraries/phpass/ to your external script's folder 
-Include the PasswordHash.php in login.php 
-Create an instance of PasswordHash like this: 

login.php

입니다 두 개의 암호가 일치합니다. NB : 데이터베이스에서 자동으로 확인하는 쿼리를 작성해야합니다.