Magento 1.9 Redis Session Cache – Check if User Logged on to Admin

cachemagento-1.9PHPredis

I have this code, found on the Internet, to check if a user is logged on to the admin pages or not.

I recently installed Redis Session Cache successfully, and now these lines of code doesn't work anymore.

Anyone know if it's possible to change this, to get it to work when storing sessions in Redis?

Thanks in advance,

<?php
require $_SERVER['DOCUMENT_ROOT'] . "/app/Mage.php";

function isBackendUserLoggedIn() {
    if (!array_key_exists('adminhtml', $_COOKIE)) return false;

    if(!session_id()) session_start();
    $oldSession = $_SESSION;

    Mage::app();

    $sessionFilePath = Mage::getBaseDir('session') . DS . 'sess_' . $_COOKIE['adminhtml'];
    $sessionContent  = file_get_contents($sessionFilePath);

    session_decode($sessionContent);

    /** @var Mage_Admin_Model_Session $session */
    $session  = Mage::getSingleton('admin/session');
    $loggedIn = $session->isLoggedIn();

    //set old session back to current session
    $_SESSION = $oldSession;

    return $loggedIn;
}

if( isBackendUserLoggedIn() ){
    $logonStat = 'true';
} else {
    die('You must be logged on to access this page!');
}
?>

Best Answer

You should use something like this instead of the session check you are doing based on a file.

//get the admin session
Mage::getSingleton('core/session', array('name'=>'adminhtml'));

//verify if the user is logged in to the backend
if(Mage::getSingleton('admin/session')->isLoggedIn()){
  //do stuff
}
else
{
  echo "go away bad boy";
}
Related Topic