Matthew M.

Matthew M.

Cookies

Slide Duration:

Table of Contents

Section 1: Advanced PHP with MySQL
Course Introduction

13m 36s

Intro
0:00
Advanced PHP w/ MySQL
0:13
Course Introduction
0:14
Course Content
0:59
Web Application: Educator Store
1:45
Web Application: Educator Store
1:46
Object-Oriented Programming
6:09
Object-Oriented Programming Overview
6:10
MySQL
6:50
MySQL Overview
6:51
Example: Command Prompt & MySQL
8:34
What You Will Learn
9:34
What You Will Learn
9:35
Course Prerequisites
11:52
Course Prerequisites
11:53
Advanced Course Development Environment

18m 46s

Intro
0:00
Lesson Overview
0:11
Lesson Overview
0:12
Development Environment Setup
1:36
Firefox Web Browser, XAMPP, and PSPad Text Editor
1:37
Course Directory Structure
4:36
Course Directory Structure
4:37
phpDocumentor
7:23
phpDocumentor Overview
7:24
Parsing Source Code
7:44
docs Subdirectory
9:06
Asvanced PHP Course Documentation Example
10:21
Previous Educator PHP Students
14:14
Lib Directory Changes
14:38
Images Directory Changes
14:57
VERSION Constant
15:40
Comments Noting a Specific Version Number
17:04
Homework Challenge
17:30
Homework Challenge
17:31
Introduction to File I/O

35m 37s

Intro
0:00
Lesson Overview
0:10
Lesson Overview
0:11
File I/O
1:34
File I/O Overview
1:35
Performing file I/O: 3 Steps
1:52
File Permissions
2:28
Read Permission & Write Permission
2:29
fopen()
4:47
fopen()
4:48
Two Required Parameters
5:01
Successful fopen(): File Handle and Resource
5:21
FALSE and E_WARNING Error
6:00
Example
6:39
File Paths
7:10
File Paths
7:11
File Access Modes
9:18
File Access Modes
9:19
Coding Example: fopen()
10:31
Using Relative and Absolute Path & Non-Existent File
10:32
File Pointers
15:55
File Pointers
15:56
Example File
16:11
Opening a File with fopen() Using 'r' as the Access Mode
16:22
fgets()
17:55
fgets() Overview
17:56
Coding Example: fgets() - Reading a Line at a Time
18:26
feof()
20:10
feof() Overview
20:09
Coding Example: feof()
20:50
fclose()
23:43
fclose() Overview
23:44
Coding Example: fclose()
25:02
Coding Example: Current Stock
26:10
Coding Example: Current Stock
26:11
trim ()
31:39
trim () Overview and Example
31:40
Homework Challenge
33:35
Homework Challenge
33:36
Web Application Development

36m 36s

Intro
0:00
Lesson Overview
0:16
Lesson Overview
0:17
Version 1.0 Changelog
0:31
catalog.php
0:32
Function Library: fileLIB.php
5:00
Version 1.1 Changelog
27:12
createItemDataArray() and createDeptDataArray()
27:13
outputSimpleItemLink() and outputSimpleDeptLink()
32:06
HTTP & the POST Method

34m 36s

Intro
0:00
Lesson Overview
0:10
Lesson Overview
0:11
HTTP Model
1:28
HTTP Model
1:29
Client-Server Model 2 Steps Process
2:00
HTTP Messages
2:23
HTTP Messages
2:24
HTTP Requests
3:09
HTTP Requests
3:10
Example: HTTP Request Header
3:17
Coding Example: HTTP Requests
4:29
HTTP Responses
7:15
HTTP Responses
7:16
Example: HTTP Response (Header & Body)
8:00
header()
11:47
header() Overview
11:48
Coding Example: header
14:02
Coding Example: header() Error
14:52
GET Method
18:55
GET Method
18:56
Coding Example: GET Method
20:13
POST Method
21:09
POST Method
21:10
Coding Example: Sample Form Using the POST Method
23:50
Accessing POST Data in PHP
26:38
Accessing POST Data in PHP
26:39
Coding Example: Accessing POST Data via $_POST
28:15
GET vs. POST
29:54
GET vs. POST
29:55
Example: Accessing POST Data via $_POST
31:20
Example: Accessing POST Data via $_POST
31:21
Homework Challenge
33:00
Homework Challenge
33:01
Web Application Development

14m 11s

Intro
0:00
Version 2.0 Changelog
0:09
Updating the Site's Current Form to Use the POST Method
0:10
Creating Admin Website
2:55
Dynamically Generated Department List
12:40
Writing to Files

17m 38s

Intro
0:00
Lesson Overview
0:09
Lesson Overview
0:10
Review of File I/O
0:40
Review of File I/O
0:41
fwrite()
1:38
fwrite()
1:39
fputs()
2:20
'r+'
2:31
Coding Example: fwrite()
2:59
fopen() Access Modes
8:41
'a' and 'a+'
8:42
'w' and 'w+'
11:34
Homework Challenge
14:47
Homework Challenge
14:48
Web Application Development

28m 14s

Intro
0:00
Lesson Overview
0:08
Lesson Overview
0:09
Updated Open File Functions
0:19
openItemsDataFile ( ), openDeptsDataFile ( ), and openLastAddedFile ( )
0:20
insertItem()
6:25
insertItem() Overview
6:26
Functions: createItemDataString ( ) and updateLastItemAdded ( )
7:07
addItemToDepartment ()
16:03
addItemToDepartment () Overview
16:04
Functions: createDeptDataString ( ) and updateDepartment ( )
17:55
HTTP POST: File Uploads

22m 51s

Intro
0:00
Lesson Overview
0:10
Lesson Overview
0:11
POST Data Encoding
1:13
POST Data Encoding
1:14
When Data is Included in the Body of the HTTP POST Request and is of the Content Type
1:31
When Uploading Files Using HTTP POST
3:17
Uploading Files
6:22
Uploading Files
6:23
If Encoding Type is Not Set to Multipart/Formdata
9:27
$_FILES Superglobal
10:59
$_FILES Superglobal
11:00
Structure of $_FILES
12:15
Structure of $_FILES: Name, Type, tmp_name, Error, and Size
12:16
Coding Example: $_FILES Superglobal
13:34
Moving an Uploaded File
15:39
Introduction to Moving an Uploaded File
15:40
move_uploaded_file ( ): Definition and Example
16:40
Homework Challenge
21:19
Homework Challenge
21:20
Web Application Development

15m 30s

Intro
0:00
Lesson Overview
0:09
Lesson Overview
0:10
Uploading Item Image File
1:22
Uploading Item Image File
1:23
Uploading Item Image File (cont.)
2:43
Altering insertItem ( )
2:44
Helpfer Functions: getFileExtension ( ) and moveUploadedFile ( )
4:30
isValidItem ( )
13:03
Introduction to Object-Oriented Programming

32m 44s

Intro
0:00
Lesson Overview
0:11
Lesson Overview
0:12
Object-Oriented Programming
0:56
Introduction to Object-Oriented Programming
0:57
Associative Arrays
5:27
Associative Arrays
5:28
Classes
7:27
Classes Overview
7:28
Defining Classes
8:24
Defining Classes
8:25
Declaring Class Properties
9:25
Coding Example: Classes
10:11
Objects
12:18
Objects Overview
12:19
Classes vs. Objects
12:49
Classes vs. Objects
12:50
Instantiating Classes
14:58
Instantiating Classes
14:59
Coding Example: Instantiate an Item Object
16:30
Object Properties
19:21
Access and Set an Object's Property
19:22
Coding Example: Set & Access the Properties of an Item Object
24:23
Homework Challenge
30:22
Homework Challenge
30:23
Web Application Development

17m 47s

Intro
0:00
Lesson Overview
0:11
Lesson Overview
0:12
Version 5.0 Overview
0:50
Version 5.0 Overview and Examples
0:51
Outputting Object Properties
12:38
Outputting Object Properties
12:39
Array Object Properties
14:18
Access and Set Array Value
14:19
Object Methods

39m 57s

Intro
0:00
Lesson Overview
0:12
Lesson Overview
0:13
Object Methods
0:52
Definition of Object Methods
0:53
Calling Object Methods
3:25
Calling Object Methods
3:26
Coding Example: Simple Object Methods
4:44
Object Methods and Return Values
7:02
$this Variable
10:06
$this Variable: Definition and Example
10:07
$this Variable (cont.)
15:51
$this Variable (cont.)
15:52
Getters & Setters
21:21
Getters & Setters Methods
21:22
Object Methods In Strings
25:46
Object Methods In Strings
25:47
Coding Example: Outputting Method Return Values in Strings
27:41
Using $this to Call Other Methods
28:49
Using $this to Call Other Methods
28:50
Homework Challenge
34:18
Homework Challenge: 1-3
34:19
Homework Challenge (cont.)
36:20
Homework Challenge: 4-6
36:21
Homework Challenge (cont.)
37:52
Homework Challenge: 7-10
37:53
Web Application Development

17m 30s

Intro
0:00
Lesson Overview
0:11
Lesson Overview
0:12
Version 6.0 Overview
0:40
Version 6.0 Changes & Examples
0:41
Item Methods
0:53
Item Class Definition: getImageFilename()
0:54
Coding Example: getImageFilename()
1:58
Department Methods
7:33
addItem(), removeItem(), and isItemInDept() Method
7:34
addItemtToDepartment() & buildDeptObject() Functions
11:46
A Default Value Needed to Be Set for $items
16:26
Object Constructors

22m 20s

Intro
0:00
Lesson Overview
0:11
Lesson Overview
0:12
Object Constructors
0:32
Introduction to Object Constructors
0:33
Coding Example: Object Constructors
1:43
Defining Constructors
3:26
Defining Constructors
3:27
Coding Example: Constructors with No Arguments
4:24
Constructor Arguments
8:09
Constructor Arguments
8:10
Coding Example: Constructor Arguments
9:49
Important Notes
16:13
Important Notes
16:14
Homework Challenge
17:35
Homework Challenge: 1 - 4
17:36
Homework Challenge (cont.)
19:13
Homework Challenge: 5 - 9
19:14
Homework Challenge (cont.)
21:40
Homework Challenge: 10 and 11
21:41
Web Application Development

16m 19s

Intro
0:00
Lesson Overview
0:11
Lesson Overview
0:12
Version 7.0 Overview
0:39
Version 7.0 Overview
0:40
Item Constructor
2:19
Item Constructor
2:20
Department Constructor
7:47
Department Constructor
7:48
Customer Constructor
12:23
Customer Constructor
12:24
More Magic Methods

53m 37s

Intro
0:00
Lesson Overview
0:13
Lesson Overview
0:14
Magic Methods
1:12
Magic Methods
1:13
Destructors
2:45
Destructors Overview
2:46
Coding Example: Calling Destructors
4:30
Coding Example: Object Destructor
9:19
_to String ()
16:12
_to String () Overview
16:13
Coding Example: _to String () Magic Method
18:10
Access Modifiers
21:23
Introduction to Access Modifiers
21:24
Access Modifiers: Public
21:39
Access Modifiers: Private
22:18
Access Modifiers: Protected
22:41
Object Properties and Methods
23:06
Coding Example: Public Access Modifiers
26:48
Coding Example: Private Access Modifiers
28:30
_get()
31:37
_get() Overview
31:38
Coding Example: _get () Magic Method
33:30
_set ()
36:23
_set () & the Magic Method
36:24
Using Getters & Setters
44:37
Coding Example: Using Getters & Setters
44:38
Homework Challenge
50:33
Homework Challenge: 1 - 6
50:34
Homework Challenge (cont.)
51:41
Homework Challenge: 7 - 12
51:42
Web Application Development

31m 12s

Intro
0:00
Version 8.0 Review
0:12
Version 8.0 Review
0:13
private Object Properties
1:15
private Object Properties
1:16
Coding Example
2:54
_toString() Methods
6:51
_toString() Methods
6:52
Coding Example
8:09
DataFile Class
13:27
DataFile Class & I/O Operations
13:28
Using DataFile Class: Instantiate a DataFile, DataFile open () Method, and DataFile close () Method
18:09
Homework Challenge
29:35
Homework Challenge
29:36
Classes vs. Objects

37m 49s

Intro
0:00
Lesson Overview
0:09
Lesson Overview
0:10
Class vs. Object Review
0:58
Class vs. Object Review
0:59
Static Properties
2:42
Static Properties
2:43
Scope Resolution Operator (::)
5:26
Scope Resolution Operator (::) & Static Property
5:27
Coding Example: Accessing a Static Property Using the Scope Resolution Operator
7:57
Coding Example: Accessing a Static Property in a Class Definition
10:33
Coding Example: Using a Static Property as an Instance Counter
14:18
Static Methods
18:51
Static Methods Overview
18:52
Coding Example: Static Methods
21:39
Classes vs. Objects
26:28
Classes vs. Objects: Diagrams
26:29
Class Constants
30:24
Class Constants Definition
30:25
Coding Example: Using Class Constants
31:55
Homework Challenge
34:01
Homework Challenge: 1 - 5
34:02
Homework Challenge (cont.)
35:25
Homework Challenge: 6 - 10
35:26
Web Application Development

21m 7s

Intro
0:00
Version 9.0 Overview
0:12
Version 9.0 Changes & Examples
0:13
Output Class
1:08
Output Class & Static Methods
1:09
outputHtmlSelector ( ) Method
2:43
outputItemSelector ( ) Method
4:53
outputDeptSelector ( ) Method
6:42
buildObject() Methods
11:54
buildObject() Methods & Examples
11:55
Exceptions

22m 43s

Intro
0:00
Lesson Overview
0:12
Lesson Overview
0:13
try Blocks
0:44
try Blocks Definition
0:45
catch Blocks
2:03
catch Blocks: Definition and Syntax
2:04
Coding Example: try/catch Blocks
3:48
When an Exception Object is Caught & Error Handling
5:59
Exception Class
7:02
Exception Class
7:03
Coding Example: Using try/catch to Catch an Exception Thrown from a Method
8:04
Re-Throwing Exceptions
16:39
Re-Throwing Exceptions
16:40
Coding Example: Re-throwing an Exception
17:21
Homework Challenge
20:40
Homework Challenge: 1 - 5
20:41
Homework Challenge (cont.)
22:17
Homework Challenge: 6
22:18
Web Application Development

22m

Intro
0:00
Version 10.0 Overview
0:11
Updating addItem.php, addItemToDept.php, and error.php
0:12
Updating DataFile Class: open ( ) Method & try/catch blocks
7:45
Version 10.1 Overview
13:37
Version 10.1 Changes & Examples
13:38
Updating DataFile Class: close ( ) Method & logWarning ( )
20:17
Cookies

39m 47s

Intro
0:00
Lesson Overview
0:09
Lesson Overview
0:10
What is a Cookie?
1:15
Definition of Cookie
1:16
HTTP - A Stateless Protocol
2:17
HTTP - A Stateless Protocol
2:18
Purpose of Cookies
4:10
Set - Cookie Header
6:21
Introduction to Set - Cookie Header
6:22
Coding Example: Set - Cookie Header
7:38
Cookie Header
9:45
Introduction to Cookie Header
9:46
Example: Cookie Header
10:27
setcookie()
12:33
Introduction to setcookie()
12:34
Example: Using setcookie
13:54
Cookies Attributes
15:48
Cookies Attributes Overview
15:49
Common Cookies Attributes: Domain
16:24
Common Cookies Attributes: Path
17:04
Common Cookies Attributes: Expires
19:34
Coding Example: Setting Cookie Attributes
20:58
$_COOKIE Superglobal
27:03
$_COOKIE Superglobal
27:04
Coding Example: Accessing Cookies from PHP Using $_COOKIE
29:45
Security Implications
34:44
Security Implications
34:45
Homework Challenge
36:10
Homework Challenge: 1 - 4
36:11
Homework Challenge (cont.)
37:47
Homework Challenge: 5 - 7
37:48
Web Application Development

39m 11s

Intro
0:00
Lesson Overview
0:17
Lesson Overview
0:18
instanceof Operator
1:34
instanceof Operator
1:35
Objects with Object Properties
2:53
Objects with Object Properties
2:54
Version 11.0 Overview
6:41
Version 11.0 Changes: Cart and CartItem Classes
6:42
Version 11.0 Coding Example
10:40
Version 11.1 Overview
31:03
Version 11.1 Changes & Examples
31:04
Introduction to Sessions

46m 8s

Intro
0:00
Lesson Overview
0:08
Lesson Overview
0:09
What is a Session?
0:53
What is a Session?
0:54
Client-Side Sessions
3:51
Client-Side Sessions
3:52
Server-Side Sessions
5:04
Server-Side Sessions
5:05
Session IDs
6:26
Session IDs
6:27
The SID is Used by the Server to:
7:08
A Client Passes an SID to the Server with Each HTTP Request Via:
8:15
Sessions in PHP
12:19
Sessions in PHP
12:20
session_start()
14:40
session_start() Overview
14:41
Coding Example: Using session_start () to Continue a Session
18:03
$_SESSION Superglobal
22:06
$_SESSION Superglobal Overview
22:07
Coding Example: Setting a Session Variable via $_SESSION
25:22
Accessing Session Data
28:08
Accessing Session Data
28:09
Deleting Session Data
31:43
Deleting Session Data
31:44
Coding Example: Deleting Session Data
32:16
Configuring PHP Sessions
36:14
session.cookie_lifetime, session.cookie_domain, and session.cookie_path
36:15
Coding Example: Configuring PHP Sessions
37:28
Configuring PHP Sessions (cont.)
40:56
session.use_cookies, session.use_trans_sid, and session.use_only_cookies
40:57
Coding Example: Configuring PHP Sessions
42:25
Homework Challenge
42:55
Homework Challenge: 1 - 5
42:56
Homework Challenge (cont.)
44:10
Homework Challenge: 6 - 10
44:11
Homework Challenge (cont.)
45:27
Homework Challenge: 11 - 13
45:28
Web Application Development

11m

Intro
0:00
Version 12.0 Overview
0:12
Version 12.0 Changes Part I: Updating 'viewCart.php' & 'checkout.php'
0:13
Version 12.0 Changes Part II
5:03
Destroying Sessions

29m 59s

Intro
0:00
Lesson Overview
0:12
Lesson Overview
0:13
Destroying Sessions
1:02
Destroying Sessions
1:03
session_destroy()
2:10
session_destroy() Overview
2:11
Coding Example: Setting a Session Variable and Destroying a Session
3:18
Deleting Session Cookies
8:38
Deleting Session Cookies
8:39
Coding example: Deleting Session Cookies
9:17
Review of Steps
21:07
Review of Steps
21:08
Garbage Collection
21:50
Garbage Collection Overview
21:51
Coding Example: Garbage Collection
24:30
Homework Challenge
26:28
Homework Challenge: 1 - 4
26:29
Homework Challenge (cont.)
28:16
Homework Challenge: 5 - 9
28:17
Web Application Development

19m 14s

Intro
0:00
Lesson Overview
0:13
Lesson Overview
0:14
_autoload()
0:46
Introduction to _autoload ()
0:47
Version 13.0 Overview
4:06
_autoload () and SessionManager Class
4:07
Updating 'viewCart.php' & 'checkout.php'
11:16
Introduction to Databases

21m 24s

Intro
0:00
Lesson Overview
0:16
Lesson Overview
0:17
Flat Files
1:09
Flat Files: Definition and Example
1:10
Problems Associated with Using Flat Files as a Database
2:15
Relational Databases
3:29
Relational Databases
3:30
Relational Database Management System (RDBMS)
3:50
Tables
7:43
Tables
7:44
Columns
9:24
Columns
9:25
What is SQL?
10:45
Introduction to Structured Query Language
10:46
Standard Data Types of SQL
12:23
Primary Keys
13:19
Primary Keys
13:20
Primary Key Examples
16:36
Primary Key Examples
16:37
Introduction to MySQL

28m 11s

Intro
0:00
Lesson Overview
0:11
Lesson Overview
0:12
What is MySQL?
1:33
MySQL
1:34
MySQL Monitor
5:16
MySQL Monitor Overview
5:17
XAMPP & MySQL
5:58
In the MySQL Command: hostname, username and -p
7:05
Connecting to a MySQl Server
9:52
Connecting to a MySQl Server
9:53
Using MySQl Monitor
13:48
Using MySQl Monitor
13:49
GRANT Command
15:10
GRANT SQL Command
15:11
Privileges & Objects
18:18
Username, Hostname, and Password
19:37
Specifying Objects
20:05
Creating a PHP User Account
21:41
Creating a PHP User Account Overview
21:42
Meaning of all & *.*
23:35
Example: Creating a PHP User Account
23:48
Required Homework
26:27
Required Homework: 1 - 5
26:28
Required Homework (cont.)
27:28
Required Homework: 6
27:29
Creating Databases & Tables

23m 1s

Intro
0:00
Lesson Overview
0:08
Lesson Overview
0:09
Creating a Database
1:40
Introduction to Creating a Database
1:41
Example: Creating a Database
3:33
CREATE TABLE Command
6:43
CREATE TABLE Command
6:44
Data Types
7:39
Numeric Data Types: INT, INT UNSIGNED, and DECIMAL
7:40
String Data Types: CHAR (M), VARCHAR (M), and TEXT
9:23
Colum Attributes
11:00
PRIMARY KEY Attribute
11:01
AUTO INCREMENT Attribute
11:35
Items Table
13:07
Items Table
13:08
Useful Commands
17:04
SHOW TABLES Command & DESCRIBE Utility Statement
17:05
Example: Creating an Items Table
17:58
Required Homework
20:51
Required Homework: 1 - 6
20:52
Required Homework (cont.)
21:55
Required Homework: 7 - 9
21:56
SQL Command: INSERT

27m 11s

Intro
0:00
Lesson Overview
0:11
Lesson Overview
0:12
INSERT Command
1:20
SQL INSERT Command
1:21
Specifying Values
3:16
Columns with Numeric Data Types
3:17
Columns with String Data Types
3:34
Columns with AUTO INCREMENT
4:07
Inserting Items
5:21
Inserting Items
5:22
Example: Inserting Items
7:03
SQL Text Files
14:04
SQL Monitor Client
14:05
MySQL Monitor & the < Operator
15:15
Example
17:35
Required Homework
22:32
Required Homework: 1 - 6
22:33
Required Homework (cont.)
24:19
Required Homework: 7 - 10
24:20
SQL Command: SELECT

24m 57s

Intro
0:00
Lesson Overview
0:10
Lesson Overview
0:11
SELECT Command
0:42
SELECT Command: Definition and Syntax
0:43
Results of a SELECT Query
1:53
To Specify the Retrieval of All Columns from a Table
3:06
Example: SELECT Command
4:18
WHERE Clause
7:11
WHERE Clause
7:12
A SELECT Query with WHERE Clause has the Following Syntax
8:10
A Where Condition can Contain both Logical and Comparison Operators
9:44
Literal Values
13:24
Literal Values
13:25
Example: Literal Values
14:18
ORDER BY Clause
15:31
ORDER BY Clause & SELECT Query
15:32
Ascending and Descending Order
17:30
An ORDER BY Clause can Specify Multiple Columns to Order the Rows by
18:01
Example: SELECT Query with WHERE and ORDER BY Clause
20:32
Homework Challenge
21:59
Homework Challenge
22:00
Homework Challenge (cont.)
23:54
Homework Challenge
23:55
Using PHP from MySQL

32m 43s

Intro
0:00
Lesson Overview
0:10
Lesson Overview
0:11
PHP/MySQL Architecture
0:56
PHP/MySQL Architecture
0:57
MySQLi Extension
3:05
MySQLi Extension
3:06
Steps to Using MySQL from PHP
5:53
Four Steps to Using MySQL from PHP
5:54
Step # 3
7:18
Connecting to a Database
8:12
To Connect to a MySQL Database
8:13
To Test for Connection Errors
10:20
Running an SQL Statement
11:54
Running an SQL Statement
11:55
Example
13:30
Processing the Results
15:58
For non-SELECT Queries
15:59
MySQLi Object Property: affected_rows
16:19
For INSERT Statements on Tables with an AUTO_INCREMENT Column
18:07
Closing the Connection
19:45
Closing the Connection
19:46
Coding Example
21:24
Coding Example: Using MySQL from PHP
21:25
Homework Challenge
29:00
Homework Challenge: 1 - 4
29:01
Homework Challenge (cont.)
31:10
Homework Challenge: 5 - 9
31:11
Web Application Development

30m 30s

Intro
0:00
Lesson Overview
0:14
Lesson Overview
0:15
sprintf()
1:28
sprintf() Overview
1:29
Example
1:56
INSERT Statements with sprintf()
5:15
INSERT Statements with sprintf()
5:16
Version 14.0 Overview
10:00
Version 14.0 Changes & Examples Part 1
10:01
Version 14.0 Changes & Examples Part 2
21:58
SELECT QUERIES from PHP

24m 22s

Intro
0:00
Lesson Overview
0:14
Lesson Overview
0:15
SELECT Queries
1:21
SELECT Queries
1:22
MySQLi_Result Class
3:17
MySQLi_Result Class: num_rows
3:18
MySQLi_Result Class: fetch_assoc ( ) and fetch_object ( )
3:59
fetch_assoc()
5:17
fetch_assoc() Overview
5:18
Coding Example: Using fetch_assoc()
9:00
stdClass Objects
12:46
stdClass Objects
12:47
fetch_object()
14:19
fetch_object() Overview
14:20
Coding Example: Using fetch_object()
16:45
Freeing Result Set Memory
18:42
Freeing Result Set Memory
18:43
Coding Example: Using free ( )
20:01
Homework Challenge
22:07
Homework Challenge: 1 - 5
22:08
Web Application Development

17m 6s

Intro
0:00
Version 15.0 Overview
0:13
Version 15.0 Changes & Examples Part 1
0:14
Version 15.0 Changes & Examples Part 2
8:43
Linking Tables

24m 32s

Intro
0:00
Lesson Overview
0:16
Lesson Overview
0:17
A Departments Table
1:08
Departments Table
1:09
How Could We Model This in a Table: Option 1
1:26
How Could We Model This in a Table: Option 2
3:12
Database Normalization
4:37
Database Normalization
4:38
Foreign Keys
8:47
Foreign Keys
8:48
Linking Table
11:26
Linking Table
11:27
Multiple Column Primary Keys
15:30
Multiple Column Primary Keys
15:31
Example
18:51
Required Homework
22:34
Required Homework: 1 - 3
22:35
Required Homework (cont.)
24:10
Required Homework: 4
24:11
Web Application Development

24m

Intro
0:00
Version 16.0 Overview
0:11
Version 16.0 Changes
0:12
Version 16.0 Coding Examples
4:12
Version 16.0 (cont.)
18:30
Version 16.0 Changes & Examples Part 2
18:31
SQL Command: DELETE

10m 33s

Intro
0:00
Lesson Overview
0:12
Lesson Overview
0:13
DELETE Statement
0:30
DELETE Statement & Its WHERE Clause
0:31
Delete All Rows From a Table
2:57
Using DELETE from PHP
4:04
Using DELETE from PHP Overview
4:05
Coding Example: Using DELETE from PHP
6:37
Homework Challenge
8:53
Homework Challenge: 1 - 4
8:54
Web Application Development

16m 2s

Intro
0:00
Version 17.0 Overview
0:11
Version 17.0 Changes
0:12
Version 17.0 Coding Example
2:51
Transactions
12:50
Database Transaction
12:51
Steps in Using Transactions
14:00
SQL Command: UPDATE

32m 14s

Intro
0:00
Lesson Overview
0:10
Lesson Overview
0:11
UPDATE Statement
0:54
UPDATE Statement & Its WHERE Clause
0:55
Example: UPDATE Statement
3:32
Using UPDATE from PHP
5:21
Using UPDATE from PHP Overview
5:22
Coding Example: Using UPDATE from PHP
6:53
htmlspecialchars()
9:04
htmlspecialchars()
9:05
Encoding Special Characters
9:23
Coding Example: htmlspecialchars()
13:41
addslashes()
20:40
addslashes()
20:41
Coding Example: addslashes()
22:52
Homework Challenge
27:55
Homework Challenge: 1 - 2
27:56
Homework Challenge (cont.)
30:14
Homework Challenge: 3 - 5
30:15
Homework Challenge (cont.)
30:41
Homework Challenge: 6 - 9
30:42
Web Application Development

12m 33s

Intro
0:00
Version 18.0 Overview
0:12
Version 18.0 Changes
0:13
Version 18.0 Coding Example
1:24
SQL: Joins

42m 16s

Intro
0:00
Lesson Overview
0:11
Lesson Overview
0:12
DATETIME Data Type
1:52
DATETIME Data Type
1:53
Modeling Orders
5:12
Modeling Orders
5:13
Customers Table
10:46
Customers Table
10:47
Example
16:18
Example: Order Table in MySQL
16:19
Joins
22:26
Joins Overview
22:27
Inner Join
23:09
Example: Joins
24:11
Join Conditions
28:14
Join Conditions
28:15
Example
28:32
Join Queries
30:00
Example: Join Conditions in MySQL
30:36
Implicit Joins
34:42
Implicit Joins
34:43
Example: Implicit Joins
35:57
Aliases
37:28
Introduction to Aliases
37:29
Example: Aliases
38:55
Required Homework
40:32
Problem 1
40:33
Problem 2
41:01
Problem 3
41:17
Web Application Development

29m 34s

Intro
0:00
Lesson Overview
0:12
Lesson Overview
0:13
Inserting Orders
0:44
Inserting Orders
0:45
Version 19.0 Overview
3:45
Version 19.0 Changes & Example Part 1
3:46
Version 19.0 Changes & Example Part 2
13:18
Version 19.1 Overview
21:10
Version 19.1 Changes
21:11
Version 19.1 Coding Example
22:18
User Authentication

26m 13s

Intro
0:00
Lesson Overview
0:10
Lesson Overview
0:11
User Authentication
1:31
User Authentication
1:32
Authentication with Sessions
2:34
Authentication with Sessions
2:35
Four Steps in Authentication via Sessions
2:50
Using Sessions to Restrict Access
3:58
Using Sessions to Restrict Access
3:59
Coding Example: Restricted Access Area
4:47
Authentication Methods
5:54
Authentication Methods Overview
5:55
Coding Example: Authentication Methods
7:31
Logging Out
9:57
Logging Out
9:58
Coding Example: Log Out
10:47
Users Table
13:50
Users Table
13:51
Example: Creating a Users Table
15:08
Password Hashing
17:30
Password Hashing
17:31
PHP and MySQL Built-in Hash Functions: sha1 ( ) and md5 ( )
18:43
Coding Example: Password Hashing
19:27
Required Homework
24:41
Required Homework: 1 - 4
24:42
Web Application Development

14m 36s

Intro
0:00
Version 20.0 Overview
0:13
Version 20.0 Changes & Examples Part 1
0:14
Version 20.0 Changes & Examples Part 2
5:05
Version 20.0 Changes & Examples Part 3
7:29
Version 20.0 (cont.)
8:31
Version 20.0 Changes & Examples Part 4
8:32
Loading...
This is a quick preview of the lesson. For full access, please Log In or Sign up.
For more information, please see full course syllabus of Advanced PHP
Bookmark & Share Embed

Share this knowledge with your friends!

Copy & Paste this embed code into your website’s HTML

Please ensure that your website editor is in text mode when you paste the code.
(In Wordpress, the mode button is on the top right corner.)
  ×
  • - Allow users to view the embedded video in full-size.
Since this lesson is not free, only the preview will appear on your website.
  • Discussion

  • Study Guides

  • Download Lecture Slides

  • Table of Contents

  • Transcription

  • Related Services

Start Learning Now

Our free lessons will get you started (Adobe Flash® required).
Get immediate access to our entire library.

Sign up for Educator.com

Membership Overview

  • Unlimited access to our entire library of courses.
  • Search and jump to exactly what you want to learn.
  • *Ask questions and get answers from the community and our teachers!
  • Practice questions with step-by-step solutions.
  • Download lesson files for programming and software training practice.
  • Track your course viewing progress.
  • Download lecture slides for taking notes.
  • Learn at your own pace... anytime, anywhere!

Cookies

  • A cookie is a piece of information set by an HTTP server and sent to a web client within an HTTP response for storage by the client.
  • Cookies are associated with a particular website, and each time a client requests a page from a website for which it has received & stored a cookie, it includes the corresponding cookie’s data within its HTTP request.
  • HTTP is a stateless protocol meaning that each HTTP transaction between a client & server has no ‘memory’ of any previous transactions.
  • The purpose of cookies is to allow state, or knowledge of previous events, to be introduced into HTTP transactions.
  • The way a web server sends a cookie to a client is via the Set-Cookie header of an HTTP response:
    HTTP/1.1 200 OK
    Date: Wed, 3 Aug 2011 23:59:59 GMT
    Content-Type: text/html
    Set-Cookie: cookie=cookieValue
    Host: www.educator.com
  • Cookie data is in the form of name-value pairs.
  • Each time a client requests a page from a website for which it has a cookie, it sends the cookie’s information back to the server using the Cookie HTTP request header:
    GET /index.html HTTP/1.1
    Host: www.educator.com
    Cookie: cookie=cookieValue
  • setcookie() is a built-in PHP function that allows a script to send a cookie to a web client. It must be called before any of a script’s output is generated.
  • Cookies have several attributes that can be set which determine whether a client sends a particular cookie in its request for a page from a particular website:
    • domain – specifies the domain which a cookie applies to
    • path – specifies a particular path within a domain that the cookie applies to
    • expires – the data & time when a cookie is no longer valid
  • $_COOKIE is a superglobal associative array containing any cookie name/value pairs sent by a client in its request for a PHP script.
  • Additional Resources:

Cookies

Lecture Slides are screen-captured images of important points in the lecture. Students can download and print out these lecture slide images to do practice problems as well as take notes while watching the lecture.

  • Intro 0:00
  • Lesson Overview 0:09
    • Lesson Overview
  • What is a Cookie? 1:15
    • Definition of Cookie
  • HTTP - A Stateless Protocol 2:17
    • HTTP - A Stateless Protocol
    • Purpose of Cookies
  • Set - Cookie Header 6:21
    • Introduction to Set - Cookie Header
    • Coding Example: Set - Cookie Header
  • Cookie Header 9:45
    • Introduction to Cookie Header
    • Example: Cookie Header
  • setcookie() 12:33
    • Introduction to setcookie()
    • Example: Using setcookie
  • Cookies Attributes 15:48
    • Cookies Attributes Overview
    • Common Cookies Attributes: Domain
    • Common Cookies Attributes: Path
    • Common Cookies Attributes: Expires
    • Coding Example: Setting Cookie Attributes
  • $_COOKIE Superglobal 27:03
    • $_COOKIE Superglobal
    • Coding Example: Accessing Cookies from PHP Using $_COOKIE
  • Security Implications 34:44
    • Security Implications
  • Homework Challenge 36:10
    • Homework Challenge: 1 - 4
  • Homework Challenge (cont.) 37:47
    • Homework Challenge: 5 - 7

Transcription: Cookies

Hello again, and welcome back to Educator.com's Advanced PHP with MySQL course.0000

In today's lesson, we are going to be covering the topic of cookies.0005

Specifically, we are going to be going over what a cookie is.0010

It may be something you are familiar with, or maybe it's a term that you have heard thrown around in the web development world.0014

We are going to talk a little bit about HTTP and how it is known as a stateless protocol, and how cookies can change that.0021

We are going to talk about a couple of HTTP headers.0031

We are going to talk about the Set-Cookie header, which is an HTTP response header.0035

And we are going to talk about the Cookie header, which is an HTTP request header.0039

We are also going to talk about a function built into PHP for what is known as setting cookies.0044

We are going to talk about different attributes that cookies can have.0055

We are going to talk about (after we learn about what cookies are) how to access cookie data within your file using the COOKIE superglobal variable.0058

And then, we are going to briefly talk about security implications as they relate to cookies.0069

What exactly is a cookie? Well, it is a piece of information or data that is set by an HTTP server,0077

and it gets sent back to a web client within an HTTP response for storage by the client.0086

For example (and it is done in name/value pairs), a server might want to set a cookie that says the user ID equals 1001.0092

Well, it will create that cookie, and then what it will do is: using an HTTP response header,0103

it will send that cookie back to the client, and the client is going to store that on its local machine.0109

Cookies are associated with a particular website, and what happens is: every time a client goes to request a page from a particular website,0115

it checks to see if it has a cookie stored that is associated with that website.0123

If it does, it goes ahead and, in its HTTP request, sends the information contained in that cookie to the web server.0128

Before we go into the details about how the cookies work, I just want to talk quickly about HTTP and what is known as a stateless protocol.0139

What that means is that, for each HTTP request and response, no state is preserved,0148

which means there is no memory between one request and another.0155

For example, when you go to a website, and you request a page...0158

for example, let's say this was your HTTP request, and then your server would respond with something like this,0162

with HTML as part of the body; when you do that request--let's say you do that multiple times--0171

each time you do that, the HTTP server has no idea that those are linked from the same computer.0179

It can know that it is from the same computer, but it doesn't store any information about each of the different requests.0187

For example, let's say that you were trying to implement a shopping cart, and you add an item to a cart.0195

Well, maybe what you would do is send a GET request that provides some information.0201

Maybe you have a query string that says to add item 1001.0206

And the HTTP server is going to go ahead and process that GET request.0212

It is going to add an item to a shopping cart, and then it is going to send the response back to you that maybe shows you your shopping cart.0216

However, the next time you go to add another item to the shopping cart, how does the server know that that item 1001 is already part of your cart?0224

HTTP, in and of itself, doesn't have a mechanism for that.0234

It doesn't store this information anywhere--for example, on the server.0237

Well, cookies are a way to enable some memory--some state--to occur between different HTTP transactions.0242

For example, cookies are to provide knowledge of previous events.0253

What it does is connects one HTTP request to another.0260

And this can be used for things...for example, it can identify a particular client.0263

Maybe, when you go to a website, and you log in, the HTTP server, when it sends you back your welcome page,0267

will send you a cookie, which is a piece of information.0276

And it is going to say, "Your user ID is this."0279

Then, every time you go and visit a page on that website, you are going to send that cookie back to the server, and it is going to identify you.0282

And then, the server can say, "Hey, this is a person I have dealt with before," and maybe it can alter its page content based on that.0289

Another thing that you can do is contain information in a cookie, such as the contents of a shopping cart.0296

For example, let's say you send an HTTP request; you click on a button that says "Add Item to Shopping Cart"0303

that sends a GET request with the item ID to the server.0310

Well, the HTTP server can then create a cookie that says, "OK, maybe the cookie's name will be 1001, which is the ID of the item."0313

And maybe its value is going to set to 1, which means you added one of those items to your shopping cart.0324

Well, what the server can do is create a cookie that contains this data.0329

It can send that back in its response to the client.0333

And then, when the client goes to another page in that website (for example, clicks on the Add to Cart button0337

for another item on that website), well, because, as we had mentioned, any time you have cookies stored for a particular website,0343

when you go to a page on that website, you automatically send it; when you go to add that second item to the cart,0351

you are going to send this cookie back to the server.0357

So, now the server knows that you have already had one item in your cart.0362

And it can add this new item to the cart; it can update the cookie to reflect that.0366

But now that it knows this information about you having this item already in the cart,0371

when it sends you a page back to view your cart, it can show both of the items, because it has knowledge of that.0375

The way that setting and getting cookies works is through some headers.0383

In particular, there is an HTTP response header called Set-Cookie.0391

And what that does is: that is a way for the web server to send a cookie (which is a name/value pair) back to the client that has information.0397

An example of an HTTP response header that sends a cookie to the client is this right here.0406

We have our typical HTTP response information, and down here you have the body of the response.0413

Maybe it is the HTML to represent a shopping cart.0419

But what you can see is that it has a new header here called Set-Cookie.0424

And what you do is provide the name of the cookie, and then the value, to it.0427

The server is going to say, "OK, I am sending to this client a cookie that has the name cookie, and it is going to have the value equal to cookie value."0433

And what that is going to do is: it is going to get sent to the client.0446

When the client receives this response, it is going to store this cookie and associate it with whatever website this response came from, right here.0448

If we go and take a look at an example, I'm going to have Firebug open, and in the Firebug, we select the net tab,0459

so that we can see the HTTP header information that is going on.0469

For example, if we click on the script httpSetCookieHeader.php, this is a script that,0474

when you go to it, creates a cookie and sends it back to the client as part of the script.0481

And we are going to talk a little bit more about how to do that in a few slides.0489

But for now, that is what is going on behind the scenes.0491

It just shows a little bit of output, of what the HTTP response when you go to this page might look like.0495

It is going to show some of your typical HTTP response headers.0502

And you can see, also, that it has a Set-Cookie header, which is setting a cookie named example1Cookie, and it has the value equal to value.0504

Now, if we go down here and we look at our Firebug output, and we see the HTTP response header that we get back0516

from the server when we requested this page, we can see, in the raw header,0526

that there is this Set-Cookie header; and it has, as we expected, the name of the cookie and the value.0530

And so, that is how the server sends the cookie back to the user; it uses this Set-Cookie header.0538

Now, we have another script called cookieHeader, which shows the client side of things.0547

And what this shows is: when the client requests another page from this particular website0553

(in this case, it is a localhost website), what it is going to do is say, "Get me the page httpCookieHeader.php."0559

It is going to have the Host header, and then it is also going to have a new header that we are going to talk about, called Cookie,0571

which is the way of the client sending the cookie back to the user.0580

For example, Set-Cookie is used by the server to send a cookie to the client.0587

Well, any time a client requests a page (for example, if a client has a cookie associated with the host www.educator.com)0591

from that website (here, index.html), it is going to send any cookies it has that are associated with that site back to the server.0601

So, if a cookie had previously been set and stored from Educator.com on this client,0612

the client uses what is known as a Cookie header in its HTTP request, and what it does is provides the name of the cookie and its value.0619

If we go back and look at our script here again, we can see that this is what it looks like when we request a page after we have already received a cookie.0628

In our previous script, setCookieHeader, the HTTP server on our localhost sent us a cookie called example1cookie, and it has the value value.0638

When we go to request another page on this same website (in this case, it's called httpCookieHeader),0652

what we can see is: if we look down at the request information, if we look at the raw HTTP request, there is a Cookie header.0660

And it says example1cookie=value; so what it has done is: because we have gone to that same website,0672

before it sends this request off to the website, our web browser goes and looks to see if it has any cookies stored for localhost.0678

And then, if it does, it goes ahead and sends those cookies along.0685

Well, in this case, we have a cookie called example1cookie, and it sends the value back to the server called value.0687

And one of the things you can do in Firefox is see the different cookies that you have stored.0695

If you go to Firefox menu, and you go to Options, and then you select the Privacy tab, there is a link on the default page here.0700

You can click Remove Individual Cookies, and what this does is shows all of the cookies that you have from all the different sites.0709

So, here you can see all of the different sites that we have visited, that we have received cookies from.0715

That means that, when we have gone to these websites, we have received an HTTP response from the server that had the Set-Cookie header in it.0720

For example, if we search for our localhost, we can see that we have one cookie.0729

The name of the cookie is example1cookie; its content, or the value of the cookie, is value, which is what we had set, to which the cookie's value was set.0735

And so, this shows you information about the particular cookie.0745

How do you set a cookie? Let's say you are writing a script, and you want to send a cookie back to the server.0755

For example, you want to maybe provide a user ID; you can say userID=1001.0762

So, every time the user comes back to the site, it will automatically supply that information to the server via the Cookie header in the HTTP request.0768

Well, PHP provides a built-in function called setCookie.0778

And what that does is allows your script to send a cookie back to a client.0782

One important thing to note is that, because setCookie involves setting an HTTP header (in particular, the Set-Cookie header),0787

you have to call it before any of the script's output is generated.0795

We had seen this before in examples when we used the header method--for example, to redirect the user to another web page.0799

And when we use that before, we have to do that before we generate any output, or else we are going to get an error.0817

So, it is important that you call setCookie before you generate any output for your script.0821

The first two parameters of the setCookie method are the name of the cookie and the value that you want to assign to the cookie.0827

If we go and take a look at a script called setCookie, we can see that it is a PHP script.0834

And before any of our output is generated, we have a PHP section up here that calls the setCookie method.0840

And what it does is: it is setting a cookie called example2cookie, and we are setting the value of that cookie just equal to the string 'value.'0846

So, when the user requests this page, when PHP generates the HTTP response to send back to the client,0855

it is going to generate a Set-Cookie header that is going to contain this cookie.0864

It is going to say "example2cookie=value."0869

And then, it is going to return whatever HTML the page contains, that you are sending back to the user.0872

In this case, it is just some output that shows an example of the headers that you are receiving from the web server.0880

If we go and look at our example2, and we go to setCookie.php, we can see that this is an example of what our response header looks like.0891

So, when we go to this page, we are getting a response that has the header Set-Cookie that says example2cookie=value.0904

If we go down here in our Firebug and we look at our response, and we view the raw HTTP response header,0911

we can see there is, in fact, the Set-Cookie header that shows that a cookie name example2cookie0918

has been sent to the client, and it has the value 'value.'0927

If we go ahead and look again at our cookies for localhost, we should now be able to see that we have two cookies.0931

We have the example1cookie from our previous example and example2cookie from this example.0940

One of the features of cookies is that they have several attributes that you can set.0949

And these attributes determine whether or not a client is going0953

to send a particular cookie to a web server when it is making an HTTP request for a page.0957

We have already implicitly said that, when a client receives a cookie from a particular website, it stores that cookie.0965

And then, any time it visits a page on that website, what it does is goes ahead and sends that cookie, along with any request that it has.0975

Well, you can explicitly set that; there is a domain attribute that you can set for a cookie that explicitly specifies the domain for which a cookie applies.0985

So, in our case, for example, it has been localhost.0995

We were not explicitly providing that; and what a specification does is: when a browser receives a cookie0997

that doesn't have a domain attribute set, it uses the domain of whatever website it received it from.1003

In our case, when we looked at those cookies in our Firefox web browser's Cookies dialog box,1011

and we searched for localhost, we could see that the domain1019

associated with those cookies is localhost, because that is where they came from.1022

There is also a path attribute that you can associate with a cookie.1026

And that says, "Within a particular domain, the client is only going to send a cookie back to the browser if it requests a page within a particular path."1030

For example, if we look in a little more detail at the cookie we received on our example number 2,1042

we can see information down here that Firefox provides.1056

It the name of the cookie, example2cookie; it has its content, which is value.1059

We can see it has the host, which is the same thing as domain, and it sets localhost.1063

And we didn't explicitly set that, but that is implicitly set.1067

Additionally, if path is not explicitly set, then it the path of the cookie gets set to whatever path the script that you requested was.1070

In this particular case, the path for our script, setCookie.php, was in the example-2 directory1087

of the lecture_23 directory of the lecture_examples directory in the advanced_php directory.1098

Any time we use localhost, and we access or request a script within this example-2 directory, this example2cookie is going to get passed on.1104

For example, if we refresh this page, because we are still in this example-2 directory,1117

we are also going to have a request header now that is going to pass that cookie along.1123

Not only does the cookie get set, but also, if we look at our request header, we can see that this example2cookie has been passed along.1129

Now, that is associated with this particular path.1136

Let's say we were to go up one directory, out of the example-2 directory, and we were just to refresh the page,1139

which is just requesting the directory listing for lecture_23.1149

Well, the lecture_23 directory is not lecture_23/example-2, so our web browser should not send that example2cookie in its request for this directory listing.1153

If we go and look down at our request headers, we can see that there is no Cookie header that is sending that along.1166

The other common cookie attribute that is set is called expires.1177

And that is the date and time when a cookie is no longer valid.1181

So, in addition to when a client is requesting a web page, it checks to see if it has any cookies for that particular domain;1184

it checks the path that it is requesting the script from.1192

If a cookie in that domain matches that path, it will send the cookie.1195

And then also, it checks to see if the cookie has not expired.1199

If the cookie has not expired (meaning that the expiration date and time of the cookie has not yet occurred), then it will set the cookie.1201

If the cookie has expired, it doesn't set that.1208

These are three attributes that allow you to determine that, once the server sets a cookie on a client,1211

when and under what conditions that client will send that cookie back to the server.1222

The setCookie method can take values for all three of these attributes: domain, path, and expires.1228

One thing to note is that the expires parameter takes a UNIX timestamp.1235

However, because in the cookie specification, the expires attribute is a formatted date string1242

(for example, like Thursday, September 3rd, with a time), you specify a UNIX timestamp,1248

and PHP automatically converts it to the appropriate timestamp.1253

For example, we have another script called cookieAttributes.php.1259

We are going to create a couple of variables for our setCookie method.1267

We are going to name the cookie example3cookie; we are going to set its value equal to value.1271

We are going to set the domain that the cookie is valid for equal to localhost (even though this is done implicitly, we are just going to do it here explicitly).1276

One thing to note is that the domain always has to be preceded by a dot; that is part of the specification.1283

We are going to specify the path of the cookie; again, this is something that would be implicitly done,1288

but we are going to explicitly specify that this cookie is only valid for the example-3 directory in our lecture_23 directory.1293

We are setting the cookie's expiration equal to time=0; and what that does is: that means that the cookie expires whenever the browser shuts down.1301

That is sort of a default of the cookie specification.1315

Here, we call the setCookie method; we pass it the name and the value of the cookie.1317

We pass it when it expires, and then the path and the domain.1321

And then, this is just an output section that shows what that Set-Cookie header looks like when we receive a response from this page.1327

If we go to example-3, and we go to cookieAttributes.php, we can see here that we are receiving a response1336

that has a Set-Cookie header that has the name of the cookie;1346

it has the path the cookie is valid for; and it also has the domain that it is valid for.1350

It doesn't have an expires attribute, because it has a default expires attribute, which is whenever the session ends, which is why it was set to 0.1356

If we look down here at our raw HTTP response header, we can see the same information1366

with the path, the domain, and then the cookie name and value specified.1373

Now, one thing that you can do is: let's go ahead and try to explicitly set the time.1378

As mentioned, the setCookie method takes a UNIX timestamp, and then converts it to an appropriate date and time.1383

So, let's say we want this cookie to expire one hour from now.1389

It does things in seconds, so 60 seconds times 60 minutes will be one hour.1394

We save it; now, when we go ahead and reload this page...1400

Well, first of all, let's look at the cookie details for example3cookie.1405

We can see that it has the name example3cookie and the value 'value'; it is for the domain localhost.1417

Its path is for the example-3 directory; and expires--it says at end of session, which is when the browser closes (and that is the default).1423

Well, now we have explicitly set the expires value; so when we refresh this page, we are going to see that,1432

in our new Set-Cookie header that is generated, now there is an expires attribute1439

that has been set by the server, when it sends the cookie back to the client.1445

And what it does is: it took that UNIX timestamp that we created and went ahead and generated a date/time string.1450

This is what the format of the date/time string looks like.1456

And what it is saying is "an hour from now is when this cookie expires."1458

And if we look at the raw HTTP response header, we can see that it has this Expires header down here, the path attribute, and the domain attribute.1464

Now, when we go and look at the cookies from localhost, and we look at example3cookie,1475

now we can see that this expires attribute has been updated to a specific date and time,1485

which means that any time after 0100 p.m. on September 2nd, this cookie is no longer valid.1490

And so, the browser is not going to send it any time it requests a page on the localhost domain at the path for the example-3 directory.1496

One thing that you can do is: in order to delete a cookie (get rid of it), you simply set the expires date of the cookie to a time in the past.1506

For example, if instead of adding 60 minutes, we subtracted 60 minutes from the time,1515

that is going to set the cookie's time to a time in the past.1520

What is going to happen is: when we refresh our page, you can see that it shows an expires time that is in the past.1522

And when we look at our raw Cookie header, we can see that it sets the expires attribute to a date in the past.1533

Now, because of that, when we go and look at our cookies for localhost, the example3cookie is no longer there.1543

And that is because our browser recognized, when it received the cookie, that the cookie was expired.1553

So, what it did was deleted it.1557

So, that is one thing to note: if you already have a cookie set on your client machine for a particular domain,1561

a particular path, and with a particular name (for example, example3cookie), if you reset that cookie using the Set-Cookie header,1568

as we did here, it overwrites all of those cookie attributes.1576

So, in this case, we are overwriting the value to 'value,' which is the same as before.1580

We are overwriting the path and domain; well, actually, the path and domain will be the same.1585

But what we are doing is overwriting the expires attribute.1590

What that is going to do is change the value of that cookie on the server, and it is going to update1592

the expires attribute to be in the past, which means that the cookie is no longer going to be set.1596

Now, if we notice, when we refresh this page, even though we had previously set a cookie1603

for this particular domain and this particular path, we can see, if we look at the raw HTTP request,1609

that there is no Cookie header, because there is no cookie to pass on, because the cookie had expired.1615

We use these cookies to pass information back and forth between the client and the server.1624

And so, the question now is, "How do we access that information on a PHP script?"1629

We have a client that we have set a cookie for; the client requests a PHP page, and we want to find out some information in the cookie.1634

For example, maybe the cookie contains information a shopping cart, and we want to access that information in our script1640

to find out what items the user already has in their shopping cart.1646

The way you do that is: we are going to introduce another superglobal called _COOKIE.1653

And what that does is: on any script that is loaded, if a Cookie header has been sent by the client in the HTTP request,1658

any cookie name/value pairs that have been sent are going to be created in this _COOKIE superglobal.1669

For example, if a client were to request index.php at localhost, and it were to set the Cookie header,1676

it is going to send a cookie named testCookie, and it is going to have its value equal to cookieValue,1683

which means that this cookie was previously set for localhost for this particular client.1690

So, it has it stored, and it is sending it back to the server when it is requesting index.php.1695

Well, this index.php page can access this testCookie; it can access the value that it was set to, using the _COOKIE superglobal.1702

And the _COOKIE superglobal is an associative array that contains the names of all of the cookies that were sent to a particular script by the client.1710

In this case, we have one cookie, testCookie; and then, the value of it is the value of this cookie.1721

cookieValue here is going to be equal to the string 'cookieValue.'1727

One of the things we could have done here instead: we could have said item-1001 equals 2.1735

And we will say that that cookie represents that we have item 1001 in quantity 2 in our shopping cart.1742

Now, we could say quantity(Item) equals...and what that is going to do is look for a cookie1749

named 1001 (in this case, assuming we pass it along, it is going to say 1001).1769

And then, it is going to load that value, which is going to be equal to 2.1774

And so now, we are going to know the quantity of that item in a particular cart.1778

If we look at our file called cookieSuperglobal, here we have our same setup here at the beginning,1787

where we are setting a cookie in this particular page.1793

And then, we are outputting information about the HTTP response that is generated,1797

which outputs the Set-Cookie header, so that we can see what it looks like.1803

Then also, we have a section that is going to try and access the value of this cookie.1809

And what it is going to do is: we named our cookie, in this case, example4cookie; and then, we are going to try to output the value of it.1813

What we do is: if we go to example-4, and we load our page cookieSuperglobal.php,1821

we can see that a cookie was set with the value sampleValue; it is called example4cookie.1835

However, in our script, when we try to access example4cookie, it is nonexistent.1842

Now, the reason for that is that the _COOKIE superglobal only has access to cookies that were sent as part of a request for a particular script.1847

The first time, when we requested this particular script, cookieSuperglobal.php, we had no example for cookie.1858

It hadn't been set yet, so we had nothing to send back to the client.1866

If you look at our request and response when we requested this page, you can look at the requests,1869

and you can see that there is no cookie here, because we had not received a cookie yet.1875

However, in our response, we can see that a cookie was set, example4cookie; and it has the path and the domain for it.1879

So now that we have received this cookie, the next time that we refresh this page, we are going to send that cookie along,1887

because it is associated with that path and that domain.1893

And then, we are going to be able to access it in our script.1898

For example, if we refresh the page, now we have access to the value of that cookie.1901

The reason is that, because we had already received the cookie, when we re-requested the page, we send that cookie along.1906

And if we look at the second time we requested that page, we can see that we passed this cookie along via the Cookie header.1913

And one of the things that you can do is set multiple cookies for a particular page.1922

For example, we could set another cookie, call the cookie hello, and set its value equal to world,1927

and just have it have default values for domain, path, and expires.1937

Now, we are setting two cookies when we go to this page.1942

And then, instead of just outputting our cookie example4cookie value, let's go ahead and...1946

we'll just print out the entire _COOKIE superglobal, which will provide information about both the cookies.1957

So now, what we do is: when we go and reload this page, we can see that it has access to our example4cookie.1975

However, we can see that the cookies array only has example4cookie.1989

That is because this page, when we reloaded it for the first time, sent that cookie back to the user.1993

So, the first time we load that page, that second cookie is not available to be sent back.1999

And if we look at our HTTP header, we can see that, in our Set-Cookie header, we have two cookies that are sent:2003

example4cookie equals sampleValue, and then also our other cookie that we created, called hello=world.2017

And when we requested this page, we only had the example4cookie; that is all we sent back.2024

If we go ahead and refresh this page, and look at our HTTP request, now we can see that it sends both cookies back.2029

And one thing to note is that when a client sends a cookie via a Cookie header back to the web server by an HTTP request,2037

it only sends the name and value of the cookie; it doesn't send the expires attribute or the domain attribute or the path attribute--2047

whereas, when the cookie is set, the server does send that information.2057

The reason is that that information is used by the client to decide whether to send the cookie back; and the server does not need to know about that.2060

So, now, when we look at the output of this script, when we dump our _COOKIE superglobal, we can see that it has two cookies in it.2067

One is example4cookie with value sampleValue, and one is the hello cookie that has the value world.2076

I just want to talk, really quickly, about security implications of cookies.2086

Cookies can be essentially be used to track users, and so they have some privacy issues associated with them.2090

For example, let's say you go to a website, and your browser is allowed to accept cookies.2095

They (the website) can set a cookie that sends a unique user ID.2100

So, every time you go back to that website, your browser is going to match the domain of that website, and it is going to send that cookie.2105

So, in that way, the website can track how many times you go to the site, and so forth.2111

There are also some security issues with cookies, in that they store information.2117

So, you could have some sensitive information that gets stored on a person's computer,2121

so anybody that has access to that computer could look at that information.2125

For these reasons, you may have heard that users can disable cookies, which means that they don't accept cookies from web servers.2128

The reason I am mentioning that is that it has the implication that, if you are planning on using cookies2138

to enable some functionality of your website, you have to consider the fact that2144

some of your users may have cookies disabled, and therefore may not be able to access any cookie-dependent functionality.2148

That is just something you should consider.2155

Most people typically use cookies; most websites use them; but that is one thing to consider,2156

depending on your user base and your application, as to whether or not to use cookies--2163

because some of your users may not be able to have access to them.2167

For our homework challenge, I want you to create a script in a directory call subdir in the lecture_23 directory.2172

So, it is a subdirectory; it uses the Set-Cookie function to set a cookie named userID with the value 100.2183

And then, in the same script, I want you to use the _COOKIE superglobal to output the value of that cookie.2191

And remember that the first time you browse to that page, even though you have set the cookie, you are not going to be able to see it,2197

because when you request the page for the first time, you are receiving the cookie for the first time.2203

So, you don't have that cookie to be able to send back.2207

When you refresh the page, you will be able to see the output of that cookie.2209

One thing to note is that you make sure that you call your setCookie function before you try to output the value of the cookie,2213

because setCookie affects HTTP headers, and it has to be called first.2219

Otherwise, it will generate an error.2222

Then, in that same directory, create a second script that deletes the userID cookie.2225

And the way it does that is: you are going to set the expires attribute for that cookie in the setCookie method to a time in the past.2231

And what that is going to is delete the cookie.2238

When you run the second script, it is going to delete that cookie, and you then go ahead and reload (in step 4) that first script again.2241

You should, again, see that the value of the cookie is not available for output.2248

The reason is that the second script deleted that cookie, so when you go back to that first script, you have no cookie to send to the browser.2252

If you go ahead and refresh that page again, however, because that first script sets the cookie,2258

the second time you load it, the value of the cookie is going to be able to be output.2264

Then, going up one directory in lecture_23, I want you to create a script that tries to output the value of the userID cookie, using our _COOKIE superglobal.2269

And when you try to load a script, you should notice that, again, the cookie's value is not output.2281

In this case, it is not because the cookie doesn't exist.2287

It is because, as we mentioned implicitly, when you create a cookie, if you don't explicitly set a path or domain,2290

the cookie's path and domain is the path and domain of whatever script set that particular cookie.2296

So, in this case, because the cookie we generated before was associated with the path lecture_23/subdir,2304

when we are just in the lecture_23 directory, this cookie that we had set doesn't apply, because the path doesn't match.2314

So, it is not going to send that cookie; because it doesn't send that cookie, you are not going to be able2323

to access it via the _COOKIE superglobal, and you are not going to be able to output its value.2326

And so, that shows you a little bit about how the path attribute works for cookies.2332

One thing to note is: practice using the Firebug add-on, when you are running these scripts,2338

to take a look at the raw HTTP request and response headers.2344

That will allow you to look at the Set-Cookie and Cookie headers to see what is going on behind the scenes,2347

so you can understand how those headers work, and how the different cookie attributes (such as expires, domain, and path) are set.2353

It also allows you to see when a cookie is sent from the client to the server, and when the cookie is set in an HTTP response from the server to the client.2361

And that will give you more practice in understanding how cookies work, and when and why2372

they are sent back and forth between a server and a client.2378

That ends today's lesson; thank you for watching Educator.com--I look forward to seeing you next time.2383

Educator®

Please sign in to participate in this lecture discussion.

Resetting Your Password?
OR

Start Learning Now

Our free lessons will get you started (Adobe Flash® required).
Get immediate access to our entire library.

Membership Overview

  • Available 24/7. Unlimited Access to Our Entire Library.
  • Search and jump to exactly what you want to learn.
  • *Ask questions and get answers from the community and our teachers!
  • Practice questions with step-by-step solutions.
  • Download lecture slides for taking notes.
  • Track your course viewing progress.
  • Accessible anytime, anywhere with our Android and iOS apps.