Saturday, February 28, 2009

Encoding - UTF8

How to make sure the whole site is in UTF-8.

Send the following header.
  1. header('Content-Type: text/html; charset=utf-8');  

Can be done with Zend Framework the following way in bootstrap:
  1. $response = new Zend_Controller_Response_Http;  
  2. $response->setHeader('Content–Type''text/html; charset=UTF-8'true);  
  3. self::$frontController->setResponse($response);  

  1. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />  

Setting the option form option:
  1. <form accept-charset="utf-8">  


In Zend_Form:
  1. $this->setAttrib('accept-charset''UTF-8');  

Making sure the database is also using UTF-8.
When setting up use:
  1. $db->query("SET NAMES 'utf8'");  

Zend_View can also be set to use UTF-8:
  1. $view = new Zend_View;  
  2. $view->setEncoding('UTF-8');  

Reference: http://www.phpwact.org/php/i18n/charsets

Thursday, February 26, 2009

Form ticket

This makes sure that the submitted form is from the same server.
  1.    
  2. class New_Form extends Zend_Form  
  3. {  
  4.     public function init() {  
  5.         $ticket = $this->_ticket();  
  6.         $sessionZendForm = new Zend_Session_Namespace('Zend_Form');  
  7.         $sessionZendForm->ticket = $ticket;  
  8.         $ticketElement = new Zend_Form_Element_Hidden('Form_Session_Ticket');  
  9.         $ticketElement->setValue($ticket);  
  10.         $this->addElement($ticketElement);  
  11.     }  
  12.   
  13.     public function isValid($data) {  
  14.         $ticketElement = $this->getElement('Form_Session_Ticket');  
  15.         if ($ticketElement != NULL) {  
  16.             $sessionZendForm = new Zend_Session_Namespace('Zend_Form');  
  17.             if ($sessionZendForm->ticket != $ticketElement->getValue()) {  
  18.                 throw new Zend_Form_Exception("Submitted form is not from this server.");  
  19.             }  
  20.         }  
  21.         return parent::isValid($data);  
  22.     }  
  23.   
  24.     protected function _ticket() {  
  25.         return md5(uniqid(rand(), true));  
  26.     }  
  27. }