Virginia Beach Web Development | Website Developer | Mobile Dev | Mobile Video Integration | Doodersrage

Just about everything Website Development related. Virginia Beach VA headquartered with a global reach!

Zend Framework User Authentication with Action Helper

Posted on | November 8, 2011 | 2 Comments

What happens if you would like to use an authentication method for a chunk of controllers but would not like to replicate the code in each and every controller for your web development application? Zend Framework Action Helper to the rescue! Simply move your authentication code to an Action Helper then reference that helper in the init method of the controller you would like to require authentication. Cannot get much simpler than that!

class Action_Helper_Auth extends Zend_Controller_Action_Helper_Abstract
{
    function direct()
    {

		$storage = new Zend_Auth_Storage_Session();
        $data = $storage->read();
        if(empty($data)){
			$controller = $this->getRequest()->getControllerName();
			$action = $this->getRequest()->getActionName();
			if($action != 'login'){
				$front = Zend_Controller_Front::getInstance();
				if($front->getRequest()->getControllerName() != 'admin_login'){
					$redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('Redirector');
					$redirector->gotoSimple('index','admin_login');
				}
			}
        } else {
			$usersGroups = new Application_Model_UsersGroups;
			$usersGroupsMapper = new Application_Model_UsersGroupsMapper;

			$usersGroupsMapper->find($data[group], $usersGroups);

			$rest = unserialize($usersGroups->getRestrictions());
		}

	return $rest;
    }
}

Then to force authentication of the user, add this line to your init method of your desired controller:

$this->_helper->auth();

The helper uses a reference to the Zend_Controller_Action_HelperBroker in order to redirect the user should they be found not to be an authenticated user. If you would like to do redirects using the redirect action helper this is the way it should be done from within an action helper.

More on Zend Authentication

Zend Framework User Authentication

Zend Framework User Authentication

Posted on | November 8, 2011 | 1 Comment

You’re building a web application with the Zend Framework and want a user controllable back-end system that allows for any number of users. To do this you will want to make use of Zend_Auth, which provides an API for authentication.

This is the basic way to retrieve Zend_Auth session storage data:

$storage = new Zend_Auth_Storage_Session();
$data = $storage->read();

This will retrieve whatever authenticated user session data has been stored.

But How do I authenticate and store this information to begin with?

$users = new Application_Model_Users;
		$usersMapper = new Application_Model_UsersMapper;
		$dbAdapter = Zend_Db_Table::getDefaultAdapter();
        if($this->getRequest()->isPost()){
            if($form->isValid($_POST)){
                $data = $form->getValues();
                $auth = Zend_Auth::getInstance();
                $authAdapter = new Zend_Auth_Adapter_DbTable($dbAdapter,'users');
                $authAdapter->setIdentityColumn('username')
                            ->setCredentialColumn('password');
                $authAdapter->setIdentity($data['username'])
                            ->setCredential($this->_helper->pass($data['password']));
                $result = $auth->authenticate($authAdapter);
                if($result->isValid()){
                    $storage = new Zend_Auth_Storage_Session();
                    $storage->write($authAdapter->getResultRowObject());
                    $this->_helper->redirector('index','admin_index');
                } else {
                    $this->view->errorMessage = "
Invalid username or password. Please try again.
";
                }
            }
        }

In the above example the the Users model and mapper if referenced then set to the auth adapter db table. The identity and credential column are assigned to usersname and password respectfully. If the user is then found to be valid based on the provided data they are then granted a session storage and redirected to the admin index page which will then give them the chance to modify any available admin values. If invalid user data is entered the user is presented with an error message.

So what does this all do??? More on that later!

More on Zend Authentication

Zend Framework User Authentication with Action Helper

Zend Framework Action Helpers

Posted on | November 1, 2011 | No Comments

When developing websites that use quite a bit of server side code frameworks come in handy to save time from rolling your own format parser to simplifying the sending of emails. When working with PHP the standard is pretty much the Zend Framework provided Zend Technologies which maintains the PHP language. They also have a bunch of other neat products like the Zend IDE, and Zend Server which provides an optimized server stack for quick and easy usage. As your code stacks up you will want quick and easy ways to use commonly accessed functions and classes. With action helpers you can easily achieve this solution.

Action controllers are by default stored within your /application/controllers/helpers/ directory. To create a new controller simply create a new file in this directory then give it a name that will also be the name of the initializing class. A simple example would be Foo.php. Make sure your first letter has been capitalized! The framework will not be able to find your file if your file-name is in all lowercase!

Next it is time to create your first helper class. A simple example can be viewed below.

class Action_Helper_Foo extends Zend_Controller_Action_Helper_Abstract {

function direct(){

echo 'I\'m a helper!';

}

}

You can now access this helper from within any of your controllers by putting this code within any action or function.

$this->_helper->foo()

I you would like to get meta and open a helper within a helper the method below is typically your best route.

$foobar = Zend_Controller_Action_HelperBroker::getStaticHelper('Foo');
$view->foo = $foobar->direct('aHa!');

That’s all for now! Will cover this topic more in-depth in the future!

Document compression with IIS 7 and Apache

Posted on | June 21, 2011 | No Comments

Have been using this method for years in heavy load environments but have just recently moved to using it with pretty much every build instance. The increased use of JavaScript in todays web development efforts pretty much require some sort of compression in order to speed up page loads and cut off those few precious seconds you would like to keep your users there.

There are a few ways to enable gzip compression within Apache. The simplest would be to set the values within your HTACCESS file with the Apache module mod_deflate.

With mod_deflate installed to Apache 2 you could enable it in your HTACCESS file by entering something like what is on the next line.

AddOutputFilterByType DEFLATE text/html text/plain text/xml

The above reads “AddOutputFilterByType DEFLATE” + mime-type + mime-type + …

If you would like to compress all documents within your selected directory just add the below to your HTACCESS file.

SetOutputFilter DEFLATE

More neat tweaks on the Apache Org site itself!

IIS 7:

Dynamic page compression by default is not enabled. In most cases IIS 7 will only have static file compression enabled, this is mostly due to the process consuming more server resources. Don’t let that scare you! With this method enabled you can save your users anywhere from 30-80% of the typical document download size!

To add dynamic compression to your server go to role management within server management then choose add new role function and enable “Dynamic Compression”. Once enabled you will have a new option for compression settings within IIS. Select this option then enable Dynamic compression. If you would like all sites to use this method, enable it on a global scope within the primary IIS config. If you would like to only have it enabled on specific sites, browse to the site you would like it enabled then enable it within the compression settings. Alternately you can also configure compression within your web.config file by adding the “<urlCompression>” procedure.

More on this at Microsoft Technet!

Above are only a few ways to enable document compression within your website. Many more exist! If a project is causing a headache, check your page request file sizes then possibly give document compression a try. By today’s demanding and overly visual development standards, you should likely make this common practice whenever working on any kind of web development project!

« go backkeep looking »
  • Social

    twitter facebook flickr google youtube stumble upon reddit digg
  • Portfolio

  • Twitter

  • My Tumblr

    • photo from Tumblr

      Medium rare filet mignon topped with melted butter.


    • photo from Tumblr

      Sweaty mushrooms.


  • Alexa Rank