简体   繁体   English

在PHP中找不到Joomla会话变量

[英]Joomla session variable not found in a php

I am setting a session variable in an Article: 我在文章中设置会话变量:

 $session->set('test' , 'testing' ); 

As long as I stay within other Joomla modules and articles I can read it without problem. 只要停留在其他Joomla模块和文章中,我都可以毫无问题地阅读它。

$db = $session->get('test' , '' );

But when I execute a php that is called by a XMLHttpRequest within a Javascript (everything in the same folder), it doesnt find the session variable and returns the default value. 但是,当我执行Javascript(所有文件都在同一文件夹中)中的XMLHttpRequest调用的php时,它找不到会话变量并返回默认值。

What am I missing? 我想念什么?

If you want to declare those session values are variables you can read from anywhere in Joomla you should be using the setUserState and getUserState methods. 如果要声明这些会话值是变量,则可以从Joomla中的任何位置读取,则应使用setUserState和getUserState方法。

For example if you want to set a session variable named test your could do this: 例如,如果您要设置一个名为test的会话变量,则可以这样做:

$app = JFactory::getApplication();
$app->setUserState('test','my test value');

Then if you want to call that anywhere else in Joomla just set it to a variable using getUserState like this: 然后,如果您想在Joomla中的其他任何地方调用它,只需使用getUserState将其设置为变量,如下所示:

$app = JFactory::getApplication();
$mySessionVariable = $app->getUserState('test');
echo $mySessionVariable;

Here is a link to the session state variables documentation for Joomla: Joomla User State Variables 这是指向Joomla的会话状态变量文档的链接: Joomla用户状态变量

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

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