Magento 1.9 Session Variable – How to Set in index.php File

errormagento-1.9session

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);

Best Answer

Try this code

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