Page 1 of 7
Getting Started with Zend_Auth
By Rob Allen,www.akrabat.com Document Revision 1.0.8 Copyright © 2007
This tutorial is intended to give a very basic introduction to using the Zend_Auth componentwith the Zend Framework. It builds on the previous tutorial “Getting Started with the ZendTutorial” which is available athttp://akrabat.com/zend-framework-tutorial.
This tutorial has been tested on versions 1.0.0 and 1.0.1 of the Zend Framework. Itstands a very good chance of working with later versions.
Before We Get Started
The implementation of authentication that we are going to use in this tutorial uses PHP’ssessions. Make sure that the directory set in your php.ini file’s
is writableby the web server otherwise you will get a very odd exception thrown!
For the purposes of this tutorial, authentication is the process of logging someone into a webapplication. We are going to modify the CD listing application created in “Getting Started withthe Zend Framework” to require log in before accessing any part of the application.Loosely, the things we need to do are:
Create a database table for users (and populate it with a user!).
Create a login form.
Create a controller containing actions for logging in and logging out.
Alter the footer to allow for log out
Ensure that the user is logged in before allowing access to the application.
The first thing we need is a users table. It doesn’t need to be complicated, so the schemalooks like this:
Fieldname Type Null? Notes
id Integer No Primary key, Autoincrementusername Varchar(50) No Unique keypassword Varchar(50) Noreal_name Varchar(100) NoWhen using MySQL, the SQL statement to create the table is:
CREATE TABLE users (id int(11) NOT NULL auto_increment,username varchar(50) NOT NULL,password varchar(50) NOT NULL,real_name varchar(100) NOT NULL,PRIMARY KEY (id),UNIQUE KEY username (username));
We also need a user that can log in:
INSERT INTO users (id, username, password, real_name)VALUES (1, 'rob', 'rob', 'Rob Allen');