Event:- An Event is an action that takes place during normal program execution ex:- sales_order_place_before, sales_order_place_after etc.
Observer:- Observer is the event handler.
“Customizing Magento code using Event observer allows us to add your own logic, without changing any core code file.”
Now we create the extension AdminSessionUserLoginFailed and we will use event and observer in this extension.
“This extension is used for login failed record save in the login failed table”.
Firstly create the table loginfailed in the magento site database
CREATE TABLE IF NOT EXISTS `loginfailed` ( `id` int(11) PRIMARY KEY NOT NULL AUTO_INCREMENT, `user_name` varchar(200) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Step1:- Firstly create the Mywork_AdminSessionUserLoginFailed.xml file which is in the app/etc/modules/
<?xml version="1.0"?> <config> <modules> <Mywork_AdminSessionUserLoginFailed> <active>true</active> <codePool>local</codePool> </Mywork_AdminSessionUserLoginFailed> </modules> </config>
Note:- Where “Mywork” is a namespace and “AdminSessionUserLoginFailed” is a module name.
Step2:- Now create the config.xml file which is in the app/code/local/Mywork/AdminSessionUserLoginFailed/etc/
<?xml version="1.0"?> <config> <modules> <Mywork_AdminSessionUserLoginFailed> <version>1.0.0</version> </Mywork_AdminSessionUserLoginFailed> </modules> <global> <events> <admin_session_user_login_failed> <observers> <mywork_adminsessionuserloginfailed_observer> <type>singleton</type> <class>Mywork_AdminSessionUserLoginFailed_Model_Observer</class> <method>Loginfailed</method> </mywork_adminsessionuserloginfailed_observer> </observers> </admin_session_user_login_failed> </events> </global> </config>
Note:-
(1)
(2)
(3)
(4)
(5)
(6)
Step3:- Now create the Observer.php file which is in the app/code/local/Mywork/AdminSessionUserLoginFailed/Model/
<?php class Mywork_AdminSessionUserLoginFailed_Model_Observer { public function Loginfailed(Varien_Event_Observer $observer) { //$observer contains the object returns in the event. $event = $observer->getEvent(); $user_name = $event->getUserName(); $write_connection = Mage::getSingleton('core/resource')->getConnection('core_write'); $fields_arr = array(); $fields_arr['user_name'] = $user_name; $write_connection->insert('loginfailed', $fields_arr); return $this; } } ?>
when we put the wrong username or password then username save into login failed.
Now the wrong username saves into a login failed table.