Except where otherwise noted, the contents of this document are Copyright 2012 Marty Stepp, Jessica Miller, Victoria Kirst and Roy McElmurry IV. All rights reserved. Any redistribution, reproduction, transmission, or storage of part or all of the contents in any form is prohibited without the author's expressed written permission.
<label>Name: <input type="text" /></label> <label><input type="radio" name="cc" /> Visa</label> <label><input type="radio" name="cc" /> MasterCard</label> <br /> Favorite Star Trek captain: <select name="startrek"> <option>James T. Kirk</option> <option>Jean-Luc Picard</option> </select> <br />
[cc] => on, [startrek] => Jean-Luc Picard
value
attribute<label>Name: <input type="text" name="personname" /></label> <label><input type="radio" name="cc" value="visa" /> Visa</label> <label><input type="radio" name="cc" value="mastercard" /> MasterCard</label> <br /> Favorite Star Trek captain: <select name="startrek"> <option value="kirk">James T. Kirk</option> <option value="picard">Jean-Luc Picard</option> </select> <br />
name
attribute sets what the name of the data will be when submittedvalue
attribute sets what will be submitted if a control is selected[personname] => "", [cc] => visa, [startrek] => picard
" "
, "/"
, "="
, "&"
"Webdev's cool!?"
→ "Webdev%27s+cool%3F%21"
$_GET
and $_POST
arrays automatically decode themWe want to transform our grades.html page into a page for submitting our assignments rather than looking up grades.
GET
vs. POST
requests
GET
: asks a server for a page or data
POST
: submits data to a web server and retrieves the server's response
POST
is more appropriate than GET
GET
requests embed their parameters in their URLs<form action="http://foo.com/app.php" method="post"> <div> Name: <input type="text" name="name" /> <br /> Food: <input type="text" name="meal" /> <br /> <label>Meat? <input type="checkbox" name="meat" /></label> <br /> <input type="submit" /> <div> </form>
if ($_SERVER["REQUEST_METHOD"] == "GET") { # process a GET request ... } elseif ($_SERVER["REQUEST_METHOD"] == "POST") { # process a POST request ... }
$_SERVER
array's "REQUEST_METHOD"
elementArray | Description |
---|---|
$_GET ,
$_POST
|
parameters passed to GET and POST requests |
$_FILES
|
files uploaded with the web request |
$_COOKIE ,
$_SESSION
|
"cookies" used to identify the user (seen later) |
$_SERVER ,
$_ENV
|
information about the web server |
$blackbook = array(); $blackbook["marty"] = "206-685-2181"; $blackbook["stuart"] = "206-685-9138"; ... print "Marty's number is " . $blackbook["marty"] . ".\n";
"marty"
maps to value "206-685-2181"
print "Marty's number is {$blackbook['marty']}.\n";
Let's write a PHP file that will receive the action of our turnin form that does the following:
POST
methodvalue
parameters to make this task easier<form action="http://webster.cs.washington.edu/params.php" method="post" enctype="multipart/form-data"> Upload an image as your avatar: <input type="file" name="avatar" /> <input type="submit" /> </form>
input
tag with type
of file
enctype
attribute of the formpost
(an entire file can't be put into a URL!)enctype
(data encoding type) must be set to multipart/form-data
or else the file will not arrive at the server$_FILES
, not $_POST
$_FILES
is itself an associative array, containing:
name
: the local filename that the user uploadedtype
: the MIME type of data that was uploaded, such as image/jpeg
size
: file's size in bytestmp_name
: a filename where PHP has temporarily saved the uploaded file
<input type="file" name="avatar" />
borat.jpg
as a parameter named avatar
,
$_FILES["avatar"]["name"]
will be "borat.jpg"
$_FILES["avatar"]["type"]
will be "image/jpeg"
$_FILES["avatar"]["tmp_name"]
will be something like "/var/tmp/phpZtR4TI"
$username = $_POST["username"]; if (is_uploaded_file($_FILES["avatar"]["tmp_name"])) { move_uploaded_file($_FILES["avatar"]["tmp_name"], "$username/avatar.jpg"); print "Saved uploaded file as $username/avatar.jpg\n"; } else { print "Error: required file not uploaded"; }
is_uploaded_file(filename)
TRUE
if the given filename was uploaded by the user
move_uploaded_file(from, to)
is_uploaded_file
, then do move_uploaded_file
include
include("filename");
include("header.html"); include("shared-code.php");
Validation can be performed:
<form action="http://foo.com/foo.php" method="get"> <div> City: <input name="city" /> <br /> State: <input name="state" size="2" maxlength="2" /> <br /> ZIP: <input name="zip" size="5" maxlength="5" /> <br /> <input type="submit" /> </div> </form>
htmlspecialchars
function
htmlspecialchars
|
returns an HTML-escaped version of a string |
$text = "<p>hi 2 u & me</p>";
$text = htmlspecialchars($text); # "<p>hi 2 u & me</p>"
# construct an object $name = new ClassName(parameters); # access an object's field (if the field is public) $name->fieldName # call an object's method $name->methodName(parameters);
$zip = new ZipArchive(); $zip->open("moviefiles.zip"); $zip->extractTo("images/"); $zip->close();
class_exists
# create an HTTP request to fetch student.php $req = new HttpRequest("student.php", HttpRequest::METH_GET); $params = array("first_name" => $fname, "last_name" => $lname); $req->addPostFields($params); # send request and examine result $req->send(); $http_result_code = $req->getResponseCode(); # 200 means OK print "$http_result_code\n"; print $req->getResponseBody();
HttpRequest
object can fetch a document from the webclass ClassName { # fields - data inside each object public $name; # public field private $name; # private field # constructor - initializes each object's state public function __construct(parameters) { statement(s); } # method - behavior of each object public function name(parameters) { statements; } }
$this
<?php class Point { public $x; public $y; # equivalent of a Java constructor public function __construct($x, $y) { $this->x = $x; $this->y = $y; } public function distance($p) { $dx = $this->x - $p->x; $dy = $this->y - $p->y; return sqrt($dx * $dx + $dy * $dy); } # equivalent of Java's toString method public function __toString() { return "(" . $this->x . ", " . $this->y . ")"; } } ?>
<?php # this code could go into a file named use_point.php include("Point.php"); $p1 = new Point(0, 0); $p2 = new Point(4, 3); print "Distance between $p1 and $p2 is " . $p1->distance($p2) . "\n\n"; var_dump($p2); # var_dump prints detailed state of an object ?>
Distance between (0, 0) and (4, 3) is 5 object(Point)[2] public 'x' => int 4 public 'y' => int 3
$p1
and $p2
are references to Point
objects
class ClassName extends ClassName { ... }
class Point3D extends Point { public $z; public function __construct($x, $y, $z) { parent::__construct($x, $y); $this->z = $z; } ... }
static $name = value; # declaring a static field const $name = value; # declaring a static constant
# declaring a static method
public static function name(parameters) {
statements;
}
ClassName::methodName(parameters); # calling a static method (outside class) self::methodName(parameters); # calling a static method (within class)
interface InterfaceName { public function name(parameters); public function name(parameters); ... } class ClassName implements InterfaceName { ...
abstract class ClassName { abstract public function name(parameters); ... }