【问题标题】:How to check if a session in JFactory::getSession() is set or not ?如何检查 JFactory::getSession() 中的会话是否已设置?
【发布时间】:2012-08-31 10:39:06
【问题描述】:

鉴于组件,我在 joomla 2.5 viewform.php 中有以下几行

 $session = & JFactory::getSession();
 if(empty($session->get('MasterIndex'))) $session->set('MasterIndex',0);

我明白了

致命错误:无法在写入上下文中使用方法返回值 /var/www/ ..

我也试过

 if(empty($session->get('MasterIndex'))) $session->set('MasterIndex',0);

如何检查会话值是否设置?

【问题讨论】:

    标签: php joomla joomla2.5


    【解决方案1】:

    试试:

    $session =& JFactory::getSession();
    $setSession = $session->get('MasterIndex');
    if(empty($setSession)) $session->set('MasterIndex',0);
    

    注意:您收到该错误是因为 empty() 仅检查变量,因为其他任何内容都会导致解析错误

    【讨论】:

      【解决方案2】:
      $session =& JFactory::getSession();
      $setSession = $session->get('MasterIndex');
      if(empty($setSession)) 
      {
          $s_var = $session->set('MasterIndex',0);
      }
      else
      {
          $s_var = $session->set('MasterIndex',1);
      }
      
      echo $s_var;
      

      试试这个.....

      【讨论】:

      • 谢谢,正如@Sudhir 提到的,它是空的,我不能直接在 $sesion->get 上使用它,因为它返回一个值而不是变量。所以你的代码会起作用。谢谢
      猜你喜欢
      • 2014-06-15
      • 1970-01-01
      • 2014-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-04
      • 2011-04-23
      • 1970-01-01
      相关资源
      最近更新 更多