CSC309 Winter 2016 Lecture 4 - Department of Computer ...ylzhang/csc309w16/files/lec04-php.pdf ·...
Transcript of CSC309 Winter 2016 Lecture 4 - Department of Computer ...ylzhang/csc309w16/files/lec04-php.pdf ·...
![Page 1: CSC309 Winter 2016 Lecture 4 - Department of Computer ...ylzhang/csc309w16/files/lec04-php.pdf · • PHP7.0 released on December 3, 2015 7. Lifecycle a PHP request 8. Hello World!](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97a9a900fb017c432ae26/html5/thumbnails/1.jpg)
CSC309 Winter 2016 Lecture 4Larry Zhang
1
![Page 2: CSC309 Winter 2016 Lecture 4 - Department of Computer ...ylzhang/csc309w16/files/lec04-php.pdf · • PHP7.0 released on December 3, 2015 7. Lifecycle a PHP request 8. Hello World!](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97a9a900fb017c432ae26/html5/thumbnails/2.jpg)
Today’s topic• Server-side programming
• PHP
• We will go through some basic concepts then we will do demos.
2
![Page 3: CSC309 Winter 2016 Lecture 4 - Department of Computer ...ylzhang/csc309w16/files/lec04-php.pdf · • PHP7.0 released on December 3, 2015 7. Lifecycle a PHP request 8. Hello World!](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97a9a900fb017c432ae26/html5/thumbnails/3.jpg)
What is server-side programming• Normally, the client requests an HTML
document (e.g., hello.html) on the server; the server simply gets the document and sends it back to the client as response.
• But if the client requests a script file (e.g., hello.php), the server would run that script, do some computations, generate an HTTP response and send it back to the server.
• The script is run at the server, not at the client.
3
![Page 4: CSC309 Winter 2016 Lecture 4 - Department of Computer ...ylzhang/csc309w16/files/lec04-php.pdf · • PHP7.0 released on December 3, 2015 7. Lifecycle a PHP request 8. Hello World!](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97a9a900fb017c432ae26/html5/thumbnails/4.jpg)
Why server-side programming?• Create dynamic web page (the content of the web page is different
for different computation result from the script)
• Process user input (user data submitted using HTML forms can be used by the script run by the server)
• Interact with database on the server.
• Provides security: The content of the script cannot be seen in the browser.
4
![Page 5: CSC309 Winter 2016 Lecture 4 - Department of Computer ...ylzhang/csc309w16/files/lec04-php.pdf · • PHP7.0 released on December 3, 2015 7. Lifecycle a PHP request 8. Hello World!](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97a9a900fb017c432ae26/html5/thumbnails/5.jpg)
Server-side script can be any language • PHP, Python, Ruby, Javascript, ASP.NET, Java, C/C++, Haskell, Go,
Perl, …
• We will learn PHP first, since it is the easiest to get started with
• It’s free and open source
• Used by Facebook, Yahoo!, Wikipedia, Wordpress, Tumblr, …
5
![Page 6: CSC309 Winter 2016 Lecture 4 - Department of Computer ...ylzhang/csc309w16/files/lec04-php.pdf · • PHP7.0 released on December 3, 2015 7. Lifecycle a PHP request 8. Hello World!](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97a9a900fb017c432ae26/html5/thumbnails/6.jpg)
Market share
6
![Page 7: CSC309 Winter 2016 Lecture 4 - Department of Computer ...ylzhang/csc309w16/files/lec04-php.pdf · • PHP7.0 released on December 3, 2015 7. Lifecycle a PHP request 8. Hello World!](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97a9a900fb017c432ae26/html5/thumbnails/7.jpg)
PHP• stands for “PHP Hypertext Preprocessor”
• We are using PHP 5.3 on cs.utm, released on June, 2009
• PHP6 was started but was never released
• PHP7.0 released on December 3, 2015
7
![Page 8: CSC309 Winter 2016 Lecture 4 - Department of Computer ...ylzhang/csc309w16/files/lec04-php.pdf · • PHP7.0 released on December 3, 2015 7. Lifecycle a PHP request 8. Hello World!](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97a9a900fb017c432ae26/html5/thumbnails/8.jpg)
Lifecycle a PHP request
8
![Page 9: CSC309 Winter 2016 Lecture 4 - Department of Computer ...ylzhang/csc309w16/files/lec04-php.pdf · • PHP7.0 released on December 3, 2015 7. Lifecycle a PHP request 8. Hello World!](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97a9a900fb017c432ae26/html5/thumbnails/9.jpg)
Hello World!
9
![Page 10: CSC309 Winter 2016 Lecture 4 - Department of Computer ...ylzhang/csc309w16/files/lec04-php.pdf · • PHP7.0 released on December 3, 2015 7. Lifecycle a PHP request 8. Hello World!](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97a9a900fb017c432ae26/html5/thumbnails/10.jpg)
PHP Basic Syntax
10
![Page 11: CSC309 Winter 2016 Lecture 4 - Department of Computer ...ylzhang/csc309w16/files/lec04-php.pdf · • PHP7.0 released on December 3, 2015 7. Lifecycle a PHP request 8. Hello World!](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97a9a900fb017c432ae26/html5/thumbnails/11.jpg)
PHP Syntax Template (.php file)
11
![Page 12: CSC309 Winter 2016 Lecture 4 - Department of Computer ...ylzhang/csc309w16/files/lec04-php.pdf · • PHP7.0 released on December 3, 2015 7. Lifecycle a PHP request 8. Hello World!](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97a9a900fb017c432ae26/html5/thumbnails/12.jpg)
Variables
12
![Page 13: CSC309 Winter 2016 Lecture 4 - Department of Computer ...ylzhang/csc309w16/files/lec04-php.pdf · • PHP7.0 released on December 3, 2015 7. Lifecycle a PHP request 8. Hello World!](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97a9a900fb017c432ae26/html5/thumbnails/13.jpg)
Variable: types
13
![Page 14: CSC309 Winter 2016 Lecture 4 - Department of Computer ...ylzhang/csc309w16/files/lec04-php.pdf · • PHP7.0 released on December 3, 2015 7. Lifecycle a PHP request 8. Hello World!](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97a9a900fb017c432ae26/html5/thumbnails/14.jpg)
bool
14
![Page 15: CSC309 Winter 2016 Lecture 4 - Department of Computer ...ylzhang/csc309w16/files/lec04-php.pdf · • PHP7.0 released on December 3, 2015 7. Lifecycle a PHP request 8. Hello World!](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97a9a900fb017c432ae26/html5/thumbnails/15.jpg)
Arithmetic operators
15
![Page 16: CSC309 Winter 2016 Lecture 4 - Department of Computer ...ylzhang/csc309w16/files/lec04-php.pdf · • PHP7.0 released on December 3, 2015 7. Lifecycle a PHP request 8. Hello World!](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97a9a900fb017c432ae26/html5/thumbnails/16.jpg)
Math operations
16
![Page 17: CSC309 Winter 2016 Lecture 4 - Department of Computer ...ylzhang/csc309w16/files/lec04-php.pdf · • PHP7.0 released on December 3, 2015 7. Lifecycle a PHP request 8. Hello World!](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97a9a900fb017c432ae26/html5/thumbnails/17.jpg)
int and float
17
![Page 18: CSC309 Winter 2016 Lecture 4 - Department of Computer ...ylzhang/csc309w16/files/lec04-php.pdf · • PHP7.0 released on December 3, 2015 7. Lifecycle a PHP request 8. Hello World!](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97a9a900fb017c432ae26/html5/thumbnails/18.jpg)
Strings
18
![Page 19: CSC309 Winter 2016 Lecture 4 - Department of Computer ...ylzhang/csc309w16/files/lec04-php.pdf · • PHP7.0 released on December 3, 2015 7. Lifecycle a PHP request 8. Hello World!](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97a9a900fb017c432ae26/html5/thumbnails/19.jpg)
String
19
![Page 20: CSC309 Winter 2016 Lecture 4 - Department of Computer ...ylzhang/csc309w16/files/lec04-php.pdf · • PHP7.0 released on December 3, 2015 7. Lifecycle a PHP request 8. Hello World!](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97a9a900fb017c432ae26/html5/thumbnails/20.jpg)
String functions
20
![Page 21: CSC309 Winter 2016 Lecture 4 - Department of Computer ...ylzhang/csc309w16/files/lec04-php.pdf · • PHP7.0 released on December 3, 2015 7. Lifecycle a PHP request 8. Hello World!](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97a9a900fb017c432ae26/html5/thumbnails/21.jpg)
Interpreted strings
21
![Page 22: CSC309 Winter 2016 Lecture 4 - Department of Computer ...ylzhang/csc309w16/files/lec04-php.pdf · • PHP7.0 released on December 3, 2015 7. Lifecycle a PHP request 8. Hello World!](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97a9a900fb017c432ae26/html5/thumbnails/22.jpg)
Arrays
22
![Page 23: CSC309 Winter 2016 Lecture 4 - Department of Computer ...ylzhang/csc309w16/files/lec04-php.pdf · • PHP7.0 released on December 3, 2015 7. Lifecycle a PHP request 8. Hello World!](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97a9a900fb017c432ae26/html5/thumbnails/23.jpg)
Arrays
23
![Page 24: CSC309 Winter 2016 Lecture 4 - Department of Computer ...ylzhang/csc309w16/files/lec04-php.pdf · • PHP7.0 released on December 3, 2015 7. Lifecycle a PHP request 8. Hello World!](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97a9a900fb017c432ae26/html5/thumbnails/24.jpg)
Some array functions
24
![Page 25: CSC309 Winter 2016 Lecture 4 - Department of Computer ...ylzhang/csc309w16/files/lec04-php.pdf · • PHP7.0 released on December 3, 2015 7. Lifecycle a PHP request 8. Hello World!](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97a9a900fb017c432ae26/html5/thumbnails/25.jpg)
Functions
25
![Page 26: CSC309 Winter 2016 Lecture 4 - Department of Computer ...ylzhang/csc309w16/files/lec04-php.pdf · • PHP7.0 released on December 3, 2015 7. Lifecycle a PHP request 8. Hello World!](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97a9a900fb017c432ae26/html5/thumbnails/26.jpg)
Functions
26
![Page 27: CSC309 Winter 2016 Lecture 4 - Department of Computer ...ylzhang/csc309w16/files/lec04-php.pdf · • PHP7.0 released on December 3, 2015 7. Lifecycle a PHP request 8. Hello World!](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97a9a900fb017c432ae26/html5/thumbnails/27.jpg)
Default parameter values
27
![Page 28: CSC309 Winter 2016 Lecture 4 - Department of Computer ...ylzhang/csc309w16/files/lec04-php.pdf · • PHP7.0 released on December 3, 2015 7. Lifecycle a PHP request 8. Hello World!](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97a9a900fb017c432ae26/html5/thumbnails/28.jpg)
Comments
28
![Page 29: CSC309 Winter 2016 Lecture 4 - Department of Computer ...ylzhang/csc309w16/files/lec04-php.pdf · • PHP7.0 released on December 3, 2015 7. Lifecycle a PHP request 8. Hello World!](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97a9a900fb017c432ae26/html5/thumbnails/29.jpg)
for loop
29
![Page 30: CSC309 Winter 2016 Lecture 4 - Department of Computer ...ylzhang/csc309w16/files/lec04-php.pdf · • PHP7.0 released on December 3, 2015 7. Lifecycle a PHP request 8. Hello World!](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97a9a900fb017c432ae26/html5/thumbnails/30.jpg)
while loop
30
![Page 31: CSC309 Winter 2016 Lecture 4 - Department of Computer ...ylzhang/csc309w16/files/lec04-php.pdf · • PHP7.0 released on December 3, 2015 7. Lifecycle a PHP request 8. Hello World!](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97a9a900fb017c432ae26/html5/thumbnails/31.jpg)
if-elseif-else
31
![Page 32: CSC309 Winter 2016 Lecture 4 - Department of Computer ...ylzhang/csc309w16/files/lec04-php.pdf · • PHP7.0 released on December 3, 2015 7. Lifecycle a PHP request 8. Hello World!](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97a9a900fb017c432ae26/html5/thumbnails/32.jpg)
foreach loop
32
![Page 33: CSC309 Winter 2016 Lecture 4 - Department of Computer ...ylzhang/csc309w16/files/lec04-php.pdf · • PHP7.0 released on December 3, 2015 7. Lifecycle a PHP request 8. Hello World!](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97a9a900fb017c432ae26/html5/thumbnails/33.jpg)
Expressions in PHP
33
![Page 34: CSC309 Winter 2016 Lecture 4 - Department of Computer ...ylzhang/csc309w16/files/lec04-php.pdf · • PHP7.0 released on December 3, 2015 7. Lifecycle a PHP request 8. Hello World!](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97a9a900fb017c432ae26/html5/thumbnails/34.jpg)
PHP expression block
34
![Page 35: CSC309 Winter 2016 Lecture 4 - Department of Computer ...ylzhang/csc309w16/files/lec04-php.pdf · • PHP7.0 released on December 3, 2015 7. Lifecycle a PHP request 8. Hello World!](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97a9a900fb017c432ae26/html5/thumbnails/35.jpg)
A more complex example
35
![Page 36: CSC309 Winter 2016 Lecture 4 - Department of Computer ...ylzhang/csc309w16/files/lec04-php.pdf · • PHP7.0 released on December 3, 2015 7. Lifecycle a PHP request 8. Hello World!](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97a9a900fb017c432ae26/html5/thumbnails/36.jpg)
Another complex example
36
![Page 37: CSC309 Winter 2016 Lecture 4 - Department of Computer ...ylzhang/csc309w16/files/lec04-php.pdf · • PHP7.0 released on December 3, 2015 7. Lifecycle a PHP request 8. Hello World!](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97a9a900fb017c432ae26/html5/thumbnails/37.jpg)
Bad style
37
![Page 38: CSC309 Winter 2016 Lecture 4 - Department of Computer ...ylzhang/csc309w16/files/lec04-php.pdf · • PHP7.0 released on December 3, 2015 7. Lifecycle a PHP request 8. Hello World!](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97a9a900fb017c432ae26/html5/thumbnails/38.jpg)
PHP include file
38
![Page 39: CSC309 Winter 2016 Lecture 4 - Department of Computer ...ylzhang/csc309w16/files/lec04-php.pdf · • PHP7.0 released on December 3, 2015 7. Lifecycle a PHP request 8. Hello World!](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97a9a900fb017c432ae26/html5/thumbnails/39.jpg)
PHP include file
39
![Page 40: CSC309 Winter 2016 Lecture 4 - Department of Computer ...ylzhang/csc309w16/files/lec04-php.pdf · • PHP7.0 released on December 3, 2015 7. Lifecycle a PHP request 8. Hello World!](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97a9a900fb017c432ae26/html5/thumbnails/40.jpg)
PHP include files
40
![Page 41: CSC309 Winter 2016 Lecture 4 - Department of Computer ...ylzhang/csc309w16/files/lec04-php.pdf · • PHP7.0 released on December 3, 2015 7. Lifecycle a PHP request 8. Hello World!](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97a9a900fb017c432ae26/html5/thumbnails/41.jpg)
demo
41
![Page 42: CSC309 Winter 2016 Lecture 4 - Department of Computer ...ylzhang/csc309w16/files/lec04-php.pdf · • PHP7.0 released on December 3, 2015 7. Lifecycle a PHP request 8. Hello World!](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97a9a900fb017c432ae26/html5/thumbnails/42.jpg)
HTML Forms
42
![Page 43: CSC309 Winter 2016 Lecture 4 - Department of Computer ...ylzhang/csc309w16/files/lec04-php.pdf · • PHP7.0 released on December 3, 2015 7. Lifecycle a PHP request 8. Hello World!](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97a9a900fb017c432ae26/html5/thumbnails/43.jpg)
HTML Forms
43
• In short, HTML forms enables users to submit data to the website.
• This was a big step in the history of Web.
• Server-side programs accepts parameters sent from the users to guide their execution.
![Page 44: CSC309 Winter 2016 Lecture 4 - Department of Computer ...ylzhang/csc309w16/files/lec04-php.pdf · • PHP7.0 released on December 3, 2015 7. Lifecycle a PHP request 8. Hello World!](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97a9a900fb017c432ae26/html5/thumbnails/44.jpg)
Query string
44
![Page 45: CSC309 Winter 2016 Lecture 4 - Department of Computer ...ylzhang/csc309w16/files/lec04-php.pdf · • PHP7.0 released on December 3, 2015 7. Lifecycle a PHP request 8. Hello World!](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97a9a900fb017c432ae26/html5/thumbnails/45.jpg)
HTML Forms
45
![Page 46: CSC309 Winter 2016 Lecture 4 - Department of Computer ...ylzhang/csc309w16/files/lec04-php.pdf · • PHP7.0 released on December 3, 2015 7. Lifecycle a PHP request 8. Hello World!](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97a9a900fb017c432ae26/html5/thumbnails/46.jpg)
HTML Form Example
46
![Page 47: CSC309 Winter 2016 Lecture 4 - Department of Computer ...ylzhang/csc309w16/files/lec04-php.pdf · • PHP7.0 released on December 3, 2015 7. Lifecycle a PHP request 8. Hello World!](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97a9a900fb017c432ae26/html5/thumbnails/47.jpg)
demo continued…
47
![Page 48: CSC309 Winter 2016 Lecture 4 - Department of Computer ...ylzhang/csc309w16/files/lec04-php.pdf · • PHP7.0 released on December 3, 2015 7. Lifecycle a PHP request 8. Hello World!](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97a9a900fb017c432ae26/html5/thumbnails/48.jpg)
PHP Sessions
48
![Page 49: CSC309 Winter 2016 Lecture 4 - Department of Computer ...ylzhang/csc309w16/files/lec04-php.pdf · • PHP7.0 released on December 3, 2015 7. Lifecycle a PHP request 8. Hello World!](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97a9a900fb017c432ae26/html5/thumbnails/49.jpg)
Why sessions
49
• Normal HTTP requests are stateless, i.e., request, response and done. Nothing is remembered between different requests.
• If you refresh a page, all states of the page would be gone
• But sometimes we want to remember things across different page visits.
• The user can start a session, store information of the session in a file on the server, and use it across different requests.
![Page 50: CSC309 Winter 2016 Lecture 4 - Department of Computer ...ylzhang/csc309w16/files/lec04-php.pdf · • PHP7.0 released on December 3, 2015 7. Lifecycle a PHP request 8. Hello World!](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97a9a900fb017c432ae26/html5/thumbnails/50.jpg)
How session works
50
• client's browser makes an initial request to the server
• server notes client's IP address/browser, stores some local session data, and sends a session ID back to client (via cookies)
• client sends that same session ID (in a cookie) back to server on future requests
• server uses session ID to retrieve the data for the client's session later, like a ticket given at a coat-check room
![Page 51: CSC309 Winter 2016 Lecture 4 - Department of Computer ...ylzhang/csc309w16/files/lec04-php.pdf · • PHP7.0 released on December 3, 2015 7. Lifecycle a PHP request 8. Hello World!](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97a9a900fb017c432ae26/html5/thumbnails/51.jpg)
Use sessions in PHP
51
• session_start()
• session_destroy()
• value = $_SESSION[key]
• session_save_path(dirname)
• dirname must exist in your current folder, with permission 700
• current folder since we have suPHP on CS.UTM server
![Page 52: CSC309 Winter 2016 Lecture 4 - Department of Computer ...ylzhang/csc309w16/files/lec04-php.pdf · • PHP7.0 released on December 3, 2015 7. Lifecycle a PHP request 8. Hello World!](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97a9a900fb017c432ae26/html5/thumbnails/52.jpg)
demo continued…
52
![Page 53: CSC309 Winter 2016 Lecture 4 - Department of Computer ...ylzhang/csc309w16/files/lec04-php.pdf · • PHP7.0 released on December 3, 2015 7. Lifecycle a PHP request 8. Hello World!](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97a9a900fb017c432ae26/html5/thumbnails/53.jpg)
Reference: • http://www.w3schools.com/php/default.asp • http://php.net/manual/en/
53
![Page 54: CSC309 Winter 2016 Lecture 4 - Department of Computer ...ylzhang/csc309w16/files/lec04-php.pdf · • PHP7.0 released on December 3, 2015 7. Lifecycle a PHP request 8. Hello World!](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97a9a900fb017c432ae26/html5/thumbnails/54.jpg)
Next week
54
• More PHP and server-side programming