Joomla! Discussion Forums



It is currently Thu Nov 26, 2009 11:26 am (All times are UTC )

 




Post new topic Reply to topic  [ 14 posts ] 
Author Message
 Post subject: losing session data
Posted: Sat Nov 07, 2009 1:05 am 
Joomla! Enthusiast
Joomla! Enthusiast
Offline

Joined: Tue Jul 21, 2009 3:30 pm
Posts: 127
Location: United Kingdom
I have created a simple plugin which changed {wishlist} into a button which stores a session of the current article title. I then have a module which then displays those sessions just like a shopping cart.

I got 2 problems, The first problem (which is the most important) is that my module displays the session successfully but when i navigate to a different page, example frontpage to user-login. I lose the data for that session and my module becomes empty.

How can i store this data only losing it when the user closes their web browser? my code is pasted below:
Code:
<?php

//no direct access

defined('_JEXEC') or die('Restricted Access');

//create session
session_start();

$product = mysql_real_escape_string($_POST['product']);
$session = JSession::getInstance('product',array($product));
$session->set('product', $product);

echo "<h2>". $session->get('product') ."</h2>";


And lastly how can i make my session into an array to display more than one item? is it:
Code:
$session = JSession::getInstance('product',array($product));

Thanks in advanced


Top
  E-mail  
 
 Post subject: Re: losing session data
Posted: Sat Nov 07, 2009 4:08 am 
User avatar
Joomla! Virtuoso
Joomla! Virtuoso
Offline

Joined: Sat Sep 24, 2005 11:01 pm
Posts: 4490
Location: Toronto, Canada
aaron harding wrote:
I have created a simple plugin which changed {wishlist} into a button which stores a session of the current article title. I then have a module which then displays those sessions just like a shopping cart.

I got 2 problems, The first problem (which is the most important) is that my module displays the session successfully but when i navigate to a different page, example frontpage to user-login. I lose the data for that session and my module becomes empty.

How can i store this data only losing it when the user closes their web browser? my code is pasted below:
Code:
<?php

//no direct access

defined('_JEXEC') or die('Restricted Access');

//create session
session_start();

$product = mysql_real_escape_string($_POST['product']);
$session = JSession::getInstance('product',array($product));
$session->set('product', $product);

echo "<h2>". $session->get('product') ."</h2>";


And lastly how can i make my session into an array to display more than one item? is it:
Code:
$session = JSession::getInstance('product',array($product));

Thanks in advanced

calling session_Start is probably messing with Joomla's session handling.

Probably much easier to do:
$app = &JFactory::getApplication();
$product = $app->getUserStateFromRequest('mypluginname.product', 'product', 'default value to return if no session variable or request variable found');

Ian

_________________
Joomla! Leadership Team - Production Working Group
Joomla! Bug Squad Coordinator
Joomla! Developer Documentation Team
Please don't say something 'isn't working'. Explain what you tried, and what happened as a result.


Top
   
 
 Post subject: Re: losing session data
Posted: Sat Nov 07, 2009 10:11 am 
Joomla! Enthusiast
Joomla! Enthusiast
Offline

Joined: Tue Jul 21, 2009 3:30 pm
Posts: 127
Location: United Kingdom
Sorry that didn't work, any more solutions? i am still losing my session variables.


Top
  E-mail  
 
 Post subject: Re: losing session data
Posted: Sun Nov 08, 2009 6:43 pm 
User avatar
Joomla! Virtuoso
Joomla! Virtuoso
Offline

Joined: Sat Sep 24, 2005 11:01 pm
Posts: 4490
Location: Toronto, Canada
aaron harding wrote:
Sorry that didn't work, any more solutions? i am still losing my session variables.

So you aren't getting the correct data back from $app->getUserState() ?

Ian

_________________
Joomla! Leadership Team - Production Working Group
Joomla! Bug Squad Coordinator
Joomla! Developer Documentation Team
Please don't say something 'isn't working'. Explain what you tried, and what happened as a result.


Top
   
 
 Post subject: Re: losing session data
Posted: Mon Nov 09, 2009 9:31 am 
Joomla! Enthusiast
Joomla! Enthusiast
Offline

Joined: Tue Jul 21, 2009 3:30 pm
Posts: 127
Location: United Kingdom
I am getting the data back, but i lose it again when i navigate away from the page.


Top
  E-mail  
 
 Post subject: Re: losing session data
Posted: Mon Nov 09, 2009 12:04 pm 
User avatar
Joomla! Apprentice
Joomla! Apprentice
Offline

Joined: Fri Oct 09, 2009 8:58 am
Posts: 11
Location: Chennai, Tamilnadu, India
Quote:
$session = JSession::getInstance('product',array($product));


I will create a new session. so you can't accept in next pages.

so use,

Code:
$session =& JFactory::getSession();


it will gives the current session object.

it may useful for you.

_________________
Best Regards,
SelvaG


Top
  E-mail  
 
 Post subject: Re: losing session data
Posted: Mon Nov 09, 2009 2:12 pm 
User avatar
Joomla! Virtuoso
Joomla! Virtuoso
Offline

Joined: Sat Sep 24, 2005 11:01 pm
Posts: 4490
Location: Toronto, Canada
aaron harding wrote:
I am getting the data back, but i lose it again when i navigate away from the page.

What exact code to you have?

Ian

_________________
Joomla! Leadership Team - Production Working Group
Joomla! Bug Squad Coordinator
Joomla! Developer Documentation Team
Please don't say something 'isn't working'. Explain what you tried, and what happened as a result.


Top
   
 
 Post subject: Re: losing session data
Posted: Mon Nov 09, 2009 4:00 pm 
Joomla! Enthusiast
Joomla! Enthusiast
Offline

Joined: Tue Jul 21, 2009 3:30 pm
Posts: 127
Location: United Kingdom
Just started from the basics with this session and narrowed it down to the following problem.

i tried:

Code:
session_start();
$_SESSION['product'] = 1'

echo $_SESSION['product']


that works fine but when i try to set a value from a form post like this:
Code:
session_start();
$_SESSION['product'] = JRequest::getCmd('product')

echo $_SESSION['product'];


this displays the session value like it should but when i navigate away it loses that value. I think it is something to do with the form. Is there a way to store a form value temporarly but not in a database?


Top
  E-mail  
 
 Post subject: Re: losing session data
Posted: Mon Nov 09, 2009 4:26 pm 
User avatar
Joomla! Virtuoso
Joomla! Virtuoso
Offline

Joined: Sat Sep 24, 2005 11:01 pm
Posts: 4490
Location: Toronto, Canada
aaron harding wrote:
Just started from the basics with this session and narrowed it down to the following problem.

i tried:

Code:
session_start();
$_SESSION['product'] = 1'

echo $_SESSION['product']


that works fine but when i try to set a value from a form post like this:
Code:
session_start();
$_SESSION['product'] = JRequest::getCmd('product')

echo $_SESSION['product'];


this displays the session value like it should but when i navigate away it loses that value. I think it is something to do with the form. Is there a way to store a form value temporarly but not in a database?


Joomla manages it's own session data. I suggested some code for you to try which unless you are doing something weird to break it, it will work. Did you have any luck with it?

Ian

_________________
Joomla! Leadership Team - Production Working Group
Joomla! Bug Squad Coordinator
Joomla! Developer Documentation Team
Please don't say something 'isn't working'. Explain what you tried, and what happened as a result.


Top
   
 
 Post subject: Re: losing session data
Posted: Mon Nov 09, 2009 4:46 pm 
Joomla! Enthusiast
Joomla! Enthusiast
Offline

Joined: Tue Jul 21, 2009 3:30 pm
Posts: 127
Location: United Kingdom
Sorry if im being a bit of a pain, i paste copies of my code i currently have:

This is a snippet of code from my plugin which converts the keyword "{wishlist}" into a submit button. Below is a copy of the form it generates:
Code:
$load    = str_replace( 'wishlist', '', $matches[0][$i] );

                        $load = str_replace( '{', '', $load );

                        $load = str_replace( '}', '', $load );

                  $load = trim( $load );
                       

                  $content    = "<form method='post' name='wishlist' >".

                                "<input type='submit' value='Add to wishlist'" .

                             "name='add to wishlist' />".
                            
                             "<input type='hidden' value='". $row->title ."'".
                            
                             "name='product' /></form>";

                  $row->text  = preg_replace( '{'. $matches[0][$i] .'}', $content, $row->text );

                    return $load;


next is my module which is displaying the content:
Code:
<?php

//no direct access
defined('_JEXEC') or die('Restricted Access');

//start the session

session_start();

$product = mysql_real_escape_string($_POST['product']);
$session = JSession::getInstance('product', array());
$session->set('product', $product);

echo "<h2>". $session->get('product') ."</h2>";


I did try the "$app->getUserState()" but still had the same results as with the JSession. Thanks again and sorry for being a pain.


Top
  E-mail  
 
 Post subject: Re: losing session data
Posted: Mon Nov 09, 2009 5:01 pm 
User avatar
Joomla! Virtuoso
Joomla! Virtuoso
Offline

Joined: Sat Sep 24, 2005 11:01 pm
Posts: 4490
Location: Toronto, Canada
aaron harding wrote:
Sorry if im being a bit of a pain, i paste copies of my code i currently have:

This is a snippet of code from my plugin which converts the keyword "{wishlist}" into a submit button. Below is a copy of the form it generates:
Code:
$load    = str_replace( 'wishlist', '', $matches[0][$i] );

                        $load = str_replace( '{', '', $load );

                        $load = str_replace( '}', '', $load );

                  $load = trim( $load );
                       

                  $content    = "<form method='post' name='wishlist' >".

                                "<input type='submit' value='Add to wishlist'" .

                             "name='add to wishlist' />".
                            
                             "<input type='hidden' value='". $row->title ."'".
                            
                             "name='product' /></form>";

                  $row->text  = preg_replace( '{'. $matches[0][$i] .'}', $content, $row->text );

                    return $load;


next is my module which is displaying the content:
Code:
<?php

//no direct access
defined('_JEXEC') or die('Restricted Access');

//start the session

session_start();

$product = mysql_real_escape_string($_POST['product']);
$session = JSession::getInstance('product', array());
$session->set('product', $product);

echo "<h2>". $session->get('product') ."</h2>";


I did try the "$app->getUserState()" but still had the same results as with the JSession. Thanks again and sorry for being a pain.

What code did you use when you tried $app->getUserState?

Ian

_________________
Joomla! Leadership Team - Production Working Group
Joomla! Bug Squad Coordinator
Joomla! Developer Documentation Team
Please don't say something 'isn't working'. Explain what you tried, and what happened as a result.


Top
   
 
 Post subject: Re: losing session data
Posted: Mon Nov 09, 2009 5:02 pm 
Joomla! Explorer
Joomla! Explorer
Offline

Joined: Wed Apr 15, 2009 5:33 pm
Posts: 280
Location: Fortaleza, CE
ianmac wrote:
Joomla manages it's own session data

When you call this, you mess with the joomla session.
Code:
session_start();


Then here you use Joomla session and you can access the session data from anywhere in this page load.
Code:
$session = JSession::getInstance('product',array($product));
$session->set('product', $product);

echo "<h2>". $session->get('product') ."</h2>";



Then here you mess with the Joomla session again and lose the data you set on the previous load.
Code:
session_start();


You should not call this, and then let Joomla handle the session, so you can use it like ianmac described here.

_________________
-------------------------------------------------------------------
Nailson Oliveira - http://imagineseusite.com.br/
-------------------------------------------------------------------


Top
  E-mail  
 
 Post subject: Re: losing session data
Posted: Mon Nov 09, 2009 5:13 pm 
User avatar
Joomla! Virtuoso
Joomla! Virtuoso
Offline

Joined: Sat Sep 24, 2005 11:01 pm
Posts: 4490
Location: Toronto, Canada
nailson_imgn wrote:
ianmac wrote:
Joomla manages it's own session data

When you call this, you mess with the joomla session.
Code:
session_start();


Then here you use Joomla session and you can access the session data from anywhere in this page load.
Code:
$session = JSession::getInstance('product',array($product));
$session->set('product', $product);

echo "<h2>". $session->get('product') ."</h2>";



Then here you mess with the Joomla session again and lose the data you set on the previous load.
Code:
session_start();


You should not call this, and then let Joomla handle the session, so you can use it like ianmac described here.

You should get the session via JFactory and not using JSession::getInstance IIRC.

Ian

_________________
Joomla! Leadership Team - Production Working Group
Joomla! Bug Squad Coordinator
Joomla! Developer Documentation Team
Please don't say something 'isn't working'. Explain what you tried, and what happened as a result.


Top
   
 
 Post subject: Re: losing session data
Posted: Tue Nov 10, 2009 4:30 pm 
Joomla! Enthusiast
Joomla! Enthusiast
Offline

Joined: Tue Jul 21, 2009 3:30 pm
Posts: 127
Location: United Kingdom
Solved the problem. It seemed that everytime i redirected to a different page my form seemed to reset the value to null or "". I fixed it by using this bit of code:
Code:
<?php

//no direct access
defined('_JEXEC') or die('Restricted Access');

//create session
session_start();

//get post data from the form
$course = mysql_real_escape_string($_POST['course']);
$session = JSession::getInstance('course', array());

//check to see if the post data is empty
if( $course != null || "" ){
      $session->set('course', $course) ;
   }
   
echo "<h4>". $session->get('course') ."</h4>"


That checks to see the value, if it is not equal to null or "" then set the sesion to the post value.


Top
  E-mail  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 14 posts ] 

Quick reply

 



Who is online

Users browsing this forum: altentic, mazeem and 50 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group