Pro PHP- Patterns, Frameworks, Testing and More

(vip2019) #1

(^252) CHAPTER 16 ■ ADVANCED ZEND FRAMEWORK
public function TwoAction() {
$session = new Zend_Session_Namespace('AUniqueNamespace');
$this->view->key = $session->key;
}
If you visit your /session/one action, it will set the session variable and forward to the
/session/two location. If you reload on /session/two, the value will then become undefined,
as the number of expiry hops has been reached.
It is often desirable to regenerate session IDs regularly. This helps reduce the likelihood
that a session could be hijacked by a third party. For this, the Zend_Session class provides a
static regenerateId() method that will re-create a new session ID and associate it with the
client. By adding the following line to your bootstrap file, you can ensure that every page load
results in a new session ID being identified.:
Zend_Session::regenerateId();
The framework is smart enough not to regenerate the ID when a session does not exist,
and calling this method will not start a session that was not otherwise started.
■Tip Zend_Session also has the ability to allow you to write your own storage back-end. This can allow
you to create back-ends that work off memcached or a database and may improve your ability to cluster your
session-based applications. For more information, see the Zend Framework reference manual at http://
framework.zend.com.


Sending Mail


Most reasonably complex web applications need to generate all kinds of e-mail, for example to
send notice of a posting or a product order, or even just a daily report. The Zend Framework
offers an extensive mail-sending API.
The simplest implementation is sending a plain-text e-mail to a recipient via the local mail
relay. Listing 16-18 demonstrates sending a one-line e-mail message.

Listing 16-18. Sending Plain-Text Mail via the Local Relay

$mail = new Zend_Mail();
$mail->addTo('[email protected]', 'Firstname Lastname');
$mail->setFrom('[email protected]', 'example.com messaging system');
$mail->setSubject('The subject');
$mail->setBodyText('The content');
$mail->send();

The next step is to upgrade from plain text to HTML e-mail. You have two options:


  • If you want to send a multipart/alternative method (both plain text and HTML), add a
    setBodyHtml($html) call.

  • If you want a single-part, text/HTML message, replace the existing setBodyText() call.


McArthur_819-9C16.fm Page 252 Friday, February 29, 2008 5:07 PM

Free download pdf