PHP
modern
not your grandma’s php
& secure
Who is this guy?
Ben Edmunds	

!
@benedmunds	

http://benedmunds.com
Who is this guy?
Ben Edmunds	

!
Open Source 	

Author	

PHP Town Hall Podcast	

CTO at Mindfulware
Welcome to
the Future
Welcome to the Future
Exceptions
Namespaces
Closures
Welcome to the Future
Statics
PDO
Short Arrays
Security
Legit Tools
Legit Tools
Built-in Server
Unit Testing
Composer
Welcome to!
the Future
Great Scott!
Exceptions
Exceptions
try {

	 	 //your code goes here

}

catch (Exception $e) {

	 	 die($e->getMessage());

}
Exceptions
try {

	 	 //your code goes here

}

catch (Exception $e) {

	 	 die($e->getMessage());

}
Closures
Closures
Route::get(‘/', function(){

	 

return View::make(‘index');

!
});
Closures
Route::get(‘/', function(){

	 

return View::make(‘index');

!
});
Namespaces
Namespaces
namespace IlluminateConsole;

class Command

{

	 //…
Namespaces
use IlluminateConsoleCommand;
namespace IlluminateConsole;

class Command

{

	 //…
Namespaces
use IlluminateConsoleCommand;
namespace IlluminateConsole;

class Command

{

	 //…
Statics
Statics
Class Route {

	 public static function get() {

	 	 //… 

	 }
Statics
Route::get();
Class Route {

	 public static function get() {

	 	 //… 

	 }
Statics
Route::get();
Class Route {

	 public static function get() {

	 	 //… 

	 }
Statics
	 	 NO $this

	 

	 	 $var = self::varAtDefinition;

!
	 	 $var = static::varAtExec;
Short Array!
Syntax
Short Array Syntax
$array = array(

	 0 => ‘value1’,

	 1 => ‘value2’,

);
Short Array Syntax
$array = [

	 0 => ‘value1’,

	 1 => ‘value2’,

];
Short Array Syntax
$array = [

	 0 => ‘value1’,

	 1 => ‘value2’,

];
PDO
PDO
Cross System
PDO
Cross System
MS SQL

MySQL

Oracle

PostgreSQL

SQLite
CUBRID

Firebird

Informix

ODBC & DB2

4D
PDO
Cross System
Safe Binding
PDO
$stmt = $db->prepare(‘

	 SELECT * FROM users

	 WHERE id=:id

’);

!
$stmt->bindParam(‘:id’, $id);

$stmt->execute();
Security
Security
SQL Injection
HTTPS
Password Hashing
Security
Authentication
Safe Defaults
XSS & CSRF
Security
//escaping input

$stmt->bindParam(‘:id’, $id);
Security
//escaping input

$stmt->bindParam(‘:id’, $id);
//escaping output

htmlentities($_POST[‘name’]);
Security
HTTPS / SSL

!
Encrypts traffic across the wire

!
Trusted sender and receiver

!
Required by OAUTH 2
Security
//authentication - access control

if (!$user->inGroup(‘admin’)) {

	 return ‘ERROR YO’;

}
Security
//authentication - brute force

if ($user->loginAttempts > 5) {

	 return ‘CAUGHT YA’;

}
Security
//safe password hashing

password_hash($_POST['pass']);
Security
//safe password hashing

password_hash($_POST['pass']);
//password verification

password_verify($_POST['pass'], $u->pass);
Security
//safe defaults

class Your Controller {

	 protected $var1 = ‘default value’;

!
	 function __construct() { … }

}
Security
//safe defaults

$something = false;

!
foreach ($array as $k => $v) {

	 $something = $v->foo;

	 if ($something == ‘bar’) { … }

}
Security
//Non-Persistent XSS

!
http://www.yourSite.com/

?page_num=2&per_page=50

!
Send the link to someone, boom
Security
//Persistent XSS

!
Same idea, except with data that is
saved to the server and 

re-displayed
Security
//XSS Protection

!
<h1>Title</h1>

Hello <?=htmlentities($name)?>

!
!
Security
//Cross Site Request Forgery

//(CSRF)

!
http://yourSite.com/

users/12/delete

!
!
Security
//CSRF Protection

!
POST / PUT / UPDATE / DELETE

behind forms with one-time use
tokens

!
!
Security
//CSRF Protection

!
function generateCsrf() {

$token = mcrypt_create_iv(

16, MCRYPT_DEV_URANDOM);

Session::flash('csrfToken', $token);

return $token; 

}
Security
//CSRF Protection

!
if (

$_POST['token'] == Session::get(‘csrfToken')

) { … }
!
Legit Tools
Built-in !
Web Server
Built-in Server
$ php -S localhost:8000

!
PHP 5.4.0 Development Server started…
Listening on localhost:8000

Document root is /home/ben/htdocs

Press Ctrl-C to quit
Composer
Another
Package Manager!?
Composer
Sane Package

Management
Composer
Autoloading
Composer
PEAR, ha!
packagist.org
Composer
/ composer.json

!
{

	 "require": {

	 	 "stripe/stripe-php": "dev-master",

"twilio/sdk": "dev-master"

	 }

}
Composer
$ php composer.phar update
$ php composer.phar install
Composer
$client = 

new Services_Twilio($sid, $tkn);
!
$client->account

->messages

->sendMessage(…)
Unit Testing
Unit Testing
PHPUnit

Behat

Mink
Selenium

CodeCeption

PHPSpec
Unit Testing
class ApiAuthTest extends PHPUnit_Framework_TestCase {

!
public function testVerify() {

!
	 $auth = new apiAuth();

	 	 

	 $this->assertTrue($auth->verify());
Unit Testing
class ApiAuthTest extends PHPUnit_Framework_TestCase {

!
public function testVerify() {

!
	 $auth = new apiAuth();

	 	 

	 $this->assertTrue($auth->verify());
Unit Testing
$ phpunit tests

!
PHPUnit 3.3.17 by Sebastian Bergmann.

Time: 0.01 seconds

OK (1 tests, 1 assertions)
Resources
Resources
PHP.net
Resources
Modern Frameworks
Laravel

Symfony2

Fuel PHP
SlimPHP 2

Aura for PHP

Silex
Resources
leanpub.com/

phptherightway
PHPtheRightWay.com
Resources
BuildSecurePHPapps.com
Coupon Code:

codementor
$3 off
http://buildsecurephpapps.com/?coupon=codementor
Q/A TIME!
Ben Edmunds	

@benedmunds	

http://benedmunds.com
http://buildsecurephpapps.com/?coupon=codementor

Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds