Event Observer

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
[php]
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;
[/php]

Step1:- Firstly create the Mywork_AdminSessionUserLoginFailed.xml file which is in the app/etc/modules/
[php]
<?xml version="1.0"?>
<config>
<modules>
<Mywork_AdminSessionUserLoginFailed>
<active>true</active>
<codePool>local</codePool>
</Mywork_AdminSessionUserLoginFailed>
</modules>
</config>
[/php]

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/
[php]
<?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>
[/php]
Note:-
(1) :- This is the name of the event for which you want to create observer.

(2) :- It denotes that you are creating the observer for the event.

(3) :- This is name of observer. It could be anything. Maybe you can write your name. But make sure that this is unique and has not been used in any other observer. Either of core, community, or local module. So the best practice is to prefix namespace and module name.

(4) :- This tag tell type of method.

(5) :- This tag contains name of the class in which observer event handler function specified. So that whenever the event will be dispatched observer will load this class.

(6) :- This tag contains name of the method which will be executed of event firing or dispatch.

Step3:- Now create the Observer.php file which is in the app/code/local/Mywork/AdminSessionUserLoginFailed/Model/
[php]
<?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;
}
}
?>
[/php]

when we put the wrong username or password then username save into login failed.
login_failed

Now the wrong username saves into a login failed table.
login_failed_saveindb