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

Joomla 3 authentication to get access to external app in PHP

我发现这个非常有用的脚本可以检查 Joomla 中的身份验证。

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 网站 (domain_name/index.php) 上为 PHP 中的应用程序获取用户身份验证我正在编码 (domain_name/app/index.php) 它工作得很好,但是硬编码部分是个问题,我不知道如何根据用户动态获取这些变量:

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

有没有人对我如何做到这一点有任何提示或解决方案? 提前致谢!

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

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

  1. 使用$_POST左右
  2. 收集登录表单的用户信息
  3. 对 Joomla! 执行 select 查询users table 获取用户名和通讯员密码。例如。 SELECT username, password FROM #__users WHERE username = '...'
  4. 验证收到的密码password_verify($_POST['password'], $data['password'])。如果正确,则用户登录成功,否则 return 一条消息。

查看更多信息:

您可能想问自己的另一件事是:我真的需要一个使用与 Joomla! 相同凭据的独立应用程序吗?如果您可以将应用程序集成到 Joomla!您也可以创建自己的组件。在某些情况下,这样更好。

编辑: 我忘记了一个重要说明。这仅在使用 passsword_hash 生成密码时有效。因此,如果该站点由于旧 PHP 版本而无法使用它(更新你的东西!!!),则使用 md5 和 salt 作为后备。在那种情况下 password_verify 将不起作用。

终于可以如愿以偿了。 我保留了脚本的前半部分 :

<?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.

创建我的变量