Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Save to My Library
Look up keyword
Like this
6Activity
0 of .
Results for:
No results containing your search query
P. 1
Zend Controller Failed to Log In

Zend Controller Failed to Log In

Ratings: (0)|Views: 3,487|Likes:
Published by ebookpdfreader

More info:

Published by: ebookpdfreader on Mar 16, 2009
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

09/05/2010

pdf

text

original

 
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.
NOTE:
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
session.save_path 
is writableby the web server otherwise you will get a very odd exception thrown!
Authentication
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
users 
Table
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');
 
Page 2 of 7
Run these statements in a MySQL client such as phpMyAdmin or the standard MySQLcommand-line client. (Obviously, you should pick a better username and password!)
Bootstrap Changes
In order to keep track of the fact that the user is logged in, we are going to use the session.The Zend Framework provides Zend_Session_Namespace which provides a nice objectoriented interface to the session.The changes to index.php are:
zf-tutorial/index.php:
...Zend_Loader::loadClass('Zend_Db_Table');Zend_Loader::loadClass('Zend_Debug');
Zend_Loader::loadClass('Zend_Auth');
// load configuration...
and
..// setup database$db = Zend_Db::factory($config->db->adapter,$config->db->config->toArray());Zend_Db_Table::setDefaultAdapter($db);
Zend_Registry::set('db', $db);
// setup controller$frontController = Zend_Controller_Front::getInstance();...
All we actually have to do here is ensure we have loaded the Zend_Auth class and registeredthe database adapter with the registry. We store it to the registry as we need it when we dothe authentication later.
The Auth Controller
We need a controller to hold the login and logout actions. It makes sense to call itAuthController.We’ll start it with the basics from the IndexController:
zf-tutorial/application/controllers/AuthController.php:
<?phpclass AuthController extends Zend_Controller_Action{function init(){$this->initView();$this->view->baseUrl = $this->_request->getBaseUrl();}function indexAction(){$this->_redirect('/');}}
We set up init() so that the view is initialised and that baseUrl is assigned to it. We also createan indexAction() function as required by Zend_Controller_Action. We don’t need indexAction()though as we are going to use loginAction() and logoutAction(), so we just redirect back to thedefault if someone does navigate to auth/index.
 
Page 3 of 7
Logging In
To log into the application we need a form, so the login action is going to work in a mannervery similar to the other forms in IndexController. The form template will live inviews/scripts/auth/login.phtml and the code will be in AuthController::loginAction(). The form isvery simple, requiring just two fields: username and password:
zf-tutorial/application/views/scripts/auth/login.phtml:
<?php echo $this->render('header.phtml'); ?><h1><?php echo $this->escape($this->title); ?></h1><?php if(!empty($this->message)) :?><div id="message"><?php echo $this->escape($this->message);?></div><?php endif; ?><form action="<?php echo $this->baseUrl ?>/auth/login" method="post"><div><label for="username">Username</label><input type="text" name="username" value=""/></div><div><label for="password">Password</label><input type="password" name="password" value=""/></div><div id="formbutton"><input type="submit" name="login" value="Login" /></div></form><?php echo $this->render('footer.phtml'); ?>
The template renders the header.phtml and footer.phtml at the top and bottom as usual. Notethat we display a message only if
$this->message
is not empty. This is used to tell the userthat they failed to log in. The rest of the template is the log in form itself.Now that we have a form, we need to create the controller action that will run it. This is addedto AuthController.php:
zf-tutorial/application/controllers/AuthController.php:
class AuthController extends Zend_Controller_Action{...
function loginAction(){$this->view->message = '';$this->view->title = "Log in";}
}
Initially, all we need to do is set the title and message and then render the form. If younavigate to http://zf-tutorial/auth/login then you should see the login form.What about processing the form after it has been submitted though? To do that, we utilise thesame trick as in the add and edit forms in the IndexController and do the process if therequest method is a post. Change the loginAction() that we have just created:

Activity (6)

You've already reviewed this. Edit your review.
1 hundred reads
1 thousand reads
Tuan Dao Duy liked this
oma0000 liked this
sksbd liked this

You're Reading a Free Preview

Download
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->