using php in a web application - internet applications, id1354 · architecture using php in a web...

94
PHP Cookies HTTP Sessions HTTP Parameters Application Scope and File Handling To Identify a List Item Architecture Using PHP in a Web Application Internet Applications, ID1354 1 / 36

Upload: others

Post on 19-Mar-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

Using PHP in a WebApplication

Internet Applications, ID1354

1 / 36

Page 2: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

Contents

2 / 36

Page 3: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

Section

3 / 36

Page 4: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

CookiesI HTTP is stateless. Still there are many

reasons why it is useful for a server toidentify the client.

I Authentication (login)I SettingsI AdvertisingI Shopping basket

I This is solved with cookies.I A cookie is a name/value pair passed

between browser and server in the HTTPheader.

I A cookie is only passed to the server fromwhich it originated.

4 / 36

Page 5: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

CookiesI HTTP is stateless. Still there are many

reasons why it is useful for a server toidentify the client.

I Authentication (login)I SettingsI AdvertisingI Shopping basket

I This is solved with cookies.

I A cookie is a name/value pair passedbetween browser and server in the HTTPheader.

I A cookie is only passed to the server fromwhich it originated.

4 / 36

Page 6: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

CookiesI HTTP is stateless. Still there are many

reasons why it is useful for a server toidentify the client.

I Authentication (login)I SettingsI AdvertisingI Shopping basket

I This is solved with cookies.I A cookie is a name/value pair passed

between browser and server in the HTTPheader.

I A cookie is only passed to the server fromwhich it originated.

4 / 36

Page 7: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

CookiesI HTTP is stateless. Still there are many

reasons why it is useful for a server toidentify the client.

I Authentication (login)I SettingsI AdvertisingI Shopping basket

I This is solved with cookies.I A cookie is a name/value pair passed

between browser and server in the HTTPheader.

I A cookie is only passed to the server fromwhich it originated.

4 / 36

Page 8: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

CookiesI HTTP is stateless. Still there are many

reasons why it is useful for a server toidentify the client.

I Authentication (login)I SettingsI AdvertisingI Shopping basket

I This is solved with cookies.I A cookie is a name/value pair passed

between browser and server in the HTTPheader.

I A cookie is only passed to the server fromwhich it originated.

4 / 36

Page 9: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

To Set a CookieI Cookies are set with the setcookie

function. Since cookies are sent as HTTPheaders, this function must be called beforeany output is generated.

setcookie (string $name, string $value,int $expire = 0, string $path,string $domain, bool $secure = false,bool $httponly = false)

I name and value is the cookie’sname/value pair.

I expire tells the instant in time when thecookie expires. time() returns thecurrent time, so time()+60*60*24*30sets the cookie to expire in 30 days.

5 / 36

Page 10: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

To Set a CookieI Cookies are set with the setcookie

function. Since cookies are sent as HTTPheaders, this function must be called beforeany output is generated.

setcookie (string $name, string $value,int $expire = 0, string $path,string $domain, bool $secure = false,bool $httponly = false)

I name and value is the cookie’sname/value pair.

I expire tells the instant in time when thecookie expires. time() returns thecurrent time, so time()+60*60*24*30sets the cookie to expire in 30 days.

5 / 36

Page 11: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

To Set a CookieI Cookies are set with the setcookie

function. Since cookies are sent as HTTPheaders, this function must be called beforeany output is generated.

setcookie (string $name, string $value,int $expire = 0, string $path,string $domain, bool $secure = false,bool $httponly = false)

I name and value is the cookie’sname/value pair.

I expire tells the instant in time when thecookie expires. time() returns thecurrent time, so time()+60*60*24*30sets the cookie to expire in 30 days.

5 / 36

Page 12: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

To Set a CookieI Cookies are set with the setcookie

function. Since cookies are sent as HTTPheaders, this function must be called beforeany output is generated.

setcookie (string $name, string $value,int $expire = 0, string $path,string $domain, bool $secure = false,bool $httponly = false)

I name and value is the cookie’sname/value pair.

I expire tells the instant in time when thecookie expires. time() returns thecurrent time, so time()+60*60*24*30sets the cookie to expire in 30 days.

5 / 36

Page 13: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

To Retrieve a Cookie

I Cookies are retrieved using the $_COOKIEsuperglobal, which is an array containing allcookies included in the current request.

I The following statement retrieves allcookies with the name userid.$_COOKIE["userid"];

I The isset function can be used to checkif a cookie is set.if (!isset($_COOKIE["userid"])) {

echo ’<a href="login.php">log in</a>’;}

6 / 36

Page 14: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

To Retrieve a Cookie

I Cookies are retrieved using the $_COOKIEsuperglobal, which is an array containing allcookies included in the current request.

I The following statement retrieves allcookies with the name userid.$_COOKIE["userid"];

I The isset function can be used to checkif a cookie is set.if (!isset($_COOKIE["userid"])) {

echo ’<a href="login.php">log in</a>’;}

6 / 36

Page 15: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

To Retrieve a Cookie

I Cookies are retrieved using the $_COOKIEsuperglobal, which is an array containing allcookies included in the current request.

I The following statement retrieves allcookies with the name userid.$_COOKIE["userid"];

I The isset function can be used to checkif a cookie is set.if (!isset($_COOKIE["userid"])) {

echo ’<a href="login.php">log in</a>’;}

6 / 36

Page 16: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

Third Party CookiesI Cookies set by a server with a domain

name different from the server’s.I If many servers set the same third party

cookie, the third party server can track theuser’s surfing.

I Typically used for marketing.I There are many other ways, beside

cookies, to identify a user for trackingpurposes, for example IP address, installedsoftware, fingerprinting browserinformation, social networks, pixelplacement + url rewriting, etc.

7 / 36

Page 17: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

Third Party CookiesI Cookies set by a server with a domain

name different from the server’s.I If many servers set the same third party

cookie, the third party server can track theuser’s surfing.

I Typically used for marketing.

I There are many other ways, besidecookies, to identify a user for trackingpurposes, for example IP address, installedsoftware, fingerprinting browserinformation, social networks, pixelplacement + url rewriting, etc.

7 / 36

Page 18: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

Third Party CookiesI Cookies set by a server with a domain

name different from the server’s.I If many servers set the same third party

cookie, the third party server can track theuser’s surfing.

I Typically used for marketing.I There are many other ways, beside

cookies, to identify a user for trackingpurposes, for example IP address, installedsoftware, fingerprinting browserinformation, social networks, pixelplacement + url rewriting, etc.

7 / 36

Page 19: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

Third Party CookiesI Cookies set by a server with a domain

name different from the server’s.I If many servers set the same third party

cookie, the third party server can track theuser’s surfing.

I Typically used for marketing.I There are many other ways, beside

cookies, to identify a user for trackingpurposes, for example IP address, installedsoftware, fingerprinting browserinformation, social networks, pixelplacement + url rewriting, etc.

7 / 36

Page 20: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

The EU Cookie LawA person shall not store or gain access toinformation stored, in the terminal equipment ofa subscriber or user unless the requirements ofparagraph (2) are met.

(2) The requirements are that the subscriber oruser of that terminal equipment

1. is provided with clear and comprehensiveinformation about the purposes of thestorage of, or access to, that information;and

2. has given his or her consent.

8 / 36

Page 21: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

Exceptions To The Law

I The cookie is for the sole purpose ofcarrying out the transmission of acommunication over an electroniccommunications network.

I Not relevant here.

I The cookie is strictly necessary for theprovision of an information society servicerequested by the subscriber or user.

I Likely applies to authentication and shoppingbaskets.

9 / 36

Page 22: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

Exceptions To The Law

I The cookie is for the sole purpose ofcarrying out the transmission of acommunication over an electroniccommunications network.

I Not relevant here.I The cookie is strictly necessary for the

provision of an information society servicerequested by the subscriber or user.

I Likely applies to authentication and shoppingbaskets.

9 / 36

Page 23: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

Exceptions To The Law

I The cookie is for the sole purpose ofcarrying out the transmission of acommunication over an electroniccommunications network.

I Not relevant here.I The cookie is strictly necessary for the

provision of an information society servicerequested by the subscriber or user.

I Likely applies to authentication and shoppingbaskets.

9 / 36

Page 24: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

Exceptions To The Law

I The cookie is for the sole purpose ofcarrying out the transmission of acommunication over an electroniccommunications network.

I Not relevant here.I The cookie is strictly necessary for the

provision of an information society servicerequested by the subscriber or user.

I Likely applies to authentication and shoppingbaskets.

9 / 36

Page 25: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

Do Not Track Specification

I Do Not Track, DNT, is a W3C specificationenabling the user to express preferencesregarding tracking.

I Defines a HTTP header, and how to handleit on the server.

I It is not mandatory in any way to obey theusers preferences.

I Must be implemented by server developer.

10 / 36

Page 26: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

Do Not Track Specification

I Do Not Track, DNT, is a W3C specificationenabling the user to express preferencesregarding tracking.

I Defines a HTTP header, and how to handleit on the server.

I It is not mandatory in any way to obey theusers preferences.

I Must be implemented by server developer.

10 / 36

Page 27: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

Do Not Track Specification

I Do Not Track, DNT, is a W3C specificationenabling the user to express preferencesregarding tracking.

I Defines a HTTP header, and how to handleit on the server.

I It is not mandatory in any way to obey theusers preferences.

I Must be implemented by server developer.

10 / 36

Page 28: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

Do Not Track Specification

I Do Not Track, DNT, is a W3C specificationenabling the user to express preferencesregarding tracking.

I Defines a HTTP header, and how to handleit on the server.

I It is not mandatory in any way to obey theusers preferences.

I Must be implemented by server developer.

10 / 36

Page 29: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

ArchitectureQuestion 1

11 / 36

Page 30: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

Section

12 / 36

Page 31: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

SessionsI A session is the time span during which a particular

browser interacts with a particular server.

I For session tracking, PHP creates and maintains asession tracking id (Unique ID, UID), for each visitorand stores variables based on this UID.

I The UID is stored on the client, for example in acookie or as part of URLs, and included in eachrequest to the server.

I The only way to terminate a session is to manuallyunset all data related to the session in theserver-side code.

I If a session is not explicitly terminated, it times outafter an interval specified in server configuration,and session data is removed.

13 / 36

Page 32: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

SessionsI A session is the time span during which a particular

browser interacts with a particular server.

I For session tracking, PHP creates and maintains asession tracking id (Unique ID, UID), for each visitorand stores variables based on this UID.

I The UID is stored on the client, for example in acookie or as part of URLs, and included in eachrequest to the server.

I The only way to terminate a session is to manuallyunset all data related to the session in theserver-side code.

I If a session is not explicitly terminated, it times outafter an interval specified in server configuration,and session data is removed.

13 / 36

Page 33: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

SessionsI A session is the time span during which a particular

browser interacts with a particular server.

I For session tracking, PHP creates and maintains asession tracking id (Unique ID, UID), for each visitorand stores variables based on this UID.

I The UID is stored on the client, for example in acookie or as part of URLs, and included in eachrequest to the server.

I The only way to terminate a session is to manuallyunset all data related to the session in theserver-side code.

I If a session is not explicitly terminated, it times outafter an interval specified in server configuration,and session data is removed.

13 / 36

Page 34: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

SessionsI A session is the time span during which a particular

browser interacts with a particular server.

I For session tracking, PHP creates and maintains asession tracking id (Unique ID, UID), for each visitorand stores variables based on this UID.

I The UID is stored on the client, for example in acookie or as part of URLs, and included in eachrequest to the server.

I The only way to terminate a session is to manuallyunset all data related to the session in theserver-side code.

I If a session is not explicitly terminated, it times outafter an interval specified in server configuration,and session data is removed.

13 / 36

Page 35: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

SessionsI A session is the time span during which a particular

browser interacts with a particular server.

I For session tracking, PHP creates and maintains asession tracking id (Unique ID, UID), for each visitorand stores variables based on this UID.

I The UID is stored on the client, for example in acookie or as part of URLs, and included in eachrequest to the server.

I The only way to terminate a session is to manuallyunset all data related to the session in theserver-side code.

I If a session is not explicitly terminated, it times outafter an interval specified in server configuration,and session data is removed.

13 / 36

Page 36: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

Session Management

I A session is started with thesession_start function.

I To associate data with a session, use the$_SESSION superglobal.

I To delete all data from the session, use thesession_destroy function.

14 / 36

Page 37: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

Session Management

I A session is started with thesession_start function.

I To associate data with a session, use the$_SESSION superglobal.

I To delete all data from the session, use thesession_destroy function.

14 / 36

Page 38: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

Session Management

I A session is started with thesession_start function.

I To associate data with a session, use the$_SESSION superglobal.

I To delete all data from the session, use thesession_destroy function.

14 / 36

Page 39: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

How is session data saved?

I We must understand that the lifetime of aPHP variable is limited to the execution ofthe program where it is created.

I This means that a variable created in onerequest will not exist in later requests.

I Therefore, the content of $_SESSIONmust be stored externally to the PHPinterpreter.

I This storage is called a session savehandler, and is configurable. Normally, andalso normally by default, a file is used.

15 / 36

Page 40: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

How is session data saved?

I We must understand that the lifetime of aPHP variable is limited to the execution ofthe program where it is created.

I This means that a variable created in onerequest will not exist in later requests.

I Therefore, the content of $_SESSIONmust be stored externally to the PHPinterpreter.

I This storage is called a session savehandler, and is configurable. Normally, andalso normally by default, a file is used.

15 / 36

Page 41: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

How is session data saved?

I We must understand that the lifetime of aPHP variable is limited to the execution ofthe program where it is created.

I This means that a variable created in onerequest will not exist in later requests.

I Therefore, the content of $_SESSIONmust be stored externally to the PHPinterpreter.

I This storage is called a session savehandler, and is configurable. Normally, andalso normally by default, a file is used.

15 / 36

Page 42: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

How is session data saved?

I We must understand that the lifetime of aPHP variable is limited to the execution ofthe program where it is created.

I This means that a variable created in onerequest will not exist in later requests.

I Therefore, the content of $_SESSIONmust be stored externally to the PHPinterpreter.

I This storage is called a session savehandler, and is configurable. Normally, andalso normally by default, a file is used.

15 / 36

Page 43: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

How is a session identified?I To fill the $_SESSION superglobal with the

current user’s data, the session savehandler must be able to identify the user.

I This is normally done using a cookie.

I After session_start is called, PHP willlook for a cookie named PHPSESSID.

I If it is present, its value will be used as the idof the current session.

I If it is not present, it will be created and itsvalue will be set to the id of the currentsession.

I We must understand that the PHPSESSIDcookie is the link between a browser andthat browser’s session data on the server.

16 / 36

Page 44: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

How is a session identified?I To fill the $_SESSION superglobal with the

current user’s data, the session savehandler must be able to identify the user.

I This is normally done using a cookie.I After session_start is called, PHP will

look for a cookie named PHPSESSID.

I If it is present, its value will be used as the idof the current session.

I If it is not present, it will be created and itsvalue will be set to the id of the currentsession.

I We must understand that the PHPSESSIDcookie is the link between a browser andthat browser’s session data on the server.

16 / 36

Page 45: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

How is a session identified?I To fill the $_SESSION superglobal with the

current user’s data, the session savehandler must be able to identify the user.

I This is normally done using a cookie.I After session_start is called, PHP will

look for a cookie named PHPSESSID.I If it is present, its value will be used as the id

of the current session.

I If it is not present, it will be created and itsvalue will be set to the id of the currentsession.

I We must understand that the PHPSESSIDcookie is the link between a browser andthat browser’s session data on the server.

16 / 36

Page 46: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

How is a session identified?I To fill the $_SESSION superglobal with the

current user’s data, the session savehandler must be able to identify the user.

I This is normally done using a cookie.I After session_start is called, PHP will

look for a cookie named PHPSESSID.I If it is present, its value will be used as the id

of the current session.I If it is not present, it will be created and its

value will be set to the id of the currentsession.

I We must understand that the PHPSESSIDcookie is the link between a browser andthat browser’s session data on the server.

16 / 36

Page 47: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

How is a session identified?I To fill the $_SESSION superglobal with the

current user’s data, the session savehandler must be able to identify the user.

I This is normally done using a cookie.I After session_start is called, PHP will

look for a cookie named PHPSESSID.I If it is present, its value will be used as the id

of the current session.I If it is not present, it will be created and its

value will be set to the id of the currentsession.

I We must understand that the PHPSESSIDcookie is the link between a browser andthat browser’s session data on the server.

16 / 36

Page 48: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

How is a session identified?I To fill the $_SESSION superglobal with the

current user’s data, the session savehandler must be able to identify the user.

I This is normally done using a cookie.I After session_start is called, PHP will

look for a cookie named PHPSESSID.I If it is present, its value will be used as the id

of the current session.I If it is not present, it will be created and its

value will be set to the id of the currentsession.

I We must understand that the PHPSESSIDcookie is the link between a browser andthat browser’s session data on the server.

16 / 36

Page 49: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

Session Example

At session startconst USER_KEY = ’user_key’;session_start();//Assuming $user is an object with user data.$_SESSION[USER_KEY] = serialize($user);

During the sessionif (isset($_SESSION[USER_KEY]) {

$my_data = unserialize($_SESSION[USER_KEY]);}

At session end.session_destroy();

17 / 36

Page 50: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

Session Example

At session startconst USER_KEY = ’user_key’;session_start();//Assuming $user is an object with user data.$_SESSION[USER_KEY] = serialize($user);

During the sessionif (isset($_SESSION[USER_KEY]) {

$my_data = unserialize($_SESSION[USER_KEY]);}

At session end.session_destroy();

17 / 36

Page 51: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

Session Example

At session startconst USER_KEY = ’user_key’;session_start();//Assuming $user is an object with user data.$_SESSION[USER_KEY] = serialize($user);

During the sessionif (isset($_SESSION[USER_KEY]) {

$my_data = unserialize($_SESSION[USER_KEY]);}

At session end.session_destroy();

17 / 36

Page 52: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

ArchitectureQuestion 2

18 / 36

Page 53: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

Section

19 / 36

Page 54: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

HTTP Parameters

I The $_GET and $_POST superglobals areused to retrieve HTTP parameters, forexample user input in a form.

I $_GET is an array with all parameters in aHTTP GET request, $_POST is a similararray for a POST request.

20 / 36

Page 55: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

HTTP Parameters

I The $_GET and $_POST superglobals areused to retrieve HTTP parameters, forexample user input in a form.

I $_GET is an array with all parameters in aHTTP GET request, $_POST is a similararray for a POST request.

20 / 36

Page 56: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

HTTP Parameter Example

The following code retrieves the value of theaddress parameter, which might originatefrom an HTML form.//The text field where the user types the address//must have the attribute name=’address’

const ADDRESS_KEY = ’address’;if (isset($_POST[ADDRESS_KEY])) {

$address = $_POST[ADDRESS_KEY];}

21 / 36

Page 57: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

ArchitectureQuestion 3

22 / 36

Page 58: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

Section

23 / 36

Page 59: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

Application Scope DataI As opposed to other server-side

technologies, PHP does not havesomething like a $_SESSION superglobalthat is shared between different users.

I If data is to be shared between differentusers, such a mechanism must beconstructed.

I A simple approach is to store data withapplication scope in a file.

I Other alternatives are a database, an xmlfile or a plug-in such as memcached,http://www.memcached.org/, whichstores key/value pairs in memory.

24 / 36

Page 60: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

Application Scope DataI As opposed to other server-side

technologies, PHP does not havesomething like a $_SESSION superglobalthat is shared between different users.

I If data is to be shared between differentusers, such a mechanism must beconstructed.

I A simple approach is to store data withapplication scope in a file.

I Other alternatives are a database, an xmlfile or a plug-in such as memcached,http://www.memcached.org/, whichstores key/value pairs in memory.

24 / 36

Page 61: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

Application Scope DataI As opposed to other server-side

technologies, PHP does not havesomething like a $_SESSION superglobalthat is shared between different users.

I If data is to be shared between differentusers, such a mechanism must beconstructed.

I A simple approach is to store data withapplication scope in a file.

I Other alternatives are a database, an xmlfile or a plug-in such as memcached,http://www.memcached.org/, whichstores key/value pairs in memory.

24 / 36

Page 62: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

Application Scope DataI As opposed to other server-side

technologies, PHP does not havesomething like a $_SESSION superglobalthat is shared between different users.

I If data is to be shared between differentusers, such a mechanism must beconstructed.

I A simple approach is to store data withapplication scope in a file.

I Other alternatives are a database, an xmlfile or a plug-in such as memcached,http://www.memcached.org/, whichstores key/value pairs in memory.

24 / 36

Page 63: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

File Handling

I Simple file handling can be done withfile_put_contents, which writes to afile, and file_get_contents, whichreads.

\file_put_contents($path_to_file,$data, FILE_APPEND);

\file_get_contents($path_to_file));

25 / 36

Page 64: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

File Handling

I Simple file handling can be done withfile_put_contents, which writes to afile, and file_get_contents, whichreads.

\file_put_contents($path_to_file,$data, FILE_APPEND);

\file_get_contents($path_to_file));

25 / 36

Page 65: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

ArchitectureQuestion 4

26 / 36

Page 66: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

Section

27 / 36

Page 67: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

The Problem

I There is a list with buttons(or links) for multiple items,like the chat applicationexample to the left.

I How can we know whichbutton the user clicked? Inthis chat example, howcan we know which entryStina wants to delete?

28 / 36

Page 68: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

The Problem

I There is a list with buttons(or links) for multiple items,like the chat applicationexample to the left.

I How can we know whichbutton the user clicked? Inthis chat example, howcan we know which entryStina wants to delete?

28 / 36

Page 69: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

The Solution, Hidden Field

I Make a form for each item in the list.I In this chat example, that means one form for

each entry that has a Delete button.

I Each form includes a hidden field, whichholds an identifier for the list item where theform is placed.

I In this example, we use the time when theentry was written as identifier.

29 / 36

Page 70: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

The Solution, Hidden Field

I Make a form for each item in the list.I In this chat example, that means one form for

each entry that has a Delete button.I Each form includes a hidden field, which

holds an identifier for the list item where theform is placed.

I In this example, we use the time when theentry was written as identifier.

29 / 36

Page 71: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

The Solution, Hidden Field

I Make a form for each item in the list.I In this chat example, that means one form for

each entry that has a Delete button.I Each form includes a hidden field, which

holds an identifier for the list item where theform is placed.

I In this example, we use the time when theentry was written as identifier.

29 / 36

Page 72: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

The Solution, Hidden Field

I Make a form for each item in the list.I In this chat example, that means one form for

each entry that has a Delete button.I Each form includes a hidden field, which

holds an identifier for the list item where theform is placed.

I In this example, we use the time when theentry was written as identifier.

29 / 36

Page 73: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

The Solution, Hidden Field

I A hidden field is not displayed in thebrowser, but included when the form issubmitted.

I The HTML for the chat conversation is listedbelow.

30 / 36

Page 74: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

The Solution, Hidden FieldI A hidden field is not displayed in the

browser, but included when the form issubmitted.

I The HTML for the chat conversation is listedbelow.

30 / 36

Page 75: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

The Solution, Server CodeI On the server, we simply read the

timestamp of the submitted form and deletethe entry with that timestamp.

I Code is not complete, just illustrates theprinciple. Complete code is found oncourse web page.

for ($i = count($entries) - 1; $i >= 0; $i--) {$entry = unserialize($entries[$i]);if ($entry->getTimestamp() ==

$_GET[CHAT_TIMESTAMP_KEY]) {$entry->setDeleted(true);$entries[$i] = serialize($entry);break;

}}

31 / 36

Page 76: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

The Solution, Server CodeI On the server, we simply read the

timestamp of the submitted form and deletethe entry with that timestamp.

I Code is not complete, just illustrates theprinciple. Complete code is found oncourse web page.

for ($i = count($entries) - 1; $i >= 0; $i--) {$entry = unserialize($entries[$i]);if ($entry->getTimestamp() ==

$_GET[CHAT_TIMESTAMP_KEY]) {$entry->setDeleted(true);$entries[$i] = serialize($entry);break;

}}

31 / 36

Page 77: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

ArchitectureQuestion 5

32 / 36

Page 78: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

Section

33 / 36

Page 79: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

Remember Object OrientedDesign?

I We want the code to be easy to modify andeasy to understand. To achieve this weneed (among other things):

I High Cohesion, Each class, method, etchas well-defined knowledge and awell-defined task.

I Low coupling, Objects and subsystems donot depend on each other more thannecessary.

I Encapsulation, Objects and subsystems donot reveal their internals.

34 / 36

Page 80: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

Remember Object OrientedDesign?

I We want the code to be easy to modify andeasy to understand. To achieve this weneed (among other things):

I High Cohesion, Each class, method, etchas well-defined knowledge and awell-defined task.

I Low coupling, Objects and subsystems donot depend on each other more thannecessary.

I Encapsulation, Objects and subsystems donot reveal their internals.

34 / 36

Page 81: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

Remember Object OrientedDesign?

I We want the code to be easy to modify andeasy to understand. To achieve this weneed (among other things):

I High Cohesion, Each class, method, etchas well-defined knowledge and awell-defined task.

I Low coupling, Objects and subsystems donot depend on each other more thannecessary.

I Encapsulation, Objects and subsystems donot reveal their internals.

34 / 36

Page 82: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

Remember Object OrientedDesign?

I We want the code to be easy to modify andeasy to understand. To achieve this weneed (among other things):

I High Cohesion, Each class, method, etchas well-defined knowledge and awell-defined task.

I Low coupling, Objects and subsystems donot depend on each other more thannecessary.

I Encapsulation, Objects and subsystems donot reveal their internals.

34 / 36

Page 83: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

A Very Simple ArchitectureI Server-side architecture is covered

extensively later in the course.I For now, we will use a very simple

architecture.

I This means using one PHP file for eachpossible HTTP request.

I However, handling everything related to aparticular HTTP request in a separate filehas big disadvantages:

I Low cohesion since that file will do everything.I High coupling since code for view handling,

database access, etc, will be placed in thesame file.

I Duplicated code since similar code will appearin several such files.

35 / 36

Page 84: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

A Very Simple ArchitectureI Server-side architecture is covered

extensively later in the course.I For now, we will use a very simple

architecture.I This means using one PHP file for each

possible HTTP request.

I However, handling everything related to aparticular HTTP request in a separate filehas big disadvantages:

I Low cohesion since that file will do everything.I High coupling since code for view handling,

database access, etc, will be placed in thesame file.

I Duplicated code since similar code will appearin several such files.

35 / 36

Page 85: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

A Very Simple ArchitectureI Server-side architecture is covered

extensively later in the course.I For now, we will use a very simple

architecture.I This means using one PHP file for each

possible HTTP request.I However, handling everything related to a

particular HTTP request in a separate filehas big disadvantages:

I Low cohesion since that file will do everything.I High coupling since code for view handling,

database access, etc, will be placed in thesame file.

I Duplicated code since similar code will appearin several such files.

35 / 36

Page 86: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

A Very Simple ArchitectureI Server-side architecture is covered

extensively later in the course.I For now, we will use a very simple

architecture.I This means using one PHP file for each

possible HTTP request.I However, handling everything related to a

particular HTTP request in a separate filehas big disadvantages:

I Low cohesion since that file will do everything.

I High coupling since code for view handling,database access, etc, will be placed in thesame file.

I Duplicated code since similar code will appearin several such files.

35 / 36

Page 87: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

A Very Simple ArchitectureI Server-side architecture is covered

extensively later in the course.I For now, we will use a very simple

architecture.I This means using one PHP file for each

possible HTTP request.I However, handling everything related to a

particular HTTP request in a separate filehas big disadvantages:

I Low cohesion since that file will do everything.I High coupling since code for view handling,

database access, etc, will be placed in thesame file.

I Duplicated code since similar code will appearin several such files.

35 / 36

Page 88: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

A Very Simple ArchitectureI Server-side architecture is covered

extensively later in the course.I For now, we will use a very simple

architecture.I This means using one PHP file for each

possible HTTP request.I However, handling everything related to a

particular HTTP request in a separate filehas big disadvantages:

I Low cohesion since that file will do everything.I High coupling since code for view handling,

database access, etc, will be placed in thesame file.

I Duplicated code since similar code will appearin several such files.

35 / 36

Page 89: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

A Very Simple ArchitectureI Server-side architecture is covered

extensively later in the course.I For now, we will use a very simple

architecture.I This means using one PHP file for each

possible HTTP request.I However, handling everything related to a

particular HTTP request in a separate filehas big disadvantages:

I Low cohesion since that file will do everything.I High coupling since code for view handling,

database access, etc, will be placed in thesame file.

I Duplicated code since similar code will appearin several such files.

35 / 36

Page 90: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

A Slightly Better ArchitectureI Fragments (header, footer, etc) are

placed in a separate directory andincluded in each page.

I View (HTML code) is placed in separatefiles, chat.php and index.php.

I Entry.php is a class that representsan entry in the conversation. It isincluded where needed in the HTTPrequest handling PHP files.

I keys.php holds some constants thatare used in multiple places. It is includedwhere needed in the HTTP requesthandling PHP files.

I The files handling HTTP requests are login.php,store-entry.php and delete-entry.php

36 / 36

Page 91: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

A Slightly Better ArchitectureI Fragments (header, footer, etc) are

placed in a separate directory andincluded in each page.

I View (HTML code) is placed in separatefiles, chat.php and index.php.

I Entry.php is a class that representsan entry in the conversation. It isincluded where needed in the HTTPrequest handling PHP files.

I keys.php holds some constants thatare used in multiple places. It is includedwhere needed in the HTTP requesthandling PHP files.

I The files handling HTTP requests are login.php,store-entry.php and delete-entry.php

36 / 36

Page 92: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

A Slightly Better ArchitectureI Fragments (header, footer, etc) are

placed in a separate directory andincluded in each page.

I View (HTML code) is placed in separatefiles, chat.php and index.php.

I Entry.php is a class that representsan entry in the conversation. It isincluded where needed in the HTTPrequest handling PHP files.

I keys.php holds some constants thatare used in multiple places. It is includedwhere needed in the HTTP requesthandling PHP files.

I The files handling HTTP requests are login.php,store-entry.php and delete-entry.php

36 / 36

Page 93: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

A Slightly Better ArchitectureI Fragments (header, footer, etc) are

placed in a separate directory andincluded in each page.

I View (HTML code) is placed in separatefiles, chat.php and index.php.

I Entry.php is a class that representsan entry in the conversation. It isincluded where needed in the HTTPrequest handling PHP files.

I keys.php holds some constants thatare used in multiple places. It is includedwhere needed in the HTTP requesthandling PHP files.

I The files handling HTTP requests are login.php,store-entry.php and delete-entry.php

36 / 36

Page 94: Using PHP in a Web Application - Internet Applications, ID1354 · Architecture Using PHP in a Web Application Internet Applications, ID1354 1/36. PHP Cookies HTTP Sessions HTTP Parameters

PHP

Cookies

HTTP Sessions

HTTP Parameters

Application Scopeand File Handling

To Identify a List Item

Architecture

A Slightly Better ArchitectureI Fragments (header, footer, etc) are

placed in a separate directory andincluded in each page.

I View (HTML code) is placed in separatefiles, chat.php and index.php.

I Entry.php is a class that representsan entry in the conversation. It isincluded where needed in the HTTPrequest handling PHP files.

I keys.php holds some constants thatare used in multiple places. It is includedwhere needed in the HTTP requesthandling PHP files.

I The files handling HTTP requests are login.php,store-entry.php and delete-entry.php

36 / 36