[英]include Jfactory class in an external php file, Joomla
I am writing a module for Joomla, at this point I really need to be able to connect to the database using Jfactory. 我正在为Joomla编写一个模块,此时我真的需要能够使用Jfactory连接到数据库。 Normally one could simply use
$db = JFactory::getDBO();
通常可以使用
$db = JFactory::getDBO();
, but the PHP error tells me the JFactory class is not included. ,但PHP错误告诉我不包括JFactory类。 So now I need to know how to include this JFactory class .
所以现在我需要知道如何包含这个JFactory类 。 I've tried a couple of suggestions found on the internet, absent success, yet.
我已经尝试了在互联网上找到的一些建议,但没有成功。 This is the code (it works perfect on standalone)
这是代码(它在独立时工作得很好)
<?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 :)
谢谢 :)
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();
Recently, the libraries/joomla/factory.php
has been removed, which was causing all the scripts that utilized require_once ( JPATH_BASE .DS.'libraries'.DS.'joomla'.DS.'factory.php' );
最近,
libraries/joomla/factory.php
已被删除,这导致所有使用require_once ( JPATH_BASE .DS.'libraries'.DS.'joomla'.DS.'factory.php' );
的脚本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...
由于这仍然在这里被接受和最受欢迎的答案中使用,现在是时候进行更新......
You don't need the JDatabaseFactory
class anymore to use Joomla database functions through JFactory::getDBO();
您不再需要
JDatabaseFactory
类来通过JFactory::getDBO();
使用Joomla数据库函数JFactory::getDBO();
as the JFactory
class provides them and gets already included. 因为
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 eg this again: 你将能够再次这样做:
$db =& JFactory::getDBO();
$query = "SELECT ...";
$db->setQuery($query);
$db->query();
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.