How to verify email address using the Zend Email Validator

Zend framework provides some nice validators to validate the data input by your visitor/customer. If you want you can write your own custom validator and use it in your code. The one I would like to discuss today is the Zend Email Validator which is really wonderful in that you don’t need to write any JavaScript code to validate the email address. You just need to add the Zend Email Validator to the form element and it will take care of the rest.

Take a look at the following form element against which I have added the Zend Email Validator

$email = new Zend_Form_Element_Text('email');
$email->setLabel('Email Address')
	->addValidator(new Zend_Validate_EmailAddress(
			array('domain' => true)

The above code will check the domain part of the email address provided by the customer.

Once the visitor tries to enter invalid email address then the system will flag the error message. Please see below


How to allow access to authorized members in Zend using Zend_Session_Namespace

You can maintain and manage sessions and cookies in Zend framework just like you can do in normal PHP code or any other language/framework. Zend provides the Zend_Session_Namespace which allows you to create and manage sessions. Once you have created a session variable you can then use it to authorize a user’s access to any web page. You can use it by placing the code in the init() action event of the controller which you wish to restrict access to only authorized members of the website. The init() action event is executed first by the controller before any other action event. Therefore it’s the best place to put this code.

Consider the following code

public function init() {
	$uid = new Zend_Session_Namespace('uid');
	if (isset($uid->id)) {
               // Do nothing here
	else {
               // Take the user to the login page

The above code checks for a uid session variable. If it has been created then it means that the user has successfully logged into the system. If not then the user is taken to the login page to login into the system. You can also use an intermediary page to display any custom message to the user instead of redirecting to the login page.

Hope the above helped. Let me know how it went for you. I appreciate your comments.