Php 如何通过外部脚本登录joomla?

Php 如何通过外部脚本登录joomla?,php,joomla,joomla1.5,Php,Joomla,Joomla1.5,我们的站点上有一个独立的脚本,与Joomla 1.5安装相邻。我们正在使用Joomla身份验证来限制对脚本的访问。此时,我们正在将任何未经授权的用户重定向到Joomla站点进行登录。不过,我们希望在脚本中添加登录功能。有人知道如何使用用户名/密码从外部脚本登录joomla吗?谢谢 我建议以下解决方案之一: 为脚本编写一个特定的脚本 在脚本中使用CURL在普通登录表单上执行POST请求(CURL也可以处理cookies) (最简单):不要通过Joomla进行身份验证!,但是通过.htaccess

我们的站点上有一个独立的脚本,与Joomla 1.5安装相邻。我们正在使用Joomla身份验证来限制对脚本的访问。此时,我们正在将任何未经授权的用户重定向到Joomla站点进行登录。不过,我们希望在脚本中添加登录功能。有人知道如何使用用户名/密码从外部脚本登录joomla吗?谢谢

我建议以下解决方案之一:

  • 为脚本编写一个特定的脚本
  • 在脚本中使用CURL在普通登录表单上执行POST请求(CURL也可以处理cookies)
  • (最简单):不要通过Joomla进行身份验证!,但是通过.htaccess
      对于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');
      }
      

      我已经完成了这项工作,而且工作正常。
      假设您的自定义登录脚本是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 );
      
      ?>

      这就是——如果两个密码匹配,check password将返回true。
      注意:您需要编写一个查询来自动检查数据库。

      在Joomla 3.9中,建议使用此代码

      1-将此脚本上载到根文件夹。Ie public_html或htdocs

      2-更改您的用户名和密码

      3-在浏览器中运行脚本。您将在“站点而非管理员”部分自动登录(您可以更改此设置)

      4-打开受保护的页面

      <?php
      
      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/database/factory.php');
      
      
      $result_login = JFactory::getApplication()->login(
                          [
                              'username' => 'demo',
                              'password' => 'demo'
                          ],
                          [
                              'remember' => true,
                              'silent'   => true
                          ]
                      );
                      
      if ($result_login==1) echo 'Login Successful'; else echo 'Invalid Login';
                      
      

      完美!这正是我想要的。
      $error=$mainframe->login($credentials)是错误的;应该看起来像
      $result=$mainframe->login($credentials)工作于Joomla 3.4.1如果用户已在此应用程序中使用其他方法进行身份验证,则$credentials不可用。仍然可以启动joomla会话吗?需要一次(JPATH_BASE.'/libraries/joomla/factory.php');这将导致joomla 3.9.18中出现错误。您可以删除这一行,它将正常工作。