在外部 php 文件 Joomla 中包含 Jfactory 类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9459430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 06:52:13  来源:igfitidea点击:

include Jfactory class in an external php file, Joomla

phpmysqljoomla

提问by Maarten Hartman

I am writing a module for Joomla, at this point I really need to be able to connect to the database using Jfactory. Normally one could simply use $db = JFactory::getDBO();, but the PHP error tells me the JFactory class is not included. So now I need to know how to include this JFactory class. I've tried a couple of suggestions found on the internet, absent success, yet. This is the code (it works perfect on standalone)

我正在为 Joomla 编写一个模块,此时我真的需要能够使用 Jfactory 连接到数据库。通常可以简单地使用$db = JFactory::getDBO();,但 PHP 错误告诉我不包括 JFactory 类。所以现在我需要知道如何包含这个 JFactory 类。我已经尝试了在互联网上找到的一些建议,但没有成功。这是代码(它在独立运行时完​​美无缺)

    <?php
// server info
$server = 'localhost';
$user = 'ss';
$pass = 'oo';
$db = 'ss';

$connection = mysql_connect($server, $user, $pass)  or die ("Could not connect to server ... \n" . mysql_error ());
 mysql_select_db($db) or die ("Could not connect to database ... \n" . mysql_error ());


if(isSet($_POST['username']))
{
$username = $_POST['username'];
$username = mysql_real_escape_string($username);
$sql_check = mysql_query("SELECT Username FROM users WHERE Username='$username'");

if(mysql_num_rows($sql_check))
{
echo '<font color="#cc0000"><STRONG>'.$username.'</STRONG> is already in use.</font>';
}
else
{
echo 'OK';
}

}

?>

I hope my problem is clear to you. Your help will be much appreciated.

我希望我的问题对你来说很清楚。您的帮助将不胜感激。

Attempt 1

尝试 1

<?php

include('../../../../configuration.php');

include('../../../../libraries/joomla/factory.php');

$config =& JFactory::getConfig();

// server info
$server2 = $host;
$user2 = $user;
$pass2 = $password;
$db2 = $db;

$connection = mysqli_connect($server2, $user2, $pass2)  or die ("Could not connect to server ... \n" . mysqli_error ());
 mysqli_select_db($db2) or die ("Could not connect to database ... \n" . mysqli_error ());


if(isSet($_POST['username']))
{
$username = $_POST['username'];
$username = mysql_real_escape_string($username);
$sql_check = mysql_query("SELECT username FROM #__users WHERE username='$username'");

if(mysql_num_rows($sql_check))
{
echo '<font color="#cc0000"><STRONG>'.$username.'</STRONG> is already in use.</font>';
}
else
{
echo 'OK';
}

}
?>

but this is not working either.

但这也不起作用。

attempt 2 (successful)

尝试2(成功)

include('../../../../configuration.php');
$jc = new JConfig();
$table = 'users';
$users = $jc->dbprefix . $table;
// connect to the database
$mysqli = new mysqli($jc->host, $jc->user, $jc->password, $jc->db);

Now it is all working as I want. The only thing is now: safety. I'm not too sure about this being hacker proof. Can someone review this? thanks :)

现在一切都如我所愿。现在唯一的问题是:安全。我不太确定这是黑客证明。有人可以这个吗?谢谢 :)

回答by stef

I'm sure that you figure it out, but maybe it would be useful for someone else

我相信你已经弄清楚了,但也许它对其他人有用

To use joomla database class (even if you know that is not recommended :) ) you need, first to define three constants, like:

要使用 joomla 数据库类(即使您知道不推荐使用 :) ),您需要先定义三个常量,例如:

define( '_JEXEC', 1 );
define( 'DS', DIRECTORY_SEPARATOR );
define( 'JPATH_BASE', $_SERVER[ 'DOCUMENT_ROOT' ] );

Then you need to include three files, like:

然后你需要包含三个文件,比如:

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' );
$mainframe =& JFactory::getApplication('site');

EDIT

编辑

You can include only two files like:

您只能包含两个文件,例如:

define( 'JPATH_BASE', $_SERVER[ 'DOCUMENT_ROOT' ] ); // define JPATH_BASE on the external file
require_once( JPATH_BASE . DS . 'libraries' . DS . 'import.php' ); // framework
require_once( JPATH_BASE . DS . 'configuration.php' ); // config file

Finally use joomla class, like:

最后使用 joomla 类,例如:

$db = JFactory::getDBO();

回答by Esa Jokinen

Recently, the libraries/joomla/factory.phphas been removed, which was causing all the scripts that utilized require_once ( JPATH_BASE .DS.'libraries'.DS.'joomla'.DS.'factory.php' );to fail. As this is still used in the accepted & most upvoted answer here, it's time to an update...

最近,libraries/joomla/factory.php已被删除,这导致所有使用的脚本require_once ( JPATH_BASE .DS.'libraries'.DS.'joomla'.DS.'factory.php' );失败。由于这里仍然在接受和最受好评的答案中使用它,是时候更新了...

You don't need the JDatabaseFactoryclass anymore to use Joomla database functions through JFactory::getDBO();as the JFactoryclass provides them and gets already included.

您不再需要JDatabaseFactory该类来使用 Joomla 数据库功能,JFactory::getDBO();因为JFactory该类已提供它们并已包含在内。

These five lines are enough:

这五行就足够了

define('_JEXEC', 1);
define('JPATH_BASE', dirname(__FILE__));
define('DS', DIRECTORY_SEPARATOR);
require_once JPATH_BASE.DS.'includes'.DS.'defines.php';
require_once JPATH_BASE.DS.'includes'.DS.'framework.php';

You will be able to do e.g. this again:

您将能够再次执行此操作:

$db =& JFactory::getDBO();
$query = "SELECT ...";
$db->setQuery($query);
$db->query();

回答by yogeshK

For accessing Database information of Joomla ,You should include("configuration.php").This joomla configuaraion file contains all information of database access.

访问Joomla的数据库信息,你应该包含("configuration.php")。这个joomla configuaraion文件包含了所有的数据库访问信息。

class JFactory defined in this folder "joomlaRootFolder/libraries/joomla/factory.php"

在此文件夹“joomlaRootFolder/libraries/joomla/factory.php”中定义的类 JFactory