diving into php

61
Diving into PHP Fast, Easy, Complicated, and Powerful Web ITP, Spring 2011, section 1, session 1 Dan Phiffer [email protected]

Upload: dan-phiffer

Post on 21-May-2015

7.946 views

Category:

Education


8 download

DESCRIPTION

Course lecture for Fast, Easy, Complicated, and Powerful Webhttp://fecpw.phiffer.org/

TRANSCRIPT

Page 1: Diving into php

Diving into PHPFast, Easy, Complicated, and Powerful Web

ITP, Spring 2011, section 1, session 1Dan Phiffer [email protected]

Page 2: Diving into php

Diving into PHP

Page 3: Diving into php

A simple content management system

1. Build a form for user input

2. Store submissions in a database

3. Retrieve submission data

Page 8: Diving into php

<?phpif (isset($_REQUEST["query"])) { echo $_REQUEST["query"];}?><form action="basic-form.php"> <input type="text" name="query" /> <input type="submit" name="button" value="Kablooey" /></form>

Solution: check if it’s set

Page 9: Diving into php

<?phpif (isset($_REQUEST['query'])) { echo "<h1>You wrote: '{$_REQUEST['query']}'</h1>";}?><form action="basic-form.php"> <input type="text" name="query" /> <input type="submit" name="button" value="Kablooey" /></form>

Dynamic strings

Page 10: Diving into php

Try it out

Page 11: Diving into php

<?php$query = "";if (isset($_REQUEST["query"])) { $query = $_REQUEST["query"]; echo "<h1>You wrote: '$query'</h1>";}?><form action="basic-form.php" > <input type="text" name="query" value="<?php echo $query; ?>" /> <input type="submit" name="button" value="Kablooey" /></form>

Defining a new variable

Page 12: Diving into php

Step 1 complete!

Page 13: Diving into php

Wait, this is bad

Page 14: Diving into php

User types input...

Page 15: Diving into php

Clicks away... arbitrary JavaScript execution!

Page 16: Diving into php

We’ve been tricked into adding an ‘onblur’ attribute!

Page 17: Diving into php

Cross-site scripting (XSS)

• A common security vulnerability

• When content is unintentionally executed as code

• We must handle user-submitted content very carefully

Page 18: Diving into php

Dangers of XSS

• Users’ sessions could be hijacked

• Passwords could be stolen

• Your site could get spammed up

• Puppies murdered, etc.

Page 19: Diving into php

<?php$query = "";if (isset($_REQUEST["query"])) { // htmlentities() turns " into &quot; $query = htmlentities($_REQUEST["query"]); echo "<h1>You wrote: '$query'</h1>";}?><form action="basic-form.php" > <input type="text" name="query" value="<?php echo $query; ?>" /> <input type="submit" name="button" value="Kablooey" /></form>

Escaping user input

Page 20: Diving into php

Before & after escaping

Page 21: Diving into php

Now we’re really finished with step 1

1. Build a form for user input

2. Store submissions in a database

3. Retrieve submission data

Page 22: Diving into php

Adding a database

Page 23: Diving into php

Relational databases

• Tables with columns and rows of individual data cells

• SQL is the language for working with relational databases

• MySQL is the database platform used by WordPress

Page 24: Diving into php

The four operations

• Create new rows with INSERT

• Read rows with SELECT

• Update rows with UPDATE

• Delete rows with DELETE

• MySQL documentation

Page 26: Diving into php

$ mysql -u root

Page 27: Diving into php

mysql> CREATE DATABASE

Page 28: Diving into php

-> tinydb CHARACTER SET utf8;

Page 29: Diving into php

mysql> USE tinydb;

Page 30: Diving into php

mysql> CREATE TABLE tinytable

Page 31: Diving into php

-> (id INTEGER PRIMARY KEY AUTO_INCREMENT);

Page 32: Diving into php
Page 33: Diving into php

mysql> ALTER TABLE tinytable ADD COLUMN

Page 34: Diving into php

-> content TEXT;

Page 35: Diving into php
Page 36: Diving into php
Page 37: Diving into php

mysql> INSERT INTO tinytable

Page 38: Diving into php

-> (id, content)

Page 39: Diving into php

-> VALUES (1, 'Hello, world!');

Page 40: Diving into php

mysql> SELECT * FROM tinytable;

Page 41: Diving into php

Let’s build a tiny wiki!

Page 42: Diving into php

A simple content management system

1. Build a form for user input

2. Store submissions in a database

3. Retrieve submission data

Page 43: Diving into php

A simple content management system

1. Build a form for user input

2. Store submissions in a database

3. Retrieve submission data

Page 44: Diving into php

<!DOCTYPE html><html> <head> <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> <title>Tiny wiki</title> </head> <body> <?php $content = ""; // We need to load the content! ?> <form action="tiny-wiki.php" method="post"> <input type="text" name="content" value="<?php echo $content; ?>" /> <input type="submit" value="Update" /> </form> </body></html>

Basic form

Page 45: Diving into php

<?php

$content = load_content();

function load_content() { // Load content from the database return "";}

?>

Add a load function

Page 46: Diving into php

<?php

$db = connect_to_database();$content = load_content($db);

function load_content($db) { // Load content from the database return "";}

function connect_to_database() { // Connect to the database}

?>

Add a database function

Page 47: Diving into php

function connect_to_database() { $host = "127.0.0.1"; $port = 8889; $user = "root"; $pass = "root"; $name = "tinydb"; $dsn = "mysql:host=$host;port=$port;dbname=$name"; return new PDO($dsn, $user, $pass);}

Connecting to the database

Page 48: Diving into php

function load_content($db) { $sql = "SELECT * FROM tinytable ORDER BY id DESC"; $query = $db->query($sql); $results = $query->fetchAll(); $row = $results[0]; return $row["content"];}

Querying the database

Page 49: Diving into php

<?php

$db = connect_to_database();$content = load_content($db);

function load_content($db) { $sql = "SELECT * FROM tinytable ORDER BY id DESC"; $query = $db->query($sql); $results = $query->fetchAll(); $row = $results[0]; return $row['content'];}

function connect_to_database() { $host = "127.0.0.1"; $port = 8889; $user = "root"; $pass = "root"; $name = "tinydb"; $dsn = "mysql:host=$host;port=$port;dbname=$name"; return new PDO($dsn, $user, $pass);}

?><form action="tiny-wiki.php" method="post"> <input type="text" name="content" value="<?php echo $content; ?>" /> <input type="submit" value="Update" /></form>

tiny-wiki.php

Page 50: Diving into php

Result

Page 51: Diving into php

A simple content management system

1. Build a form for user input

2. Store submissions in a database

3. Retrieve submission data

Page 52: Diving into php

<?php

$db = connect_to_database();$content = load_content($db);

if (!empty($_REQUEST["content"])) { save_content($db, $_REQUEST["content"]); $content = htmlentities($_REQUEST["content"]);}

?>

Core logic

Page 53: Diving into php

function save_content($content) { $sql = "INSERT INTO tinytable (content) VALUES ('$content')"; $db->query($sql);}

Saving the content

Page 54: Diving into php

Save the content

Page 55: Diving into php

A simple content management system

1. Build a form for user input

2. Store submissions in a database

3. Retrieve submission data

Page 56: Diving into php

Wait, this is bad

Page 57: Diving into php

$content = "'); drop table tinytable; --";$sql = "INSERT INTO tinytable (content) VALUES ('$content')";

How does it work?

Page 58: Diving into php

$content = "'); drop table tinytable; --";$sql = "INSERT INTO tinytable (content) VALUES ('$content')";

// Result: (-- is a comment in SQL)// "INSERT INTO tinytable (content)// VALUES (''); drop table tinytable; --')

How does it work?

Page 59: Diving into php

SQL injection

• Another security vulnerability, similar to cross site scripting

• When user data is unintentionally executed as SQL

• Escaping works here also (also, prepared statements)

Page 60: Diving into php

function save_content($db, $content) { $content = $db->quote($content); $sql = "INSERT INTO tinytable (content) VALUES ($content)"; // no more single quotes $db->query($sql, array($content));}

Escape the user input