How the Web Works: Lecture 3

How the Web Works: Lecture 3

This talk was designed for a class (98-135) taught at Carnegie Mellon University in Spring 2010.

1b26895a0be18b58a2ec0447115a63aa?s=128

Abhinav Sharma

February 03, 2010
Tweet

Transcript

  1. None
  2. None
  3. $1 Million for building a movie recommendation engine.

  4. None
  5. None
  6. Lecture 3 The Web and PHP

  7. None
  8. User Web Server

  9. GET helloworld.com User Web Server

  10. GET helloworld.com User Web Server <html> <head> <title> Hello </title>

    </head> <body> World! </body> </html>
  11. User Web Server

  12. POST helloworld.com cmd: make me a sandwich User Web Server

  13. POST helloworld.com cmd: make me a sandwich User Web Server

    <html> <head> <title> Hello </title> </head> <body> World! </body> </html>
  14. User Web Server

  15. You Idiot! Is that all you can do? User Web

    Server
  16. You Idiot! Is that all you can do? User Web

    Server
  17. You Idiot! Is that all you can do? User Web

    Server <html> <head> <title> Make Your Own </title> </head> <body> @#$%#^%$&^&$ </body> </html>
  18. Static Pages + Placeholders <html> <head> <title> <?php $title ?>

    </title> </head> <body> <?php $text ?> </body> </html> Computed Results +
  19. <html> <head> <title> Hello </title> </head> <body> World </body> </html>

  20. GET index.html <html> <head> <title> Hello </title> </head> <body> World

    </body> </html>
  21. GET index.html Return HTML <html> <head> <title> Hello </title> </head>

    <body> World </body> </html>
  22. GET index.html Return HTML <html> <head> <title> Hello </title> </head>

    <body> World </body> </html> STATIC
  23. <html> <head> <title> <?php $title ?> </title> </head> <body> <?php

    $text ?> </body> </html>
  24. GET index.php <html> <head> <title> <?php $title ?> </title> </head>

    <body> <?php $text ?> </body> </html>
  25. GET index.php <html> <head> <title> <?php $title ?> </title> </head>

    <body> <?php $text ?> </body> </html> Interpreter <html> <head> <title> Hello </title> </head> <body> World </body> </html>
  26. GET index.php <html> <head> <title> <?php $title ?> </title> </head>

    <body> <?php $text ?> </body> </html> Return HTML Interpreter <html> <head> <title> Hello </title> </head> <body> World </body> </html>
  27. GET index.php <html> <head> <title> <?php $title ?> </title> </head>

    <body> <?php $text ?> </body> </html> Return HTML Interpreter <html> <head> <title> Hello </title> </head> <body> World </body> </html> Dynamic
  28. None
  29. Can I see the PHP?

  30. Can I see the PHP?

  31. How Is that Useful?

  32. How Is that Useful?

  33. None
  34. Step 1: Take Input

  35. Step 1: Take Input Step 2: Compute Results

  36. Step 1: Take Input Step 2: Compute Results Step 3:

    Put results in template
  37. Step 1: Take Input Step 2: Compute Results Step 3:

    Put results in template Step 4: Spit out HTML
  38. Step 1: Take Input Step 2: Compute Results Step 3:

    Put results in template Step 4: Spit out HTML
  39. I can do that too!

  40. Why PHP? ... so can Perl, Python, Java, Ruby, or

    even C PHP is easy to learn Syntax similar to Java Most popular Lots of (open) code out there - ex. Wordpress.
  41. Hello, World! <html> <head> <title>My Page</title> </head> <body> <?php print

    "Hello, World"; ?> </body> </html>
  42. Hello, World! <html> <head> <title>My Page</title> </head> <body> <?php print

    "Hello, World"; ?> </body> </html> = <html> <head> <title>My Page</title> </head> <body> Hello, World </body> </html>
  43. Ask Now or everything else will seem so pointless you’ll

    want to shoot me with a nerf gun.
  44. None
  45. Syntax Party!

  46. Syntax Overview Always between <?php and ?> End lines with

    ; Case Sensitive Syntax similar to Java Variables Weakly Typed!
  47. Syntax Overview Always between <?php and ?> End lines with

    ; Case Sensitive Syntax similar to Java Variables Weakly Typed!
  48. Variables Dont need to declare Prefixed with $ No explicit

    type! Assign with = operator No integer / floats Boolean true or false <?php $var = 23; print $var; $var = "Hello"; print $var; $c = 2; print $c/2; $b = true; ?>
  49. Operators + - * / % == < > <=

    >= String Concatenation (.) && || ! XOR <?php $a = 23; $b = 15; print $a + $b; $var = "Hello" . “World”; $c = true; $d = false; $a = $c || $d; ?>
  50. Comments <?php /* Multiline Comment */ $var = 23; print

    $var; // Single Line Comment $var = "Hello"; print $var; ?> C++ / Java Style Comments
  51. String Quoting Strings can be “string” or ‘string’, char not

    special. Single Quotes ‘ ‘ - printed literally Double quotes “ “ - can embed values <?php $lang = “PHP”; $string1 = “I like $lang”; $string2 = ‘I like $lang’; print $string1; print $string2; ?>
  52. String Quoting Strings can be “string” or ‘string’, char not

    special. Single Quotes ‘ ‘ - printed literally Double quotes “ “ - can embed values $string1: I like PHP $string2: I like $lang <?php $lang = “PHP”; $string1 = “I like $lang”; $string2 = ‘I like $lang’; print $string1; print $string2; ?>
  53. Arrays Created with array() ... actually Hash Tables Indices dont

    have to be numbers! Appending with $arr[] Similar to PERL <?php $arr = array(); $arr[32] = 23; // following appends to [33] $arr[] = 24 $arr = array(“foo”=>1, “bar”=>2); print $array[“foo”]; print $array[“bar”]; ?> http://php.net/manual/en/language.types.array.php
  54. Arrays ... are a (potentially) many-one mapping between any types

    foo bar baz A B C
  55. Arrays ... are a (potentially) many-one mapping between any types

    foo bar baz gorp A B C
  56. $_GET Special Array! Params passed in URL $val = $_GET[‘key’]

    <?php $n1 = $_GET[‘n1’]; $n2 = $_GET[‘n2’]; $sum = $n1 + $n2; print “sum is $sum”; ?>
  57. $_GET Special Array! Params passed in URL $val = $_GET[‘key’]

    <?php $n1 = $_GET[‘n1’]; $n2 = $_GET[‘n2’]; $sum = $n1 + $n2; print “sum is $sum”; ?>
  58. $_POST Similar to $_GET POST parameters $val = $_POST[‘key’] Processing

    Forms <?php $a = 23; $b = 15; print $a + $b; $var = "Hello" . “World”; $c = true; $d = false; $a = $c || $d; ?>
  59. <form method="POST" action="target.php"> <input type="text" name="login" /><br /> <input type="password"

    name="pass" /><br /> <input type="submit" value=”Submit Query”> </form>
  60. Demo 1 PHP Form Processing

  61. Control Flow, Functions

  62. If Similar to C++/Java Things that are false: false “”

    0 NULL Everything Else True if ($color == “red”) { print “red”; } else if ($color = “blue”) { print “blue”; } else { print “no color”; }
  63. For Similar to C++/Java | ‘while’ also available | foreach

    favored for ($i = 0; $i < 10; $i++) { print $i; }
  64. Foreach Iterates as key, value pair or just as values

    Similar to Java’s for(int a : arr) $languages = array( ‘best’ => ‘php’, ‘worst’ => ‘brainf*ck’, ‘easy’ => ‘php’ ); // Iterate as key-value pair foreach ($languages as $adj => $lang) { print “The $adj language is $lang”; } // Iterate over values foreach ($languages as $lang) { print “One language is $lang”; }
  65. Functions Keyword function No return types Take/not take arguments Overloading

    function count($i, $max = 10) { while ( $i< $max) { print $i; } } function factorial($n) { if ($n == 0) return 0; return $n * factorial($n-1); }
  66. Demo 2 Some Functions and Iteration

  67. Cookies & Sessions

  68. Cookies

  69. GET index.php Cookies

  70. GET index.php Return Cookie + HTML Cookies

  71. GET index.php Return Cookie + HTML Cookies

  72. GET index.php Return Cookie + HTML Cookies

  73. Cookies Website can store info on user’s computer. ... in

    small files called “Cookies” Set with setcookie() Access with $_COOKIE Happens in HTTP header, before any HTML setcookie ($name, $value, $expire) $new_value = $_COOKIE[$name]; http://php.net/manual/en/function.setcookie.php
  74. None
  75. Already Logged In

  76. Already Logged In Submitted Login Form

  77. Already Logged In Submitted Login Form Not Logged In, Show

    Login Form
  78. Sessions Also store user info. ... on server Across multiple

    pages $_SESSION[‘key’] = ‘val’ Login / Shopping Carts Data can’t be tampered session_start(); if (isset($_SESSION[‘user’])) { print “Welcome Back!”; } else { // Get $name from form $_SESSION[‘user’] = $name; }
  79. None
  80. Already Logged In

  81. Already Logged In Submitted Login Form

  82. Already Logged In Submitted Login Form Not Logged In, Show

    Login Form
  83. Example 3 Login/Logout

  84. Object Oriented

  85. Classes Logical group of variables and functions “public” & “private”

    supported Use the class keyword -> to access members
  86. None
  87. Inheritance

  88. Using Classes

  89. Useful Functions print_r($arr) : Prints arrays, useful in debugging require_once(‘lib.php’)

    : Imports other PHP files header($data) : Sets HTTP headers (Google for more) isset($var) : Checks is variable has a value explode($delimiter, $string) : Splits string to array mail($to, $subject, $message) : Sends email! htmlspecialchars($string) : Preventing XSS Attacks
  90. Resources CCCPHP http://sipb-iap.scripts.mit.edu/2009/cccphp/CCCPHP.pdf PHP.net Google “php ___”

  91. Week 3 Week 4

  92. Announcement Homework 1 isn’t out yet Involves HTML, PHP, MySQL

    I haven’t coded it yet =P Minis are now Extra Credit 2 Minis = 1 Homework Minis can cover for Homeworks
  93. Photo Credits http://upload.wikimedia.org/wikipedia/zh/e/ea/PHP-logo.png http://benewmark.com/lafayette/homework_title.gif http://www.google.com/intl/en_ALL/images/logo.gif http://blog.crowdspring.com/wp-content/uploads/2009/04/netflix-prize.png http://www.mccullagh.org/db9/1ds-16/world-space-party-2007.jpg http://blog.motane.lu/wp-content/uploads/2009/09/mysql-logo.png http://www.coincommunity.com/forum/uploaded/Jaobler/2008516_50DolStarNote.jpg

  94. None