Kruli š 11122013 by Martin Kruliš v10 1 Request Information Decoded to the SERVER array REQUESTMETHOD used method GET or POST SERVERPROTOCOL ID: 799713
Download The PPT/PDF document "PHP – Digging Deeper Martin" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
PHP – Digging Deeper
Martin Kruliš
11.12.2013
by Martin Kruliš (v1.0)
1
Slide2Request Information
Decoded to the $_SERVER array
REQUEST_METHOD – used method (“
GET”or “
POST
”)SERVER_PROTOCOL – protocol version (“HTTP/1.1”)REQUEST_URI – request part of URL (“/index.php”)REMOTE_ADDR – clients IP addressHTTP_ACCEPT – MIME types that the client acceptsHTTP_ACCEPT_LANGUAGE – desired translationHTTP_ACCEPT_ENCODING – desired encodingsHTTP_ACCEPT_CHARSET – desired charsets+ more info about the server and the client’s browser
11.12.2013
by Martin Kruliš (v1.0)
2
HTTP Issues
phpinfo
()
Slide3File Uploads
In form as <input type="file"
name=... />Provide safe way to browse disk files
HTTP wrapper handles the fileStores it in temporary locationProvide related info in
$_FILES[
name]'tmp_name' – path to the file in temp directory'error' – error code (e.g., UPLOAD_ERR_OK)'name', 'type', 'size', …File exists only as long as the script runsis_uploaded_file() – verificationmove_uploaded_file() – a safe way to move files
11.12.2013
by Martin Kruliš (v1.0)
3
HTTP Issues
Example 1
Slide4Redirect Mechanism in HTTP
3xx response code301 Moved Permanently302 Found (originally named Moved Temporarily)303 See OtherAdditional header '
Location' has the new URLBrowser must try to load the new URL
Loops in redirections are detectedCreating Redirect in PHP
header("Location:
my-new-url");Automatically changes the response code (to 302)11.12.2013by Martin Kruliš (v1.0)4HTTP Issues
Slide5Problem with POST
11.12.2013
by Martin Kruliš (v1.0)
5
HTTP Issues
Client(Browser)Web Server
POST Request
(a submitted form)
Response
(a HTML page)
script
a
dd/change
something
Refresh
Again!!!
Slide6Redirect (303 See Other) after POST
11.12.2013
by Martin Kruliš (v1.0)
6
HTTP Issues
Client(Browser)Web Server
POST Request
Redirect (new URL)
a
dd/change
something
Refresh
GET (new URL)
HTML Page
read-only
Redirects to a new URL
(without updating history)
Example
2
Slide7Cookies
A way to deal with stateless nature of HTTPKey-value pairs (of strings) stored in web browser
Set by special HTTP response headerAutomatically re-sent in headers with every requestEach page (domain) has it own set of cookies
Cookies in PHPCookies sent by browser are loaded to
$_COOKIE[]
Cookies are set/modified/removed by setcookie()The function modifies HTTP response headers11.12.2013by Martin Kruliš (v1.0)7HTTP IssuesExample 3
Slide8Functions
PHP have a huge arsenal of string functionsstrlen
(),
substr()
,
trim(), split(), join(), …Libs for charset manipulationMultibyte string libIconv libRecodeFunctions for encoding (to URL, HTML, SQL, …)urlencode(), urldecode()htmlspecialchars(), htmlspecialchars_decode
()
mysqli_real_escape_string()
11.12.2013
by Martin Kruliš (v1.0)
8
Strings
Slide9Regular Expressions
String search patterns based on regular automataUsed for pattern matching, replacement, splitting, …POSIX syntaxSame syntax as in
unix tools (grep
, sed
, …)
Deprecates as of PHP 5.3Perl (PCRE) syntaxSimilar to POSIX syntax, but with more featuresSeparate set of functions in PHPRegular expression evaluation is implemented in CFaster than implementing string parsing in PHP11.12.2013by Martin Kruliš (v1.0)9Strings
Slide10MySQL
Original mysql API is deprecated (as of PHP 5.5)MySQL Improved (mysqli
) APIDual object/procedural interfaceProcedural interface is similar to original (deprecated) APIAdvanced connectivity features
Persistent connections, compression, encryptionDirectly supports transactions
MySQL Native Driver (
mysqlnd) extensionMore direct access to MySQL serverAdditional features (e.g., asynchronous queries)11.12.2013by Martin Kruliš (v1.0)10Databases
Slide11MySQLi Procedural API
Establishing connection with MySQL server$
mysqli
= mysqli_connect
("
server", "login", "password", "db_name
");
Performing queries
$res = mysqli_query
($
mysqli
,
"
SQL …
");
Terminating connection
mysqli_close
($
mysqli
);
MySQL
statement wrapper functions
mysqli_stmt_init
($
mysqli
);
mysqli_stmt
_*(…)
11.12.2013
by Martin Kruliš (v1.0)
11
Databases
Slide12MySQL Results
mysqli_query
() result depends on the query typeOn failure always returns
falseModification queries return
true
on successData queries (SELECT, …) return mysqli_result objmysqli_fetch_assoc($res)mysqli_fetch_obj($res)mysqli_fetch_all($res, $format)mysqli_fetch_fields($res)mysqli_num_rows($res)
mysqli_free_result($res)
11.12.2013
by Martin Kruliš (v1.0)
12
Databases
Example 5
Slide13Zend
FrameworkDeveloped by open community, supported by ZendLarge and robust, based on MVC architecture
Build as independent modules (database, sessions, …)Nette
Popular PHP framework with Czech communitySimple, easy to learn and useModern approach (OO design, MVC, supports AJAX)
Dibi
Database abstraction layer for PHP11.12.2013by Martin Kruliš (v1.0)13Frameworks
Slide1411.12.2013
by Martin Kruliš (v1.0)
14
Discussion