HTTP Basic
Keywords: http, html, header, form, request, response, get, post, cookie,
session, authentication, domain, timeout, expire, redirect, caching, file
upload, apache, rewrite rule, htaccess.
Subjects:
7.1.
HTTP Fundamentals
7.2.
File Upload
7.3.
Cookie
7.4.
Session
THÀNH VIÊN HIỆP HỘI AN TOÀN THÔNG TIN VIỆT NAM- VNISA WWW.ATHENA.EDU.VN . TEL: 1900 54 54 56 -090 7879 477
VO DUY TUAN PHP Beginner & Intermediate
ATHENA INFORMATION TRAINING
86
7.1.
HTTP Fundamentals:
7.1.1. How HTTP works: (RFC 2616)
7.1.2. GET Method:
- Change URL after submit form
- using global variable $_GET or $_REQUEST to access data in form.
Ex:
$firstName = $_GET['FirstName']; $salary = $_GET['Salary'];
$contractor = $_GET['Contractor'];
7.1.3. POST Method:
- Not change URL after submit form
THÀNH VIÊN HIỆP HỘI AN TOÀN THÔNG TIN VIỆT NAM- VNISA WWW.ATHENA.EDU.VN . TEL: 1900 54 54 56 -090 7879 477
VO DUY TUAN PHP Beginner & Intermediate
ATHENA INFORMATION TRAINING
87
Ex:
$firstName = $_POST['FirstName']; $salary = $_POST['Salary']; $contractor = $_POST['Contractor'];7.1.4. HTTP Request:
- Sample of request:
---
GET Header Request:
GET /dumprequest HTTP/1.1 Host: djce.org.uk
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.12) Gecko/2009070611 Firefox/3.0.12 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: en-us,en;q=0.5 Accept-Encoding: gzip,deflate Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Keep-Alive: 300 Connection: keep-alive Referer: http://www.google.com.vn/search?hl=vi&client=firefox-a&rls=org.mozilla:en- US:official&um=1&q=http%20request&ndsp=21&ie=UTF-8&sa=N&tab=iw
THÀNH VIÊN HIỆP HỘI AN TOÀN THÔNG TIN VIỆT NAM- VNISA WWW.ATHENA.EDU.VN . TEL: 1900 54 54 56 -090 7879 477
VO DUY TUAN PHP Beginner & Intermediate
ATHENA INFORMATION TRAINING
88
POST /path/script.cgi HTTP/1.0 From: [email protected] User-Agent: HTTPTool/1.0 Content-Type: application/x-www-form-urlencoded Content-Length: 32 home=Mosby&favorite+flavor=flies
7.1.5. HTTP Response:
HTTP/1.1 200 OKDate: Sun, 08 Oct 2000 18:46:12 GMT Server: Apache/1.3.6 (Unix)
Keep-Alive: timeout=5, max=120 Connection: Keep-Alive
Content-Type: text/html <html>...
Some HTTP 1.1 status code:
Status
Code
Reason
Phrase
Description
200
OK
Generic successful request message response. This is the code
sent most often when a request is filled normally.
201
Created
The request was successful and resulted in a resource being
created. This would be a typical response to a PUT method.
301
Moved
Permanently
The resource requested has been moved to a new URL
permanently. Any future requests for this resource should use
the new URL.
THÀNH VIÊN HIỆP HỘI AN TOÀN THÔNG TIN VIỆT NAM- VNISA WWW.ATHENA.EDU.VN . TEL: 1900 54 54 56 -090 7879 477
VO DUY TUAN PHP Beginner & Intermediate
ATHENA INFORMATION TRAINING
89
This is the proper method of handling situations where a file on
a server is renamed or moved to a new directory. Most people
don't bother setting this up, which is why URLs “break” so
often, resulting in 404 errors as discussed below.
302
Found
The resource requested is temporarily using a different URL.
The client should continue to use the original URL.
304
Not Modified
The client sent a conditional GET request, but the resource has
not been modified since the specified date/time, so the server
has not sent it.
400
Bad request
Server says, “huh?” Generic response when the request cannot
be understood or carried out due to a problem on the client's
end.
401
Unauthorized
The client is not authorized to access the resource. Often
returned if an attempt is made to access a resource protected by
a password or some other means without the appropriate
credentials.
404
Not Found
The most common HTTP error message, returned when the
server cannot locate the requested resource. Usually occurs due
to either the server having moved/removed the resource, or the
client giving an invalid URL (misspellings being the most
common cause.)
500
Internal
Server Error
Generic error message indicating that the request could not be
fulfilled due to a server problem.
502
Bad Gateway
The server, while acting as a gateway or proxy, received an
invalid response from another server it tried to access on the
client's behalf.
503
Service
Unavailable
The server is temporarily unable to fulfill the request for internal
reasons. This is often returned when a server is overloaded or
down for maintenance.
504
Gateway
Timeout
The server, while acting as a gateway or proxy, timed out while
waiting for a response from another server it tried to access on
the client's behalf.
7.1.6. Header:
- Using function header() to send a HTTP header.
Ex:
THÀNH VIÊN HIỆP HỘI AN TOÀN THÔNG TIN VIỆT NAM- VNISA WWW.ATHENA.EDU.VN . TEL: 1900 54 54 56 -090 7879 477
VO DUY TUAN PHP Beginner & Intermediate
ATHENA INFORMATION TRAINING
90
header('Location: http://www.php.net/'); header("HTTP/1.0 404 Not Found");
7.2.
File Upload:
7.2.1. Prepare form:
<form method="post" enctype="multipart/form-data" action="upload.php"> File to upload: <input type="file" name="myfile"><BR>
<input type="submit" value="Submit"> </FORM>
7.2.2. Upload a file:
- Get uploaded file's data using $_FILES. Ex:
$name = $_FILES[myfile]['name']; $type = $_FILES[myfile][type]; $size = $_FILES[myfile][size]; $tmpname = $_FILES[myfile][tmp_name]; $error = $_FILES[myfile][error]; $uploadPath = 'uploads/photos/'.$name;
// Begin to upload fileif (move_uploaded_file($tmpname, $uploadPath)) {
echo 'Upload successfully!'; }
7.2.3. Upload multiple files:
- HTML form:
…
File to upload: <input type="file" name="myfile[]"><BR> File to upload: <input type="file" name="myfile[]"><BR> …
- PHP script:
$nameA = $_FILES[myfile]['name'][0]; $tmpnameA = $_FILES[myfile][tmp_name][0];
THÀNH VIÊN HIỆP HỘI AN TOÀN THÔNG TIN VIỆT NAM- VNISA WWW.ATHENA.EDU.VN . TEL: 1900 54 54 56 -090 7879 477
VO DUY TUAN PHP Beginner & Intermediate
ATHENA INFORMATION TRAINING
91
$uploadPathA = 'uploads/photos/'.$nameA; $nameB = $_FILES[myfile]['name'][1]; $tmpnameA = $_FILES[myfile][tmp_name][1]; $uploadPathB = 'uploads/photos/'.$nameB; // Begin to upload files
…
7.3.
Cookie:
7.3.1. How cookies work:
THÀNH VIÊN HIỆP HỘI AN TOÀN THÔNG TIN VIỆT NAM- VNISA WWW.ATHENA.EDU.VN . TEL: 1900 54 54 56 -090 7879 477
VO DUY TUAN PHP Beginner & Intermediate
ATHENA INFORMATION TRAINING
92
$value = 'something from somewhere'; setcookie("test", $value);
setcookie("test", $value, time()+3600); /* expire in 1 hour */
setcookie("test", $value, time()+3600, "/~test/", ".example.com", 1);
7.3.3. Get cookies data: using global variables $_COOKIES or $_REQUEST.
Ex:
echo $_COOKIE["test"];
7.3.4. Delete a cookie:
setcookie ("test", "", time() - 3600);
7.4.
Session:
THÀNH VIÊN HIỆP HỘI AN TOÀN THÔNG TIN VIỆT NAM- VNISA WWW.ATHENA.EDU.VN . TEL: 1900 54 54 56 -090 7879 477
VO DUY TUAN PHP Beginner & Intermediate
ATHENA INFORMATION TRAINING
93
7.4.2. Create sessions: If you are using cookie-based sessions, you must call
session_start() before anything is outputted to the browser.
session_start();
$_SESSION['username'] = 'testing';
7.4.3. Session ID processing:
7.4.3.1. Get the current Session ID:
- session_id() : used to get or set the session id for the current session. Ex:
$currentSessID = session_id();
7.4.3.2. Re-generate Session ID:
- session_regenerate_id() : Update the current session id with a newly generated one.
7.4.3.3. Example:
session_start();
$old_sessionid = session_id(); session_regenerate_id();
$new_sessionid = session_id();
echo "Old Session: $old_sessionid<br />"; echo "New Session: $new_sessionid<br />";
7.4.4. Delete a session:
session_destroy();
Chapter Exercise:
A> Design a webpage to display form for user login. The username is an email, and the password
must be at least 6 characters. Password must be alphanumerical characters (a-z, A-Z, 0-9). If login success, displays the name of the user (extract from email) and the number of page which is refreshed by user.
Ex: enter email: [email protected], password: abc3456ad5, website will display:
THÀNH VIÊN HIỆP HỘI AN TOÀN THÔNG TIN VIỆT NAM- VNISA WWW.ATHENA.EDU.VN . TEL: 1900 54 54 56 -090 7879 477
VO DUY TUAN PHP Beginner & Intermediate
ATHENA INFORMATION TRAINING
94
You refreshed 3 times.
B> Write photo album function for the user (after login) from question A.
a. Upload images (png,gif,jpeg) – 5 images in a submit form
b. Store images in directory "uploads/images/".
c. Max file size: 300KB
d. File name: USERNAME_imagename
e. If image existed, append the letter "i" after name part. Ex: rasmus_image1.gif exists, will be renamed to rasmus_image1i.gif
THÀNH VIÊN HIỆP HỘI AN TOÀN THÔNG TIN VIỆT NAM- VNISA WWW.ATHENA.EDU.VN . TEL: 1900 54 54 56 -090 7879 477
VO DUY TUAN PHP Beginner & Intermediate
ATHENA INFORMATION TRAINING