使用Joomla时遇到会话/ cookie问题!版本:Joomla! 1.5.9生产/稳定[Vatani] 2009年1月9日23:00 GMT。
我正在尝试将数据保存到会话中并在下一页上访问它,我尝试过:
1)使用joomlas会话($ session-> set('var','data')) - 这是我首选的方法
2)使用普通的PHP会话($ _SESSION ['Var'] ='data') - 这种工作正常,直到我初始化joomla大型机
3)使用PHP Cookies(setcookie('var','data',time()+ 3600,'/');) - 这一直有效,直到我初始化joomla。
以下是我尝试使用的代码:
第1页:
define( '_JEXEC', 1 );
define( 'JPATH_BASE', realpath(dirname(__FILE__).'/../../..' ));
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();
$session = JFactory::getSession();
$thedata = array();
$i = 0;
if($resultscount > 0) // $resultscount = mysql_num_rows($sql) == 1097 in my script
{
while($row = mysql_fetch_assoc($result))
$thedata[$i]['id'] = $row['user_id'];
...LOTS more additions to $thedata, 1000+ rows containing 28 variables each.
$i++;
}
$session->set('thedata',$thedata);
第2页:
define( '_JEXEC', 1 );
define( 'JPATH_BASE', realpath(dirname(__FILE__).'/../../..' ));
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();
$session = JFactory::getSession();
print_r($session->get('thedata'));
会话始终为空。我注意到会话只包含少量数据(1或2行),但是当我尝试将大量结果保存到会话中时,它会中断并且会话在下一页上为空。
同样的事情发生在cookie上,很少有结果=工作正常,很多结果= cookie空。
我做错了什么?
答案 0 :(得分:0)
最有可能的是,在会话中将超过30000个变量存储在二维数组中会导致溢出,从而破坏您的数据,从而导致数据丢失。通过在会话存储中复制数据库(空间有限),你什么都得不到。
重新查询数据更合适。另外,使用Joomla! API而不是直接使用mysql函数。
$db = JFactory::getDBO();
$db->setQuery("SELECT ...");
$thedata = $db->loadObjectList('id');
就是您所需要的(使用$i
进行双重索引不起任何作用)。