3

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!');
}
?>
5

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";
}
  • It's not working for me Why ? – Amaresh Tiwari Jul 10 '17 at 6:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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