简体   繁体   English

使用Jquery Ajax获取Joomla user_id

[英]Get Joomla user_id using a Jquery Ajax

I have the following PHP script file.php in my Joomla site: 我的Joomla网站中有以下PHP脚本file.php:

$user = JFactory::getUser();
$usr_id = $user->get('id');

If I run it directly, using in HTML: 如果我直接运行它,则在HTML中使用:

include_once "file.php";

It will get the user id, no problem. 它将获得用户ID,没问题。

However, if run it through an Ajax request: 但是,如果通过Ajax请求运行它:

$.ajax({
    url: "other.php",
...

Where other.php is: 其中other.php是:

include_once "file.php";

I get the error: 我得到错误:

Fatal error:  Class 'JFactory' not found in file.php on line 3

Why? 为什么? Any help!? 任何帮助!?

You need to import the Joomla library in order to use the JFactory class. 您需要导入Joomla库才能使用JFactory类。 To import the library, add the following to the top of your file: 要导入库,请在文件顶部添加以下内容:

define( '_JEXEC', 1 );
define( 'JPATH_BASE', dirname(__FILE__) ).'/../..' );

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM