在另一个 php 脚本中使用 joomla 会计

Posted

技术标签:

【中文标题】在另一个 php 脚本中使用 joomla 会计【英文标题】:using joomla accounting in another php script 【发布时间】:2013-08-15 05:55:10 【问题描述】:

我有一个 Joomla 网站,里面有一个 php 脚本。有一个按钮可以将人们重定向到 PHP 脚本。我想使用我的 Joomla 站点用户组作为访问级别。应该阻止那些没有登录的人访问PHP脚本的index.php,并收到诸如“你必须先登录!”这样的消息 /em> 和那些 登录的用户,可以访问 PHP 脚本的 index.php

我所做的就是在 PHP 脚本的 index.php 顶部包含 Joomla 框架,并放入函数:JFactory::getUser();。之后,我将整个 index.php 置于if-else 条件下。 if的条件是getUser()的返回值不等于“guest”。

else 部分工作正常,并显示消息“您必须先登录”,但 if 部分不起作用,只显示一个空白页,我收到 # p>

error 500 "internal server error"

这意味着我的代码可以识别已登录和未登录的代码。 另外,当我删除我的个人代码时,它会正确地向所有人显示 index.php

请帮帮我


这是代码:

<?php



define( '_JEXEC', 1 );
define( 'DS', DIRECTORY_SEPARATOR );
define('JPATH_BASE', dirname(__FILE__)."/../../../bt-travel/");

require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
require_once ( JPATH_BASE .'/libraries/joomla/factory.php' );



$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();

$user =& JFactory::getUser();

if(!$user->guest)



// *** check if database connection parameters file exists
if(!file_exists('include/base.inc.php'))
    header('location: install.php');
    exit;


## uncomment, if your want to prevent 'Web Page exired' message when use $submission_method = 'post';
// session_cache_limiter('private, must-revalidate');    
// *** set flag that this is a parent file

define('APPHP_EXEC', 'access allowed');


require_once('include/base.inc.php');
require_once('include/connection.php');




// *** call handler if exists
// -----------------------------------------------------------------------------
if((Application::Get('page') != '') && file_exists('page/handlers/handler_'.Application::Get('page').'.php'))
    include_once('page/handlers/handler_'.Application::Get('page').'.php');
else if((Application::Get('customer') != '') && file_exists('customer/handlers/handler_'.Application::Get('customer').'.php'))
    if(Modules::IsModuleInstalled('customers'))    
        include_once('customer/handlers/handler_'.Application::Get('customer').'.php');
    
else if((Application::Get('admin') != '') && file_exists('admin/handlers/handler_'.Application::Get('admin').'.php'))
    include_once('admin/handlers/handler_'.Application::Get('admin').'.php');
else if((Application::Get('admin') == 'export') && file_exists('admin/downloads/export.php'))
    include_once('admin/downloads/export.php');


// *** get site content
// -----------------------------------------------------------------------------
if(!preg_match('/booking_notify_/i', Application::Get('page')))    
    $cachefile = '';
    if($objSettings->GetParameter('caching_allowed') && !$objLogin->IsLoggedIn())
        $c_page        = Application::Get('page');
        $c_page_id     = Application::Get('page_id');
        $c_system_page = Application::Get('system_page');
        $c_album_code  = Application::Get('album_code');
        $c_news_id     = Application::Get('news_id');
        $c_customer    = Application::Get('customer');
        $c_admin       = Application::Get('admin');

        if(($c_page == '' && $c_customer == '' && $c_admin == '') || 
           ($c_page == 'pages' && $c_page_id != '') || 
           ($c_page == 'news' && $c_news_id != '') ||
           ($c_page == 'gallery' && $c_album_code != '')
           )
           
            $cachefile = md5($c_page.'-'.
                             $c_page_id.'-'.
                             $c_system_page.'-'.
                             $c_album_code.'-'.
                             $c_news_id.'-'.
                             Application::Get('lang').'-'.
                             Application::Get('currency_code')).'.cch'; 
            if($c_page == 'news' && $c_news_id != '')
                if(!News::CacheAllowed($c_news_id)) $cachefile = '';
            else
                $objTempPage = new Pages((($c_system_page != '') ? $c_system_page : $c_page_id));
                if(!$objTempPage->CacheAllowed()) $cachefile = '';          
                       
            if(start_caching($cachefile)) exit;
        
    
    require_once('templates/'.Application::Get('template').'/default.php');
    if($objSettings->GetParameter('caching_allowed') && !$objLogin->IsLoggedIn()) finish_caching($cachefile);


Application::DrawPreview();

echo "\n".'<!-- This page was generated by ApPHP Hotel Site v'.CURRENT_VERSION.' -->';

else
echo "you must login first";




?>

【问题讨论】:

能否请您展示您用于包含 Joomla 框架的代码,以及其他自定义代码 sn-ps。没有看到任何东西很难提供答案 这个 joomla 网站:v-uni.ir/bt-travel 这是 php 站点:v-uni.ir/amir/extension/apphp 没有人帮我吗?!! 【参考方案1】:
<?php



define( '_JEXEC', 1 );
define( 'DS', DIRECTORY_SEPARATOR );
define('JPATH_BASE', dirname(__FILE__)."/../../../bt-travel/");

require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
require_once ( JPATH_BASE .'/libraries/joomla/factory.php' );



$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();

$user =& JFactory::getUser();

if(!$user->guest)



// *** check if database connection parameters file exists
if(!file_exists('include/base.inc.php'))
    header('location: install.php');
    exit;


## uncomment, if your want to prevent 'Web Page exired' message when use $submission_method = 'post';
// session_cache_limiter('private, must-revalidate');    
// *** set flag that this is a parent file

define('APPHP_EXEC', 'access allowed');


require_once('include/base.inc.php');
require_once('include/connection.php');




// *** call handler if exists
// -----------------------------------------------------------------------------
if((Application::Get('page') != '') && file_exists('page/handlers/handler_'.Application::Get('page').'.php'))
    include_once('page/handlers/handler_'.Application::Get('page').'.php');
else if((Application::Get('customer') != '') && file_exists('customer/handlers/handler_'.Application::Get('customer').'.php'))
    if(Modules::IsModuleInstalled('customers'))    
        include_once('customer/handlers/handler_'.Application::Get('customer').'.php');
    
else if((Application::Get('admin') != '') && file_exists('admin/handlers/handler_'.Application::Get('admin').'.php'))
    include_once('admin/handlers/handler_'.Application::Get('admin').'.php');
else if((Application::Get('admin') == 'export') && file_exists('admin/downloads/export.php'))
    include_once('admin/downloads/export.php');


// *** get site content
// -----------------------------------------------------------------------------
if(!preg_match('/booking_notify_/i', Application::Get('page')))    
    $cachefile = '';
    if($objSettings->GetParameter('caching_allowed') && !$objLogin->IsLoggedIn())
        $c_page        = Application::Get('page');
        $c_page_id     = Application::Get('page_id');
        $c_system_page = Application::Get('system_page');
        $c_album_code  = Application::Get('album_code');
        $c_news_id     = Application::Get('news_id');
        $c_customer    = Application::Get('customer');
        $c_admin       = Application::Get('admin');

        if(($c_page == '' && $c_customer == '' && $c_admin == '') || 
           ($c_page == 'pages' && $c_page_id != '') || 
           ($c_page == 'news' && $c_news_id != '') ||
           ($c_page == 'gallery' && $c_album_code != '')
           )
           
            $cachefile = md5($c_page.'-'.
                             $c_page_id.'-'.
                             $c_system_page.'-'.
                             $c_album_code.'-'.
                             $c_news_id.'-'.
                             Application::Get('lang').'-'.
                             Application::Get('currency_code')).'.cch'; 
            if($c_page == 'news' && $c_news_id != '')
                if(!News::CacheAllowed($c_news_id)) $cachefile = '';
            else
                $objTempPage = new Pages((($c_system_page != '') ? $c_system_page : $c_page_id));
                if(!$objTempPage->CacheAllowed()) $cachefile = '';          
                       
            if(start_caching($cachefile)) exit;
        
    
    require_once('templates/'.Application::Get('template').'/default.php');
    if($objSettings->GetParameter('caching_allowed') && !$objLogin->IsLoggedIn()) finish_caching($cachefile);


Application::DrawPreview();

echo "\n".'<!-- This page was generated by ApPHP Hotel Site v'.CURRENT_VERSION.' -->';

else
echo "you must login first";




?>

【讨论】:

对不起,我无法巩固它

以上是关于在另一个 php 脚本中使用 joomla 会计的主要内容,如果未能解决你的问题,请参考以下文章

php Joomla 3,2的CLI脚本示例

joomla 自定义表单注销用户

Joomla 组件:在视图中包含/需要 php

Joomla 2.5 从外部脚本获取用户数据

php 简单的PHP脚本来解析Wordpress发布xml导出以获取相对URL并为Joomla做准备! com_redirects,注意使用带有W的J2XML

php 示例导出脚本,用于将MODX资源转换为CSV条目,以便在另一个站点中导入。