3

I need to save data in session on one page and retrieve same data on multiple pages. After saving the data when i refresh page Or redirect to another page data flush off . Can somebody explain me why it's happening? What can do to fix this?

I have tried with all types of sessions in magento2(customer,checkout,backend,catalog) also phpsession($_SESSION) without any luck.

am using Magento 2.1.7 with Luma Theme.

Any help will be really appreciated.

2 Answers 2

0

For Catalog, Checkout, Customer and other Magento session like

\Magento\Catalog\Model\Session
\Magento\Checkout\Model\Session
\Magento\Customer\Model\Session

Because Magento 2 is no longer using core session like in magento 1.

protected $customerSession;
public function __construct(
    ....
    \Magento\Customer\Model\Session $customerSession, 
    ....
){
    ....
    $this->customerSession = $customerSession;
    ....
}

To set and get the value of session.

$this->customerSession->setCustomerName('Bob'); //Set Value
$this->customerSession->getCustomerName(); // Get Value
$this->customerSession->unsCustomerName(); // Unset Value

You can use above Factory method by creating function or directly by using objectManager

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->create('Magento\Customer\Model\Session');
$customerSession->setCustomerName('Bob');
$customerSession->getCustomerName();
9
  • 2
    Already tried this, didn't worked. I mentioned above.
    – Om Prakash
    Aug 18, 2017 at 14:50
  • you want to get it in PHTML file? Aug 18, 2017 at 14:50
  • No just in controller. Saving data in one controller and retrieve at multiple places .
    – Om Prakash
    Aug 18, 2017 at 14:52
  • multiple places like other controllers ? right Aug 18, 2017 at 14:53
  • 1
    This worked for me "\Magento\Persistent\Model\Session" and "\Magento\Framework\Session\SessionManagerInterface"
    – Om Prakash
    Aug 15, 2019 at 2:42
0

You may have exceeded the string length limit for storing data in the session. Just increase this value from Magento admin panel (Stores > Configuration > Advanced > System > Security).

Details:

Data which Magento saving each time (almost on each request) looks like this (it is a part of real data):

session data example

As you can see at the bottom of screenshot it is 285934 chars length. But Magento (2.4.1 in my case) has own properties named "Max Session Size in Admin" and "Max Session Size in Storefront" which add limits to the maximum allowed size of that string. Default value of that settings is 256000 and it is less than size of real data in my case.

Settings in admin panel

What Magento does? It adding notification message in the log file, and save nothing new... but did not delete old value.

Here is that part of code from ./vendor/magento/framework/Session/SaveHandler.php:

Saving data from session

Checked in debugger:

Size from debugger

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.