tman_f
Total Posts: 85
Joined: 2008-02-12
Hi all,
I am trying to figure out the magento session configuration. I cant seem to find any documentation. For instance, building inside the adminhtml module, how might I save simple variable data to the session?
Thanks
Posted: July 8 2008
| top
davinder
Total Posts: 56
Joined: 2008-06-26
I m looking for same. Where exactly session variables are stored so that I can use it for other custom programming?
Posted: September 8 2008
| top
| # 1
LeeSaferite
Total Posts: 322
Joined: 2007-08-31
Lake City, FL
Try this
Mage :: getSingleton ( 'core/session' )-> setBlahBlahBlah ( 'my data' ); $myData = Mage :: getSingleton ( 'core/session' )-> getBlahBlahBlah ();
Look at:
Mage_Core_Model_Session_Abstract
It has a few special methods you can use as well. But mostly, you can set anything on the session and get it back on subsequent calls.
EDIT: fixed reference to ‘adminhtml/session’, changed to ‘core/session’
Signature
Lokey Coding, LLC
+1-386-269-0070
Posted: September 8 2008
| top
| # 2
davinder
Total Posts: 56
Joined: 2008-06-26
Thanks for your reply Lee. I get this Class ‘Mage’ not found as an error when I use it. Do I have to put it in some kind of function? I m sorry but I m not pro in PHP
Posted: September 8 2008
| top
| # 3
LeeSaferite
Total Posts: 322
Joined: 2007-08-31
Lake City, FL
Where are you calling your code from? If you are inside Magento, it should work.
Signature
Lokey Coding, LLC
+1-386-269-0070
Posted: September 8 2008
| top
| # 4
LeeSaferite
Total Posts: 322
Joined: 2007-08-31
Lake City, FL
Well, since you are calling a php file directly, I’m assuming your are not inside the Magento environment. Look how cron.php initalizes itself and try using that.
Signature
Lokey Coding, LLC
+1-386-269-0070
Posted: September 8 2008
| top
| # 6
davinder
Total Posts: 56
Joined: 2008-06-26
Thanks Lee I just checked the cron.php and updated the code. Now my code looks like this:
<?php require 'app/Mage.php' ; Mage :: app ( 'admin' ); Mage :: getSingleton ( 'customer/session' )-> isLoggedIn (); $myData = Mage :: getSingleton ( 'customer/session' )-> isLoggedIn (); echo $myData ; ?>
It does not output anything. Any idea what I m doing wrong here? Thanks for your help.
Davinder
Posted: September 8 2008
| top
| # 7
LeeSaferite
Total Posts: 322
Joined: 2007-08-31
Lake City, FL
Use Zend_Debug::dump($myData);
If $myData is null or false, echo won’t show anything
Signature
Lokey Coding, LLC
+1-386-269-0070
Posted: September 8 2008
| top
| # 8
davinder
Total Posts: 56
Joined: 2008-06-26
Thanks Lee. It returns “bool(false)” doesn’t matter if I m logged in or not.
Posted: September 8 2008
| top
| # 9
LeeSaferite
Total Posts: 322
Joined: 2007-08-31
Lake City, FL
Hmm, not sure really.
try:
require 'app/Mage.php' ; Mage :: app (); Mage :: getSingleton ( 'customer/session' )-> isLoggedIn (); $session = Mage :: getSingleton ( 'customer/session' ); $myData = $session -> isLoggedIn (); Zend_Debug :: dump ( $myData ); Zend_Debug :: dump ( $session -> toArray ());
Signature
Lokey Coding, LLC
+1-386-269-0070
Posted: September 8 2008
| top
| # 10
davinder
Total Posts: 56
Joined: 2008-06-26
This is the response
bool(false)
array(1) {
["session_hosts"] => array(1) {
["www.newagestyles.com"] => bool(true)
}
}
Posted: September 8 2008
| top
| # 11
LeeSaferite
Total Posts: 322
Joined: 2007-08-31
Lake City, FL
Well, since you are pulling from the ‘customer/session’ object, you would have to be logged in as a customer to see anything (sorry I missed that before). If you want to know if you are logged into the admin site, try using ‘admin/session’
require 'app/Mage.php' ; Mage :: app (); $session = Mage :: getSingleton ( 'admin/session' ); $myData = $session -> isLoggedIn (); Zend_Debug :: dump ( $myData ); Zend_Debug :: dump ( $session -> toArray ());
That is Mage_Admin_Model_Session
Signature
Lokey Coding, LLC
+1-386-269-0070
Posted: September 8 2008
| top
| # 12
davinder
Total Posts: 56
Joined: 2008-06-26
I was logged in as customer when I received the output above. I m logging in and out as customer to test this.
Posted: September 8 2008
| top
| # 13
edlitmus
Total Posts: 25
Joined: 2008-07-21
Here’s what I am doing to manage sessions:
<?php require_once( '/opt/local/apache2/htdocs/magento/app/Mage.php' ); umask ( 0 ); // try to restore session... if (isset( $_COOKIE[ 'magento' ] )) { $session_id = $_COOKIE[ 'magento' ] ; session_id ( $session_id ); } // or set up a new one... else { session_name ( 'magento' ); session_start (); } Mage :: app ( 'default' ); $session = Mage :: getSingleton ( 'customer/session' ); if (! $session -> login ( $username , $password )) { error_log ( "can't log in!" ); } ?>
Posted: September 8 2008
| top
| # 14
LeeSaferite
Total Posts: 322
Joined: 2007-08-31
Lake City, FL
@davinder
I’m not sure what is causing your problem right now.
Try cheating and looking at the entire session object
require 'app/Mage.php' ; Mage :: app (); $session = Mage :: getSingleton ( 'customer/session' ); Zend_Debug :: dump ( $session -> toArray ()); Zend_Debug :: dump ( $_SESSION );
@edlitmus
Why are you doing this:
// try to restore session...if (isset( $_COOKIE[ 'magento' ] )) { $session_id = $_COOKIE[ 'magento' ] ; session_id ( $session_id ); } // or set up a new one... else { session_name ( 'magento' ); session_start (); }
When you get a session model object (like customer/session or admin/session), it does that internally already.
Signature
Lokey Coding, LLC
+1-386-269-0070
Posted: September 8 2008
| top
| # 15