Attribution: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.
Otherwise noted: Claude Anderson was given permission to modify the slides for CSSE 290 at Rose-Hulman by author Jessica Miller.
The authors' original slides, based on Web Programming Step by Step, can be seen at http://webstepbook.com.
Some of the examples in some days' slides are from David Fisher at Rose-Hulman, who was kind enough to allow me to use them.
My intention is to mark these examples with [DSF].
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" elementhtmlspecialchars 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>"
| Array | Description |
|---|---|
$_REQUEST
|
parameters passed to any type of request |
$_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"] = "444-444-4444"; $blackbook["stuart"] = "444-444-5555"; ... print "Marty's number is " . $blackbook["marty"] . ".\n";
"marty" maps to value "444-444-4444"print "Marty's number is {$blackbook['marty']}.\n";
<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 filepost (an entire file can't be put into a URL!)enctype attribute of the formenctype (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/jpegsize : file's size in bytestmp_name : a filename where PHP has temporarily saved the uploaded file
<input type="file" name="avatar" />
family.jpg as a parameter named avatar,
$_FILES["avatar"]["name"] will be "family.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_fileinclude
include("filename");
include("header.html");
include("shared-code.php");
$name = array(); $name["key"] = value; ... $name["key"] = value;
$name = array(key => value, ..., key => value);
$date_hired = array("Cary" => "1981",
"Lynn" => "1987",
"Claude" => "1988",
"David" => "1994");
if (isset($date_hired["Lynn"])) {
print "Lynns's hire date is {$date_hired['Lynn']}\n";
} else {
print "No hire date found for Lynn\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 |
Note: isset()does not return TRUE for an array key whose corresponding value is NULL; array_key_exists() does.
foreach Loop and Associative Arrays
foreach ($hire_date as $name => $year) {
print "$name was hired in $year\n";
}
Cary was hired in 1981 Lynn was hired in 1987 Claude was hired in 1988 David was hired in 1994
Edit; otherwise, click New
Name field can be different)
loop.php from loop.zip
and place it in a new or existing PHP project in Aptana.
Continued on next slide
Name field can be different)
C:\xampp\php\php.ini, find the xdebug section, and add a line:xdebug.remote_port=9000
Next we will set a breakpoint, Debug as Script, and Debug as Server
Initial state:
After inserting message with picture: