Login & Signup in Zend Framework

Zend Framework is an Open-Source object-oriented Web application framework implemented in PHP .The goal of ZF is to simplify Web development while maintaining object-oriented best practices for building Web 2.0 applications and Web services.

I am starting with simple Signup & Login Authentication .

Requirements:-

(i)Latest version of PHP,My SQL Database and Apache Web Server must be installed on your system.

(ii)mod_rewrite must be enabled by using this command

sudo a2enmod rewrite

(iii)Restart Apache

sudo service apache2 restart

(iv)Download Zend minimal package

Make a Directory Structure like this

project/

application/

controllers/

forms/

models/

views/

scripts/

index/

Steps:-

1.Extract the library folder from zend package to the main working project/ directory.

2.Create a index.php file and placed in project/ directory

index.php

<?php

define(‘ROOT_DIR’, dirname(__FILE__));

set_include_path(‘.’

. PATH_SEPARATOR . ROOT_DIR . ‘/library’

. PATH_SEPARATOR . ROOT_DIR . ‘/application/models’

. PATH_SEPARATOR . ROOT_DIR . ‘/application/forms’

. PATH_SEPARATOR . get_include_path()

);

require_once “Zend/Loader/Autoloader.php”;

$autoloader = Zend_Loader_Autoloader::getInstance();

$autoloader->setFallbackAutoloader(true);

$config = new Zend_Config_Ini(ROOT_DIR.’/application/config.ini’, ‘general’);

$db = Zend_Db::factory($config->db);

Zend_Db_Table::setDefaultAdapter($db);

$frontController = Zend_Controller_Front::getInstance();

$frontController->throwExceptions(true);

$frontController->setControllerDirectory(ROOT_DIR.’/application/controllers’);

$frontController->dispatch();

?>

3.Create a Database by executing this following sql query.

CREATE TABLE IF NOT EXISTS `Users` (

`id` int(100) NOT NULL AUTO_INCREMENT,

`firstname` varchar(100) NOT NULL,

`lastname` varchar(100) NOT NULL,

`email` varchar(255) NOT NULL,

`username` varchar(100) NOT NULL,

`password` int(20) NOT NULL,

PRIMARY KEY (`id`)

) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;

4.Create a config.ini file in project/application/ Directory

[general]

db.adapter = “PDO_MYSQL”

db.params.host = “localhost”

db.params.username = “root”

db.params.password = ” “

db.params.dbname = ” “

5.Create a Controller named IndexController.php and placed in application/controllers/ Directory.

IndexController.php

class IndexController extends Zend_Controller_Action
{
    public function indexAction()
    {
    } 
    public function signupAction()
    {
    } 
    public function logoutAction()
    {
    }
    public function homeAction()
    {
    }
}

 

6.Now we will need to create template files in application/views/scripts/index/ named

   index.phtml
   signup.phtml
   logout.phtml
   home.phtml

 

7.Now we begin with us forms create LoginForm in applications/forms/

LoginForm.php

<?php

class LoginForm extends Zend_Form

{

public function init()

{

$this->setMethod(‘post’);

$username = $this->createElement(‘text’,’username’);

$username->setLabel(‘Username’)

->setAttrib(‘maxlength’,75);

 

$password = $this->createElement(‘password’,’password’);

$password->setLabel(‘password:’)

->setAttrib(‘maxlength’,75);

 

$signin = $this->createElement(‘submit’,’signin’);

$signin->setLabel(“sign in”)

->setIgnore(true);

 

$this->addElements(array(

$username,

$password,

$signin

));

}

}

?>

 

8.Next form is RegistrationForm in application/forms/ Directory.

RegistrationForm.php

 

<?php

class RegistrationForm extends Zend_Form

{

public function init()

{

$firstname = $this->createElement(‘text’,’firstname’);

$firstname->setLabel(‘First Name:’)

->setRequired(false);

 

$lastname = $this->createElement(‘text’,’lastname’);

$lastname->setLabel(‘Last Name:’)

->setRequired(false);

 

$email = $this->createElement(‘text’,’email’);

$email->setLabel(‘Email:’)

->setRequired(false);

 

$username = $this->createElement(‘text’,’username’);

$username->setLabel(‘Username:’)

->setRequired(true);

 

$password = $this->createElement(‘password’,’password’);

$password->setLabel(‘Password:’)

->setRequired(true);

 

$confirmPassword = $this->createElement(‘password’,’confirmPassword’);

$confirmPassword->setLabel(‘Confirm Password:’)

->setRequired(true);

 

$register = $this->createElement(‘submit’,’register’);

$register->setLabel(‘Sign up’)

->setIgnore(true);

 

$this->addElements(array(

$firstname,

$lastname,

$email,

$username,

$password,

$confirmPassword,

$register

));

}

}

?>

 

 

9.Now add homeAction fuction to the IndexController.php

 

public function homeAction()

{

$storage = new Zend_Auth_Storage_Session();

$data = $storage->read();

if(!$data){

$this->_redirect(‘index/index’);

}

$this->view->username = $data->username;

}

 

Create template file for this home.phtml in application/views/scripts/index/ Directory

Welcome <?=$this->username?>,<br>

This is your home page<br>

<a href='<?php $i=$this->baseUrl().”/index/logout”; echo $i; ?>’>click here to logout</a>

 

10.Now add indexAction fuction to the IndexController.php

 

public function indexAction()

{

$users = new Users();

$form = new LoginForm();

$this->view->form = $form;

if($this->getRequest()->isPost()){

if($form->isValid($_POST)){

$data = $form->getValues();

$auth = Zend_Auth::getInstance();

$authAdapter = new Zend_Auth_Adapter_DbTable($users->getAdapter(),’Users’);

$authAdapter->setIdentityColumn(‘username’)

->setCredentialColumn(‘password’);

$authAdapter->setIdentity($data[‘username’])

->setCredential($data[‘password’]);

$result = $auth->authenticate($authAdapter);

if($result->isValid()){

$storage = new Zend_Auth_Storage_Session();

$storage->write($authAdapter->getResultRowObject());

$this->_redirect(‘index/home’);

} else {

$this->view->errorMessage = “Invalid username or password. Please try again.”;

}

}

}

}

Create template file for this index.phtml in application/views/scripts/index/ Directory

<?

if(isset($this->errorMessage)){

echo $this->errorMessage;

}?>

<?

echo $this->form;

?>

<a href='<?php $i=$this->baseUrl().”/index/signup”; echo $i; ?>’>New users click here?</a>

11.Now add signupAction fuction to the IndexController.php

public function signupAction()

{

$users = new Users();

$form = new RegistrationForm();

$this->view->form=$form;

if($this->getRequest()->isPost()){

if($form->isValid($_POST)){

$data = $form->getValues();

if($data[‘password’] != $data[‘confirmPassword’]){

$this->view->errorMessage = “Password and confirm password don’t match.”;

return;

}

unset($data[‘confirmPassword’]);

$users->insert($data);

$this->_redirect(‘index/index’);

}

}

}

 

Create template file for this signup.phtml in application/views/scripts/index/ Directory

<?

if(isset($this->errorMessage)){

echo $this->errorMessage;

}?>

<h4>Sign up</h4>

<?

echo $this->form;

?>

 

12.Now add logoutAction fuction to the IndexController.php

 

public function logoutAction()

{

$storage = new Zend_Auth_Storage_Session();

$storage->clear();

$this->_redirect(‘index/index’);

}

 

13.Our model class is placed in applications/models/ Directory

Users.php

<?php

class Users extends Zend_Db_Table

{

public function checkUnique($username)

{

$select = $this->_dbTable->select()

->from($this->_name,array(‘username’))

->where(‘username=?’,$username);

$stmt = $select->query();

$result = $stmt->fetchAll();

if($result){

return true;

}

return false;

}

}

?>

 

14.Now you can run your project by put this URL on your browser bar

http://localhost/project_name/index

 

15.Thankiew