Download presentation
Presentation is loading. Please wait.
Published byMilton Ryan Modified over 6 years ago
1
19.10 Using Cookies A cookie is a piece of information that’s stored by a server in a text file on a client’s computer to maintain information about the client during and between browsing sessions. A server can access only the cookies that it has placed on the client. Function setcookie takes the name of the cookie to be set as the first argument, followed by the value to be stored in the cookie. The optional third argument indicates the expiration date of the cookie. If no expiration date is specified, the cookie lasts only until the end of the current session—that is, when the user closes the browser. This type of cookie is known as a session cookie, while one with an expiration date is a persistent cookie. © by Pearson Education, Inc. All Rights Reserved.
2
19.10 Using Cookies (Cont.) If only the name argument is passed to function setcookie, the cookie is deleted from the client’s computer. Cookies defined in function setcookie are sent to the client at the same time as the information in the HTTP header; therefore, setcookie needs to be called before any other output PHP creates the superglobal array $_COOKIE, which contains all the cookie values indexed by their names, similar to the values stored in array $_POST when an HTML5 form is posted © by Pearson Education, Inc. All Rights Reserved.
3
Create Cookies With PHP
A cookie is created with the setcookie() function. Syntax: setcookie(name, value, expire, path, domain, secure, httponly); (Only the name parameter is required. All other parameters are optional) © by Pearson Education, Inc. All Rights Reserved.
4
Create Cookies With PHP
Parameter Description name The name of the cookie. value The value of the cookie. expire The time the cookie expires. path The path on the server in which the cookie will be available on. If set to '/', the cookie will be available within the entire domain. If set to '/foo/', the cookie will only be available within the /foo/ directory and all sub-directories such as /foo/bar/ of domain. The default value is the current directory that the cookie is being set in. domain The (sub)domain that the cookie is available to. secure Indicates that the cookie should only be transmitted over a secure HTTPS connection from the client. When set to TRUE, the cookie will only be set if a secure connection exists. httponly When TRUE the cookie will be made accessible only through the HTTP protocol. © by Pearson Education, Inc. All Rights Reserved.
5
PHP Create/Retrieve a Cookie
© by Pearson Education, Inc. All Rights Reserved.
6
Modify a Cookie Value To modify a cookie, just set (again) the cookie using the setcookie() function: © by Pearson Education, Inc. All Rights Reserved.
7
Delete a Cookie To delete a cookie, use the setcookie() function with an expiration date in the past: © by Pearson Education, Inc. All Rights Reserved.
8
Check if Cookies are Enabled
First, try to create a test cookie with the setcookie() function, then count the $_COOKIE array variable: © by Pearson Education, Inc. All Rights Reserved.
9
Using cookies: Example
© by Pearson Education, Inc. All Rights Reserved.
10
©1992-2012 by Pearson Education, Inc. All Rights Reserved.
11
©1992-2012 by Pearson Education, Inc. All Rights Reserved.
12
©1992-2012 by Pearson Education, Inc. All Rights Reserved.
13
©1992-2012 by Pearson Education, Inc. All Rights Reserved.
14
©1992-2012 by Pearson Education, Inc. All Rights Reserved.
15
©1992-2012 by Pearson Education, Inc. All Rights Reserved.
16
©1992-2012 by Pearson Education, Inc. All Rights Reserved.
17
©1992-2012 by Pearson Education, Inc. All Rights Reserved.
18
PHP Sessions A session is a way to store information (in variables) to be used across multiple pages. A session is server-side information intended to exist only throughout the visitor's interaction with the website. Unlike a cookie, the information is not stored on the users computer. When you work with an application, you open it, do some changes, and then you close it. This is much like a Session. The computer knows who you are. It knows when you start the application and when you end. But on the internet there is one problem: the web server does not know who you are or what you do, because the HTTP address doesn't maintain state. Session variables solve this problem by storing user information to be used across multiple pages (e.g. username, favorite color, etc). By default, session variables last until the user closes the browser. © by Pearson Education, Inc. All Rights Reserved.
19
Start a PHP Session A session is started with the session_start() function. Session variables are set with the PHP global variable: $_SESSION. The session_start() function must be the very first thing in your document. Before any HTML tags. Example: © by Pearson Education, Inc. All Rights Reserved.
20
Get PHP Session Variable Values
© by Pearson Education, Inc. All Rights Reserved.
21
Show all the session variables
Output: Array ( [favcolor] => green [favanimal] => cat ) © by Pearson Education, Inc. All Rights Reserved.
22
Modify a PHP Session Variable
To change a session variable, just overwrite it: © by Pearson Education, Inc. All Rights Reserved.
23
Destroy a PHP Session To remove all global session variables and destroy the session, use session_unset() and session_destroy(): © by Pearson Education, Inc. All Rights Reserved.
24
19.11 Dynamic Content The isset function determines whether the $_POST array contains keys representing the various form fields. The notation$$variable specifies a variable variable, which allows the code to reference variables dynamically. You can use this expression to obtain the value of the variable whose name is equal to the value of $variable. The function mysql_real_escape_string inserts a backslash (\) before any special characters in the passed string. © by Pearson Education, Inc. All Rights Reserved.
25
©1992-2012 by Pearson Education, Inc. All Rights Reserved.
26
©1992-2012 by Pearson Education, Inc. All Rights Reserved.
27
©1992-2012 by Pearson Education, Inc. All Rights Reserved.
28
©1992-2012 by Pearson Education, Inc. All Rights Reserved.
29
©1992-2012 by Pearson Education, Inc. All Rights Reserved.
30
©1992-2012 by Pearson Education, Inc. All Rights Reserved.
31
©1992-2012 by Pearson Education, Inc. All Rights Reserved.
32
©1992-2012 by Pearson Education, Inc. All Rights Reserved.
33
©1992-2012 by Pearson Education, Inc. All Rights Reserved.
34
©1992-2012 by Pearson Education, Inc. All Rights Reserved.
35
©1992-2012 by Pearson Education, Inc. All Rights Reserved.
36
©1992-2012 by Pearson Education, Inc. All Rights Reserved.
37
©1992-2012 by Pearson Education, Inc. All Rights Reserved.
38
©1992-2012 by Pearson Education, Inc. All Rights Reserved.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.