我們在我們的網站上有一個獨立的腳本,與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