Category: PHP

All about writing secure PHP code!

Apache, Linux, MySQL, PHP, Ubuntu Server

Install LAMP stack on Ubuntu 16.04

Update system sudo apt-get update && sudo apt-get dist-upgradesudo apt-get update && sudo apt-get dist-upgrade Install Apache sudo apt-get install apache2sudo apt-get install apache2 Install MySQL sudo apt-get install mysql-serversudo apt-get install mysql-server Install PHP sudo apt-get install php libapache2-mod-php php-mcrypt php-mysql php-mbstring php-gettextsudo apt-get install php libapache2-mod-php php-mcrypt php-mysql php-mbstring php-gettext Install PHPMyAdmin sudo …

CakePHP, PHP

List table columns in cakephp 3

List table columns $db = \Cake\Datasource\ConnectionManager::get(’default’); $collection = $db->schemaCollection(); $table= $collection->describe(’tablename’); $columns = $table->columns();$db = \Cake\Datasource\ConnectionManager::get(‘default’); $collection = $db->schemaCollection(); $table= $collection->describe(‘tablename’); $columns = $table->columns();

PHP

OpenSSL AES 256 CBC encryption in PHP

define(’AES_METHOD’, ‘AES-256-CBC’);   class AES256 { public function Encrypt($password, $plainText) { if(empty($password) || empty($plainText)) { return FALSE; }   //generate a random salt $Salt = openssl_random_pseudo_bytes(8); if($Salt === FALSE){ return FALSE; }   //generate a random initialization vector $IV = openssl_random_pseudo_bytes( openssl_cipher_iv_length( AES_METHOD)); if($IV === FALSE){ return FALSE; }   //generate aes key $pwd = …

CakePHP, PHP

CakePHP 3 – Format Datetime in words

To use TimeHelper you have to enable it first. To enable a helper in your view, add the name of the helper to the controller’s $helpers array: class MyController extends AppController { public $helpers = [’Time’]; }class MyController extends AppController { public $helpers = [‘Time’]; } Each helper is exposed as a public property in …

C#, PHP

Encrypting communication between C# and PHP with Rijndael 256

PHP Encryption $iv_to_pass_to_decryption = ”; function encrypt($text, $key) { $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC), MCRYPT_RAND); $iv_to_pass_to_decryption = base64_encode($iv); return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_CBC, $iv); }$iv_to_pass_to_decryption = ”; function encrypt($text, $key) { $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC), MCRYPT_RAND); $iv_to_pass_to_decryption = base64_encode($iv); return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_CBC, $iv); } Decryption function decrypt($text, $key, $iv) { $text = base64_decode($text); $iv …

CakePHP, PHP

Simple JSON response with CakePHP 3

CakePHP 3 Book – Chapter JSON and XML views Controller code: namespace App\Controller;   class MyJsonController extends AppController { public function initialize() { parent::initialize(); $this->loadComponent(’RequestHandler’); }   public function index() { $customers[’id’] = ‘123’; $this->set(’customers’, $customers); $this->set(’_serialize’, [’customers’]); } }namespace App\Controller; class MyJsonController extends AppController { public function initialize() { parent::initialize(); $this->loadComponent(‘RequestHandler’); } public function …

CakePHP, PHP

Custom Finder Method in CakePHP 3

In your Table Model create a method and name it, findMymethod, ‘find’ prefix is mandatory, for ex. public function findOffline(Query $query, array $options) { $query->where([ ‘online’ => false, ]); return $query; }public function findOffline(Query $query, array $options) { $query->where([ ‘online’ => false, ]); return $query; } Usage: $customers = TableRegistry::get(’Customers’); $query = $customers->find(’offline’); //or $query …

CakePHP, PHP

How to limit records per page from view using CakePHP 3

Place the following code in your view: <div id="recperpage"> <?=$this->Form->create(null, [ ‘url’ => [’controller’ => ‘mycontroller’, ‘action’ => ‘index’], ‘id’ => ‘myformid’, ])?> Records per page: <?=$this->Form->select(’recperpageval’, [5=>5, 10=>10, 20=>20, 50=>50, 100=>100, 250=>250, 500=>500], [’default’ => 20, ‘onchange’=>’onSelectSubmit("myformid")’] ) ?> <?=$this->Form->end()?> </div><div id="recperpage"> <?=$this->Form->create(null, [ ‘url’ => [‘controller’ => ‘mycontroller’, ‘action’ => ‘index’], ‘id’ => …

CakePHP, PHP

How to enable Paginator in CakePHP 3

Let’s see how can we enable and initialize the CakePHP built-in Paginator Component so we can use it in our views. class MyController extends AppController { //Loading Templates from a File //This will load the file located at config/paginator-templates.php public $helpers = [ ‘Paginator’ => [’templates’ => ‘paginator-templates’] ];   //Define the default query conditions …

CakePHP, PHP

Get MySQL version in CakePHP 3

You need to import ConnectionManager: use CakeDatasourceConnectionManager;use CakeDatasourceConnectionManager; $stmt = ConnectionManager::get(’default’)->query(’SELECT version() as v’); $stmt->execute(); echo $stmt->fetch(’assoc’)[’v’];$stmt = ConnectionManager::get(‘default’)->query(‘SELECT version() as v’); $stmt->execute(); echo $stmt->fetch(‘assoc’)[‘v’]; More on executing CakePHP custom queries, here.