我们在我们的网站上有一个独立的脚本,与Joomla 1.5安装相邻。我们使用Joomla身份验证来限制对我们脚本的访问。此时,我们正在将任何未经授权的用户重定向到Joomla站点进行登录。但是,我们希望在脚本中添加登录功能。有谁知道如何使用用户名/密码从外部脚本登录到joomla?谢谢!如何通过外部脚本登录joomla?
8
A
回答
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
1
我建议以下解决方案之一:
- 写login plugin具体到你的脚本。
- 在脚本中使用curl做正常的登录表单上的POST请求(CURL可以应付饼干,太。)
- (最简单):不要通过认证的Joomla!但通过.htaccess进行。下面
6
为Joomla 3.x的是更干净,乐于助人。以下代码将验证硬编码的用户名和密码。如果用户存在,它将被重定向到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 3.4.1上工作 – 2015-08-17 21:47:06
+0
如果用户已经使用另一种方法在此应用程序中进行身份验证,那么$凭证不可用。是否可以启动joomla会话? – frthjf 2016-06-16 02:34:48
1
我已经完成了它,它的工作很好。 假设您的自定义登录脚本是login.php中
-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:
这里是PHP代码片段
$phpass = new PasswordHash(10, true);
$password= "unhashed user password";
$db_password = 'Your hashed password in the database';
$ok= $phpass->CheckPassword($password, $db_password);
?>
而且有你---检查密码是否将返回true两个密码匹配。 注意:您需要编写一个查询来自动从数据库中检查。
相关问题
- 1. Joomla从外部脚本自动登录
- 2. 从com_jumi(或其他外部(joomla)脚本)登录joomla的用户
- 3. 如何通过php脚本登录roundcube?
- 4. 通过外部脚本
- 5. 通过外部脚本
- 6. 通过脚本CURL登录到Joomla网站
- 7. Joomla - 通过脚本登录来宾用户
- 8. joomla管理员通过php登录脚本错误的令牌
- 9. 外部管理登录脚本?
- 10. 通过外部网站登录到Moodle
- 11. Joomla! 2.5.19外部用户登录
- 12. 在Joomla的外部登录验证
- 13. 在外部登录用户joomla php
- 14. 通过脚本的WordPress登录
- 15. 通过Perl脚本登录到Jenkins
- 16. Bash脚本通过SSH登录
- 17. 通过PHP脚本的Roundcube登录
- 18. 的Joomla登录简单的脚本
- 19. 使用Joomla 2.5登录脚本
- 20. 如何绕过我的登录脚本?
- 21. 使用外部php脚本获取joomla当前登录用户的用户名
- 22. 通过外部动作脚本文件
- 23. 通过脚本更改SVN外部?
- 24. 如何通过python脚本读取用户登录密码?
- 25. 如何通过php脚本在Twitter上登录?
- 26. 通过一个脚本登录到另一个脚本
- 27. 如何通过URLFetch使用Google App脚本调用外部API
- 28. 如何通过数组ZABBIX外部脚本
- 29. 如何通过send-event.pl脚本发送外部事件?
- 30. 如何通过cUrl登录?
完美!这正是我所期待的。 – user77413 2010-02-18 18:59:13
'$ error = $ mainframe-> login($ credentials);'是错误的;应该看起来像'$ result = $ mainframe-> login($ credentials);' – simon 2016-04-11 18:54:29