在外部 php 文件 Joomla 中包含 Jfactory 类 [英] include Jfactory class in an external php file, Joomla

查看:20
本文介绍了在外部 php 文件 Joomla 中包含 Jfactory 类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)

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

$connection = mysql_connect($server, $user, $pass)  or die ("Could not connect to server ... 
" . mysql_error ());
 mysql_select_db($db) or die ("Could not connect to database ... 
" . 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.

尝试 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 ... 
" . mysqli_error ());
 mysqli_select_db($db2) or die ("Could not connect to database ... 
" . 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';
}

}
?>

但这也不起作用.

尝试 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

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

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

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');

编辑

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

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

最后使用 joomla 类,如:

Finally use joomla class, like:

$db = JFactory::getDBO();

这篇关于在外部 php 文件 Joomla 中包含 Jfactory 类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆