Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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.

Abhinav Sharma

February 03, 2010
Tweet

More Decks by Abhinav Sharma

Other Decks in Education

Transcript

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

    <html> <head> <title> Hello </title> </head> <body> World! </body> </html>
  2. You Idiot! Is that all you can do? User Web

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

    </title> </head> <body> <?php $text ?> </body> </html> Computed Results +
  4. 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>
  5. 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>
  6. 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
  7. Step 1: Take Input Step 2: Compute Results Step 3:

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

    Put results in template Step 4: Spit out HTML
  9. 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.
  10. 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>
  11. Syntax Overview Always between <?php and ?> End lines with

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

    ; Case Sensitive Syntax similar to Java Variables Weakly Typed!
  13. 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; ?>
  14. Operators + - * / % == < > <=

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

    $var; // Single Line Comment $var = "Hello"; print $var; ?> C++ / Java Style Comments
  16. 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; ?>
  17. 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; ?>
  18. 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
  19. $_GET Special Array! Params passed in URL $val = $_GET[‘key’]

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

    <?php $n1 = $_GET[‘n1’]; $n2 = $_GET[‘n2’]; $sum = $n1 + $n2; print “sum is $sum”; ?>
  21. $_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; ?>
  22. <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>
  23. 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”; }
  24. For Similar to C++/Java | ‘while’ also available | foreach

    favored for ($i = 0; $i < 10; $i++) { print $i; }
  25. 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”; }
  26. 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); }
  27. 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
  28. 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; }
  29. Classes Logical group of variables and functions “public” & “private”

    supported Use the class keyword -> to access members
  30. 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
  31. 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