Sanitize data coming out of the database in cakephp 3

A security measure that most programmers forget to take when building database oriented applications is to sanitize-validate data coming out of the database. Let’s see how can we sanitize our data in cakePHP 3.

cakePHP 3 book – Entities

Create your Entity for ex. Country:

namespace AppModelEntity;
 
use CakeORMEntity;
 
class Country extends Entity
{
 
    protected $_accessible = [
        'name' => true,
        'code' => true,
        '*' => false,
    ];
 
    protected function _getCode($code)
    {
        return substr(preg_replace('/[^A-Z]/', '', strtoupper($code)), 0, 2);
    }
 
    protected function _getName($name)
    {
        return ucwords(substr(preg_replace('/[^a-zA-Z();' ]/', '', $name), 0, 50));
    }
}
Categories: CakePHP, PHP Tags: , , ,
%d bloggers like this: