phpeveryday.com

The best tutorial of php, php framework, php strategies, object oriented oriented,


Zend Framework Login: Protected Page

Tag: framework, zend, zend framework, authentication, Zend_Auth   Category: PHP Framework
post: 22 Apr 2008 read: 1,904


Zend Framework Login System Step by Step Tutorial - Part 5: We have created authentication at previous post. Now, we try to protect a page. Thus, when not member try to enter the page, they will be redirected to login form.

First, create view. Create a file named "userpage.phtml" within application/views/scripts/user. Enter following code:


<? include "header.phtml"; ?>
<h1>Hello, <?=$this->escape($this->username);?></h1>
<a href='<?=$this->escape($this->urllogout);?>'>Logout</a>
<? include "footer.phtml"; ?>

Then, we add method at controller. Open "UserController.php". Add following method:


public function userPageAction(){
    $auth		= Zend_Auth::getInstance(); 
	
	if(!$auth->hasIdentity()){
	  $this->_redirect('/user/loginform');
	}
  
    $request = $this->getRequest(); 
	$user		= $auth->getIdentity();
	$real_name	= $user->real_name;
	$username	= $user->username;
	$logoutUrl  = $request->getBaseURL().'/user/logout';

	$this->view->assign('username', $real_name);
	$this->view->assign('urllogout',$logoutUrl);
}

Line 2 - 6 we needed to protect page from unlogin users.

Try to login. You can point your browser to http://localhost/test/zend/helloworld/web_root/user/loginform. Then login.



Series this article:
Zend Framework Login: Preparing Database
Zend Framework Login: Creating Form Login
Zend Framework Login: Creating Authentication
Zend Framework Login: Fatal error Cannot use object of type stdClass as array
Zend Framework Login: Protected Page
Zend Framework Login: Creating Logout
Zend Framework Login: Creating Switching for Front Page

| Give Your Opinion | Recommend
Share and Bookmark to: These icons link to social bookmarking sites where readers can share and discover new web pages.
digg del.icio.us technorati Ma.gnolia BlinkList

Recommended articles by other readers:
Web Services: How PHP Kiss VB.NET? (Part 1)
Chart: How to Build Cool Animation Real Time Chart
Joomla: Fast Road to Understand Component Programming
Email: Send Attachement Mail
mod_rewrite - Part 1: create your "fantasy" URL

What do You Think?
Your Name *:
Email *:
(Will not be published)
Website/URL:
Your Comment *:
* Required


615
posting