1

I want create login functionality outside magento, but same server. For this I created a login form, but without correct form key it is useless.

Is there way to get customer login form key?

Here is my code. But it gives different key

<?php
require 'store/app/Mage.php';
Mage::app(); 
// Define the path to the root of Magento installation.
define('ROOT', Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB));

// Obtain the general session and search for an item called 'customer_id'
$coreSession = Mage::getSingleton('core/session', array('name' => 'frontend'));

$key = Mage::getSingleton('core/session')->getFormKey(); 
?>

<form action="mydomain/store/customer/account/loginPost/" method="post">
    <input name="form_key" value="<?php echo $key; ?>" type="hidden">
    <input type="text" name="login[username]" value="username" />
    <input type="password" name="login[password]" value="password" />
    <input type="submit" name="submit" value="submit" />
</form>

Edit

My Problem is $this->getBlockHtml('formkey'); and Mage::getSingleton('core/session')->getFormKey() not same is there any way to use real form key.

  • Not sure about this but try $coreSession = Mage::getSingleton('core/session', array('name' => 'frontend'))->start(); instead of $coreSession = Mage::getSingleton('core/session', array('name' => 'frontend'));. – Prateek Jan 2 '16 at 10:29
  • its not work i want to get same key mydomain/customer/account/login/ form and this page (i want to use this outside login form as login) – srinath madusanka Jan 2 '16 at 10:35
  • How did you resolve this issue? – Anshu Mishra Nov 21 '17 at 9:22
  • no. i disable core file key validation part unfortunately – srinath madusanka Nov 23 '17 at 3:55
1

You need to some change at your code;

Call a frontend design by Mage::app()->loadArea('frontend');

then Call formkey block from current exiting layout $layout = Mage::getSingleton('core/layout');

get block by code:

$layout = Mage::getSingleton('core/layout');

//load default xml layout handle and generate blocks
$layout->getUpdate()->load('default');  // add default handler
$layout->generateXml()->generateBlocks();

//get the loaded head and header blocks and output
$BlockHtml = $layout->getBlock('BlockName');

 echo $BlockHtml->toHtml() 

For this case you will get same session form_key value:

Modified code:

<?php
require '../app/Mage.php';
umask(0);
Mage::app()->loadArea('frontend');
define('ROOT', Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB));

$layout = Mage::getSingleton('core/layout');

//load default xml layout handle and generate blocks
$layout->getUpdate()->load('default');
$layout->generateXml()->generateBlocks();

//get the loaded head and header blocks and output
$formkeyBlock = $layout->getBlock('formkey');
$key= Mage::getSingleton('core/session')->getFormKey(); 
?>
  <form action="mydomain/store/customer/account/loginPost/" method="post">
    <input name="form_key" value="<?php echo $key; ?>" type="hidden">
    <input type="text" name="login[username]" value="username" />
<?php echo $formkeyBlock->toHtml()  ?>
    <input type="password" name="login[password]" value="password" />

    <input type="submit" name="submit" value="submit" />

    </form>
  • form key key= Mage::getSingleton('core/session')->getFormKey(); and mydomain/customer/account/login/ form key value is differant.any way to get same key. – srinath madusanka Jan 2 '16 at 10:31
  • because after post this form cant login – srinath madusanka Jan 2 '16 at 10:33
  • using this key i cant login.any way to create login form outside magento – srinath madusanka Jan 2 '16 at 10:36
  • @amit : How much of times the formkey valid, its keep updating right? – zus Jan 10 '18 at 8:40
  • @amit , i am send mail to the customer after buy product and send product review form in customer's mail inside mail customer rate that product and submit review and that review automatically save in magento database , all things working fine except "form key", can you tell me how to add a product review form key in mail template – Sanjay Gohil Mar 31 '18 at 6:23
0

You can get form key in controller like this

$customformKey = $this->getRequest()->getParam('form_key'); 
// $customformKey = $this->getRequest()->getPost('form_key');// try this if getParam() is null
if ($customformKey == Mage::getSingleton('core/session')->getFormKey()) {
   // Your code
}

Hope it helps.

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.