如何使用外部PHP脚本获取当前Joomla用户

Joe*_*ams 5 php authentication joomla external-script

我有一些用于AJAX查询的PHP脚本,但我希望它们能够在Joomla的身份验证系统的保护下运行.以下是安全的吗?有没有不必要的线路?

joomla-auth.php(与Joomla的index.php位于同一目录中):

<?php

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

/* Create the Application */
$mainframe =& JFactory::getApplication('site');

/* Make sure we are logged in at all. */
if (JFactory::getUser()->id == 0)
    die("Access denied: login required.");

?>
Run Code Online (Sandbox Code Playgroud)

test.php的:

<?php

include 'joomla-auth.php';

echo 'Logged in as "' . JFactory::getUser()->username . '"';

/* We then proceed to access things only the user
   of that name has access to. */
?>
Run Code Online (Sandbox Code Playgroud)

jll*_*anc 2

虽然我在代码中没有看到任何不安全的内容,但最好对标准 Joomla 组件进行 AJAX/JSON 调用。这里有一篇关于如何执行此操作的好文章: http: //blog.syncleon.com/2009/05/ajax-ify-your-joomla-website.html我还写了有关 JavaScript、Joomla 和异步请求的文章我的书http://www.packtpub.com/files/learning-joomla-1-5-extension-development-sample-chapter-8-using-javascript-effects.pdf(跳到第168页)。

本质上,您所做的就是为 AJAX 调用的输出创建一个视图,然后创建一个 view.xml.php(或 view.json.php)文件而不是 view.html.php。当您添加&format=xml到请求 URL 的末尾时,它将从 view.xml.php 而不是 view.html.php 中提取。