54
CSC309 Winter 2016 Lecture 4 Larry Zhang 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!

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

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!

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!

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!

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!

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!

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!

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!

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!

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!

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!

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!

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!

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!

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!

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!

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!

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!

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!

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!

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!

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!

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!

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!

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!

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!

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!

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!

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!

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!

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!

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!

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!

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!

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!

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!

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!

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!

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!

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!

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!

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!

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!

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!

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!

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!

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!

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!

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!

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!

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!

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!

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!

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!

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!

Next week

54

• More PHP and server-side programming