0

Can we login+logout Magento admin by simple PHP code which presents outside Magento setup. I need this functionality because my requirement only full fills by this scenario only.

mywebsite.com/magentosetup (Magento setup here)
mywebsite.com/hitme.php (I need a custom script here for login and logout)

How can I do this with PHP+magento code customization?

For logout, I find this example link but this code does not work for me.

I checked the code in Indexcontroller of Magento for logout function here only these lines are present.

$adminSession = Mage::getSingleton('admin/session'); 
$adminSession->unsetAll(); 
$adminSession->getCookie()->delete($adminSession->getSessionName());
$adminSession->addSuccess(Mage::helper('adminhtml')->__('You have logged out.'));
$this->_redirect('*');

But this line of code is not working externally for logout externally. When echo this line in Magento section

$adminSession->getSessionName()

it provides "adminhtml" but the same line externally PHP file it provides "PHPSESSID". Let me know what can I do for login and logout admin programmatically.

7ochem
7,61516 gold badges54 silver badges82 bronze badges
asked Oct 19, 2015 at 12:04

2 Answers 2

2

Below is code that worked on my EE dev box to log you out of the admin:

require_once('app/Mage.php');
Mage::app('default');
$session = Mage::getSingleton('core/session', array('name' => 'adminhtml'));
$session->unsetAll();
$session->getCookie()->delete($session->getSessionName());

I tested this code and it worked on my EE dev box, but I lifted it from this answer: Programatically login admin into admin panel possible or not

Be sure to set "yourusername" to the username of the user you wish to log in.

require_once 'app/Mage.php';
umask(0);
$app = Mage::app('default');
Mage::getSingleton('core/session', array('name' => 'adminhtml'));
// supply username
$user = Mage::getModel('admin/user')->loadByUsername('yourusername');
if (Mage::getSingleton('adminhtml/url')->useSecretKey()) {
 Mage::getSingleton('adminhtml/url')->renewSecretUrls();
} 
$session = Mage::getSingleton('admin/session');
$session->setIsFirstVisit(true);
$session->setUser($user);
$session->setAcl(Mage::getResourceModel('admin/acl')->loadAcl());
Mage::dispatchEvent('admin_session_user_login_success',array('user'=>$user) );
if ($session->isLoggedIn()) {
 echo("logged in");
}
3
  • Login code is not working first time , after clear cache of browser. Commented Oct 29, 2015 at 12:35
  • Did you set "yourusername" to a legitimate username for your site? Commented Oct 29, 2015 at 16:10
  • If you are reloading the login/admin page, be sure you don't have a key in your url. If you click "logout" it will add the key, and reloading the page won't work because the script sets up a new session. Try pulling up just the admin url without any parameters. Commented Oct 29, 2015 at 18:34
0

Are you requiring Mage.php in your external files?

require_once 'app/Mage.php'; 
// make sure the path is right to app/Mage.php from this file

If not, you won't have access to the objects and functions to login and logout.

7ochem
7,61516 gold badges54 silver badges82 bronze badges
answered Oct 20, 2015 at 15:52
1
  • Yes i already include file but the problem is that i am not able to do login and logout admin from externally files as i used example but it is not working for me. Commented Oct 21, 2015 at 4:31

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.