![](/img/trans.png)
[英]Get username of current logged-in user of joomla using external php script
[英]How to get current Joomla user with external PHP script
我有一些用於AJAX查詢的PHP腳本,但我希望它們能夠在Joomla的身份驗證系統的保護下運行。 以下是安全的嗎? 有沒有不必要的線路?
<?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.");
?>
<?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. */
?>
雖然我在代碼中看不到任何不安全的東西,但最好將AJAX / JSON調用到標准的Joomla組件。 這里有一篇關於如何做到這一點的好文章: 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中提取。
確實有效,你需要為用戶獲取會話數據
jimport( 'joomla.session.session' );
$session =& JFactory::getSession();
打印會話以查看結果
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.