Except where otherwise noted, the contents of this document are Copyright 2012 Marty Stepp, Jessica Miller, and Victoria Kirst. 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.
GET filename
: retrieve a file or a program’s output
POST filename
: send form data to a program
PUT filename
: create or overwrite a fileDELETE filename
: delete a fileGET
and POST
requests
$ telnet www.google.com 80
Trying 128.208.3.88...
Connected to 128.208.3.88 (128.208.3.88).
Escape character is '^]'.
GET /search?q=Black+Raven
<!DOCTYPE html> # (the server's response comes after two carriage returns)
<html>
...
$ telnet www.gmail.com 80
Trying 128.208.3.88...
Connected to 128.208.3.88 (128.208.3.88).
Escape character is '^]'.
POST /login.php
Content-type: application/x-www-form-urlencoded
Content-length: 33
username=jessica&password=guinness
<!DOCTYPE html> # (the server's response comes after two carriage returns)
<html>
...
POST
vs. GET
?
GET
requests embed parameters in their URLs
POST
requests embed parameters in the HTTP request body
<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 (parameters are in $_GET) ... } elseif ($_SERVER["REQUEST_METHOD"] == "POST") { // process a POST request (parameters are in $_POST) ... }
$_SERVER
array's "REQUEST_METHOD"
element<form action="" method="post"> ... </form>
action
to be blank (or to the page's own URL)
if ($_SERVER["REQUEST_METHOD"] == "GET") { // normal GET request; display self-submitting form ?> <form action="" method="post">...</form> <?php } elseif ($_SERVER["REQUEST_METHOD"] == "POST") { // POST request; user is submitting form back to here; process it $var1 = $_POST["param1"]; ... }
$_SERVER
array to see which request you're handling<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
$name = array(); $name["key"] = value; ... $name["key"] = value;
$name = array(key => value, ..., key => value);
$blackbook = array("marty" => "206-685-2181", "stuart" => "206-685-9138", "jenny" => "206-867-5309");
print_r($blackbook);
Array ( [jenny] => 206-867-5309 [stuart] => 206-685-9138 [marty] => 206-685-2181 )
if (isset($blackbook["marty"])) { print "Marty's phone number is {$blackbook['marty']}\n"; } else { print "No phone number found for Marty Stepp.\n"; }
name(s) | category |
---|---|
isset , array_key_exists
|
whether the array contains value for given key |
array_keys , array_values
|
an array containing all keys or all values in the assoc.array |
asort , arsort
|
sorts by value, in normal or reverse order |
ksort , krsort
|
sorts by key, in normal or reverse order |
foreach
loop and associative arraysforeach ($blackbook as $key => $value) { print "$key's phone number is $value\n"; }
jenny's phone number is 206-867-5309 stuart's phone number is 206-685-9138 marty's phone number is 206-685-2181
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>
$city = $_POST["city"]; $state = $_POST["state"]; $zip = $_POST["zip"]; if (!$city || strlen($state) != 2 || strlen($zip) != 5) { print "Error, invalid city/state/zip submitted."; }
print
ing an error message is not a very graceful result
die
functiondie("error message text");
die
function prints a message and then completely stops code execution
header
functionheader("HTTP header text"); // in general header("Location: url"); // for browser redirection
header
function can be used for several common HTTP messages
Location
header to tell the browser to redirect itself to another page
header
to redirect between pagesheader("Location: url");
$city = $_POST["city"];
$state = $_POST["state"];
$zip = $_POST["zip"];
if (!$city || strlen($state) != 2 || strlen($zip) != 5) {
header("Location: start-page.php"); // invalid input; redirect
}
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>"