Joomla 3身份验证以访问PHP中的外部应用程序

问题描述:

我发现这个非常有用的脚本来检查Joomla中的身份验证。Joomla 3身份验证以访问PHP中的外部应用程序

Joomla 3 External authentication script

<?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'); 
// JFactory 
require_once (JPATH_BASE .'/libraries/joomla/factory.php'); 
// Hardcoded for now 
$credentials['username'] = 'adam'; 
$credentials['password'] = 'adam'; 
/** 
* Code adapted from plugins/authentication/joomla/joomla.php 
* 
* @package  Joomla.Plugin 
* @subpackage Authentication.joomla 
* 
* @copyright Copyright (C) 2005 - 2014 Open Source Matters, Inc. All rights reserved. 
* @license  GNU General Public License version 2 or later; see LICENSE.txt 
*/ 
// 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!'; 
    } 
    else 
    { 
     // Invalid password 
     // Prmitive error handling 
     die('Invalid password'); 
    } 
} else { 
    // Invalid user 
    // Prmitive error handling 
    die('Cound not find user in the database'); 
} 
?> 

我想用这个来得到我的Joomla网站(域名/ index.php文件)用户认证在PHP应用程序我编码(域名/应用/索引.PHP) 它完美,但harcoded部分是一个问题,我不知道如何动态获取取决于用户这些变量:

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

有谁会对我怎么能提示或解决方案做这个? 提前谢谢!

最后,我可以得到我想要的。 我不停的脚本上半年:

<?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'); 
?> 

,并在我的应用程序恢复用户ID JFactory::getUser()->id。然后,我只需从此ID创建我的变量$_userID = JFactory::getUser()->id

有很多方法可以做到这一点! :-) 例如,您可以使用此代码创建一个class,并使用一种方法使用硬编码用户信息的参数进行调用。

但我认为有一个更好的方法来做到这一点。你不需要加载整个Joomla!框架来验证用户名和密码。的Joomla!使用默认的PHP函数password_hashpassword_verify。你可以做的是:

  1. 使用$_POST左右
  2. 执行SELECT查询到的Joomla收集登录表单的用户信息!用户表获取用户名和相应的密码。例如。 SELECT username, password FROM #__users WHERE username = '...'
  3. 验证收到的密码password_verify($_POST['password'], $data['password'])。如果它是正确的,则用户登录成功,否则返回消息。

更多信息请参见:

你可能要问自己的另一件事是:我真的需要使用相同凭证的分离应用作为Joomla !.如果应用程序是你可以集成在Joomla!你也可以创建你自己的组件。在某些情况下,这更好。

编辑:我忘了一个重要说明。这仅适用于使用passsword_hash生成的密码。因此,如果由于旧的PHP版本(更新你的东西!!!)而导致网站无法使用这个功能,则可以使用md5和salt作为后备。在这种情况下,password_verify将不起作用。

+0

谢谢您花时间回答我的问题。 事实是,我不希望用户登录两次(一次在joomla上,一次在应用程序上)。我想保持我的域名和子域名之间的身份验证。 – Nephtys