1

I am trying to create a session variable in my store index.php variable by using the following piece of code.

  Mage::getSingleton('core/session')->setRefer("access");
   $refer = Mage::getSingleton('core/session')->getRefer();

But I am getting an error saying.

Fatal error: Call to a member function getModelInstance() on null can anyone kindly let me know, how I can set a session variable in index.php file in magento store.

Edit: As ram charan asked I am adding my complete index.php file code here. I am trying to find country and change the currency type based on that and at the same time I am trying to set a session inside the condition.

<?php

if (version_compare(phpversion(), '5.3.0', '<')===true) {
    echo  '<div style="font:12px/1.35em arial, helvetica, sans-serif;">
<div style="margin:0 0 25px 0; border-bottom:1px solid #ccc;">
<h3 style="margin:0; font-size:1.7em; font-weight:normal; text-transform:none; text-align:left; color:#2f2f2f;">
Whoops, it looks like you have an invalid PHP version.</h3></div><p>Magento supports PHP 5.3.0 or newer.
<a href="http://www.magentocommerce.com/install" target="">Find out</a> how to install</a>
 Magento using PHP-CGI as a work-around.</p></div>';
    exit;
}
/**
 * Compilation includes configuration file
 */
define('MAGENTO_ROOT', getcwd());

$compilerConfig = MAGENTO_ROOT . '/includes/config.php';
if (file_exists($compilerConfig)) {
    include $compilerConfig;
}
/*ini_set('error_reporting', E_ERROR);
register_shutdown_function("fatal_handler");
function fatal_handler() {
    $error = error_get_last();
    echo("<pre>");
    print_r($error);
}*/
$mageFilename = MAGENTO_ROOT . '/app/Mage.php';
$maintenanceFile = 'maintenance.flag';
if (!file_exists($mageFilename)) {
    if (is_dir('downloader')) {
        header("Location: downloader");
    } else {
        echo $mageFilename." was not found";
    }
    exit;
}
if (file_exists($maintenanceFile)) {
    include_once dirname(__FILE__) . '/errors/503.php';
    exit;
}
require MAGENTO_ROOT . '/app/bootstrap.php';
require_once $mageFilename;

#Varien_Profiler::enable();

if (isset($_SERVER['MAGE_IS_DEVELOPER_MODE'])) {
    Mage::setIsDeveloperMode(true);
}
#Mage::setIsDeveloperMode(true);
#ini_set('display_errors', 1);

umask(0);

/* Store or website code */
$mageRunCode = isset($_SERVER['MAGE_RUN_CODE']) ? $_SERVER['MAGE_RUN_CODE'] : '';

/* Run store or run website */
$mageRunType = isset($_SERVER['MAGE_RUN_TYPE']) ? $_SERVER['MAGE_RUN_TYPE'] : 'store';

// some logic to find the ip of the user
  $country="US";
    if($country!="IN")
    {
        Mage::init();
        Mage::getSingleton('core/session')->setRefer("access");
        $refer = Mage::getSingleton('core/session')->getRefer();

       Mage::app();
       Mage::app()->getStore()->setCurrentCurrencyCode('USD');
       Mage::reset();
}

Mage::run($mageRunCode, $mageRunType);
2
  • give me full index.php code here Jul 11, 2017 at 12:18
  • @RamaChandran I have added my complete index.php file as you have asked. Jul 11, 2017 at 12:49

4 Answers 4

5

Try this code

Mage::init();
Mage::getSingleton('core/session')->setRefer("access");
$refer = Mage::getSingleton('core/session')->getRefer();
2
  • This is giving me an uncaught exception as follows Fatal error: Uncaught exception 'Zend_Controller_Response_Exception' with message 'Cannot send headers; headers already sent Jul 11, 2017 at 12:37
  • I have checked by adding your code in my index.php , it didn't threw any error
    – Piyush
    Jul 11, 2017 at 13:13
0

Try:

$messageValue = 'access';
Mage::getSingleton('core/session')->setData('my_message',$messageValue);
Mage::getSingleton('core/session')->getData('my_message');
3
  • Mage::getSingleton('core/session')->getData('my_message'); Jul 11, 2017 at 13:23
  • yes, i forgot the ' ' ;)
    – PЯINCƎ
    Jul 11, 2017 at 13:25
  • @piyush answer worked for me. Thank you for the help :) Jul 11, 2017 at 13:53
0

Try like that

$sessionaccess= Mage::getSingleton('core/session', array('name' => 'frontend'));

$sessionaccess->setData("access_value", 'access');

$refer = $sessionaccess->getData("access_value");
1
  • Hi @ramachandran piyush answer worked for me thank you for the help :) Jul 11, 2017 at 13:54
0
Mage::getSingleton('core/session')->setRefer("access");
$sessionfree = Mage::getSingleton('core/session', array('name' => 'frontend'));
$refer =  $sessionfree->getRefer();
1
  • Hi vijay piyush's answer worked for me thank you for the help :) Jul 11, 2017 at 13:55

Your Answer

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

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