2010-01-15 23 views
8

Sitemizde, Joomla 1.5 kurulumunun bitişiğinde bulunan bağımsız bir komut dosyası var. Komutumuza erişimi kısıtlamak için Joomla kimlik doğrulamasını kullanıyoruz. Bu noktada, herhangi bir yetkisiz kullanıcıyı giriş yapmak için Joomla sitesine yönlendiriyoruz. Yine de bizim betikimizde bir giriş yeteneği eklemek istiyoruz. Bir kullanıcı adı/şifre kullanarak harici bir komut dosyasından joomla'ya nasıl giriş yapacağını bilen var mı? Teşekkürler!Bir dış komut dosyası aracılığıyla joomla'ya nasıl giriş yapılır?

cevap

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

Mükemmel! Tam olarak aradığım şey bu. – user77413

+0

'$ error = $ mainframe-> login ($ credentials);' yanılıyor; '$ Result = $ mainframe-> giriş ($ kimlik) gibi görünmelidir; kimlik bilgileri daha sonra $, Joomla 3.4.1 Kullanıcı zaten başka yöntemle bu uygulamada doğrulanır ne olur – simon

1

Aşağıdaki çözümlerden birini öneririm:

  • bir login plugin belirli için komut dosyası yazın. (. CURL de çerezleri başa çıkabilir) Normal giriş formunun bir SONRASI İsteği yapmak için komut CURL kullanarak
  • (En basit): Joomla tarafından kimlik doğrulaması !, ama .htaccess tarafından vermeyin. Joomla 3.x için
6

aşağıda daha temiz ve yararlı olur. Aşağıdaki kodlar kodlanmış kullanıcı adı ve şifreyi doğrulayacaktır. Kullanıcı varsa, index.php sayfasına yönlendirilir.

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

kullanılamaz. Joomla oturumu başlatmak hala mümkün mü? –

+0

Çalıştığı' – frthjf

1

Bunu yaptım ve gayet iyi çalışıyor. varsayarsak özel giriş komut true dönecektir şifreyi kontrol --- sen orada php kod parçacığı

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

?>

Ve İşte

-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

olduğu iki şifre eşleşir. Not: Veritabanından otomatik olarak kontrol etmek için bir sorgu yazmanız gerekiyor.

İlgili konular