Try the Demo

Magento

eCommerce Software for Online Growth

Magento Forum

Our new hosted solution for small & emerging businesses
   
Page 1 of 3
Working with sessions
 
tman_f
Sr. Member
 
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

 
Magento Community Magento Community
Magento Community
Magento Community
 
davinder
Member
 
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?

 
Magento Community Magento Community
Magento Community
Magento Community
 
LeeSaferite
Guru
 
Avatar
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

 
Magento Community Magento Community
Magento Community
Magento Community
 
davinder
Member
 
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

 
Magento Community Magento Community
Magento Community
Magento Community
 
LeeSaferite
Guru
 
Avatar
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

 
Magento Community Magento Community
Magento Community
Magento Community
 
davinder
Member
 
Total Posts:  56
Joined:  2008-06-26
 

Yes I m. I have installed Magento on root and here is the page where I get the error http://www.newagestyles.com/findsession.php.

 
Magento Community Magento Community
Magento Community
Magento Community
 
LeeSaferite
Guru
 
Avatar
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

 
Magento Community Magento Community
Magento Community
Magento Community
 
davinder
Member
 
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

 
Magento Community Magento Community
Magento Community
Magento Community
 
LeeSaferite
Guru
 
Avatar
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

 
Magento Community Magento Community
Magento Community
Magento Community
 
davinder
Member
 
Total Posts:  56
Joined:  2008-06-26
 

Thanks Lee. It returns “bool(false)” doesn’t matter if I m logged in or not.

 
Magento Community Magento Community
Magento Community
Magento Community
 
LeeSaferite
Guru
 
Avatar
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

 
Magento Community Magento Community
Magento Community
Magento Community
 
davinder
Member
 
Total Posts:  56
Joined:  2008-06-26
 

This is the response

bool(false)

array(1) {
["session_hosts"] => array(1) {
["www.newagestyles.com"] => bool(true)
}
}

 
Magento Community Magento Community
Magento Community
Magento Community
 
LeeSaferite
Guru
 
Avatar
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

 
Magento Community Magento Community
Magento Community
Magento Community
 
davinder
Member
 
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.

 
Magento Community Magento Community
Magento Community
Magento Community
 
edlitmus
Jr. Member
 
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!");
}

?>

 
Magento Community Magento Community
Magento Community
Magento Community
 
LeeSaferite
Guru
 
Avatar
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

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top
Page 1 of 3
 
© Copyright 2012 Magento Inc.
Privacy Policy|Terms of Service
Magento Community Count
701238 users|901 users currently online|497314 forum posts