Slide 1

Slide 1 text

PHP modern & secure

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

Who is this guy? Ben Edmunds Open Source Author PHP Town Hall Podcast CTO at Mindfulware

Slide 4

Slide 4 text

Welcome to the Future

Slide 5

Slide 5 text

Welcome to the Future Exceptions Namespaces Closures

Slide 6

Slide 6 text

Welcome to the Future Statics PDO Short Arrays Security

Slide 7

Slide 7 text

Legit Tools

Slide 8

Slide 8 text

Legit Tools Built-in Server Unit Testing Composer

Slide 9

Slide 9 text

Welcome to the Future

Slide 10

Slide 10 text

Great Scott!

Slide 11

Slide 11 text

Exceptions

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

Exceptions try { //your code goes here } catch (Exception $e) { die($e->getMessage()); }

Slide 14

Slide 14 text

Exceptions try { //your code goes here } catch (Exception $e) { die($e->getMessage()); }

Slide 15

Slide 15 text

Closures

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

Closures Route::get(‘/', function(){ return View::make(‘index'); });

Slide 18

Slide 18 text

Closures Route::get(‘/', function(){ return View::make(‘index'); });

Slide 19

Slide 19 text

Namespaces

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

Namespaces namespace Illuminate\Console; class Command { //…

Slide 22

Slide 22 text

Namespaces use Illuminate\Console\Command; namespace Illuminate\Console; class Command { //…

Slide 23

Slide 23 text

Namespaces use Illuminate\Console\Command; namespace Illuminate\Console; class Command { //…

Slide 24

Slide 24 text

Statics

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

Statics Class Route { public static function get() { //… }

Slide 27

Slide 27 text

Statics Route::get(); Class Route { public static function get() { //… }

Slide 28

Slide 28 text

Statics Route::get(); Class Route { public static function get() { //… }

Slide 29

Slide 29 text

Statics NO $this $var = self::varAtDefinition; $var = static::varAtExec;

Slide 30

Slide 30 text

Short Array Syntax

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

Short Array Syntax $array = array( 0 => ‘value1’, 1 => ‘value2’, );

Slide 33

Slide 33 text

Short Array Syntax $array = [ 0 => ‘value1’, 1 => ‘value2’, ];

Slide 34

Slide 34 text

Short Array Syntax $array = [ 0 => ‘value1’, 1 => ‘value2’, ];

Slide 35

Slide 35 text

Traits

Slide 36

Slide 36 text

Traits // grouping without // strict inheritance trait baseUser { function getName() { return ‘Jon Snow’; } }

Slide 37

Slide 37 text

Traits class adminUser { use baseUser; }

Slide 38

Slide 38 text

Traits $adminUser = new adminUser; echo $adminUser->getName(); //output = ‘Jon Snow’

Slide 39

Slide 39 text

PDO

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

PDO Cross System

Slide 42

Slide 42 text

PDO Cross System MS SQL MySQL Oracle PostgreSQL SQLite CUBRID Firebird Informix ODBC & DB2 4D

Slide 43

Slide 43 text

PDO Cross System Safe Binding

Slide 44

Slide 44 text

PDO $stmt = $db->prepare(‘ SELECT * FROM users WHERE id=:id ’); $stmt->bindParam(‘:id’, $id); $stmt->execute();

Slide 45

Slide 45 text

Security

Slide 46

Slide 46 text

Security SQL Injection HTTPS Password Hashing

Slide 47

Slide 47 text

Security Authentication Safe Defaults XSS & CSRF

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

Security //escaping input $stmt->bindParam(‘:id’, $id); //escaping output htmlentities($_POST[‘name’], ENT_QUOTES, ‘UTF-8’);

Slide 51

Slide 51 text

Security HTTPS / SSL Encrypts traffic across the wire Trusted sender and receiver Required by OAUTH 2

Slide 52

Slide 52 text

Security //authentication - access control if (!$user->inGroup(‘admin’)) { return ‘ERROR YO’; }

Slide 53

Slide 53 text

Security //authentication - brute force if ($user->loginAttempts > 5) { return ‘CAUGHT YA’; }

Slide 54

Slide 54 text

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

Slide 55

Slide 55 text

Security //safe password hashing password_hash($_POST['pass']); //password verification password_verify($_POST['pass'], $u->pass);

Slide 56

Slide 56 text

Security //safe defaults class Your Controller { protected $var1 = ‘default value’; function __construct() { … } }

Slide 57

Slide 57 text

Security //safe defaults $something = false; foreach ($array as $k => $v) { $something = $v->foo; if ($something == ‘bar’) { … } }

Slide 58

Slide 58 text

Security //Non-Persistent XSS http://www.yourSite.com/ ?page_num=2&per_page=50 Send the link to someone, boom

Slide 59

Slide 59 text

Security //Persistent XSS Same idea, except with data that is saved to the server and re-displayed

Slide 60

Slide 60 text

Security //XSS Protection

Title

Hello

Slide 61

Slide 61 text

Security //Cross Site Request Forgery //(CSRF) http://yourSite.com/ users/12/delete

Slide 62

Slide 62 text

Security //CSRF Protection POST / PUT / UPDATE / DELETE behind forms with one-time use tokens

Slide 63

Slide 63 text

Security //CSRF Protection function generateCsrf() { $token = mcrypt_create_iv( 16, MCRYPT_DEV_URANDOM); Session::flash('csrfToken', $token); return $token; }

Slide 64

Slide 64 text

Security //CSRF Protection if ( $_POST['token'] == Session::get(‘csrfToken') ) { … }

Slide 65

Slide 65 text

Legit Tools

Slide 66

Slide 66 text

No content

Slide 67

Slide 67 text

Built-in Web Server

Slide 68

Slide 68 text

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

Slide 69

Slide 69 text

Composer

Slide 70

Slide 70 text

Another Package Manager!?

Slide 71

Slide 71 text

Composer Sane Package Management

Slide 72

Slide 72 text

Composer Autoloading

Slide 73

Slide 73 text

Composer PEAR, ha! packagist.org

Slide 74

Slide 74 text

Composer / composer.json { "require": { "stripe/stripe-php": "dev-master", "twilio/sdk": "dev-master" } }

Slide 75

Slide 75 text

Composer $ php composer.phar update $ php composer.phar install

Slide 76

Slide 76 text

Composer $client = new Services_Twilio($sid, $tkn); $client->account ->messages ->sendMessage(…)

Slide 77

Slide 77 text

Unit Testing

Slide 78

Slide 78 text

No content

Slide 79

Slide 79 text

Unit Testing PHPUnit Behat Mink Selenium CodeCeption PHPSpec

Slide 80

Slide 80 text

Unit Testing class ApiAuthTest extends PHPUnit_Framework_TestCase { public function testVerify() { $auth = new apiAuth(); $this->assertTrue($auth->verify());

Slide 81

Slide 81 text

Unit Testing class ApiAuthTest extends PHPUnit_Framework_TestCase { public function testVerify() { $auth = new apiAuth(); $this->assertTrue($auth->verify());

Slide 82

Slide 82 text

Unit Testing $ phpunit tests PHPUnit 3.3.17 by Sebastian Bergmann. Time: 0.01 seconds OK (1 tests, 1 assertions)

Slide 83

Slide 83 text

Resources

Slide 84

Slide 84 text

No content

Slide 85

Slide 85 text

Resources PHP.net

Slide 86

Slide 86 text

Resources Modern Frameworks Laravel Symfony2 Fuel PHP SlimPHP 2 Aura for PHP Silex

Slide 87

Slide 87 text

Resources leanpub.com/ phptherightway PHPtheRightWay.com

Slide 88

Slide 88 text

Resources BuildSecurePHPapps.com Coupon Code: zendcon 20% off / 5$ off http://buildsecurephpapps.com/?coupon=zendcon

Slide 89

Slide 89 text

Q/A TIME! Ben Edmunds @benedmunds http://benedmunds.com http://buildsecurephpapps.com/?coupon=zendcon http://joind.in/15620