Download presentation
Presentation is loading. Please wait.
Published byEdwin Shepherd Modified over 8 years ago
1
Cookies and Sessions in PHP
2
Arguments for the setcookie() Function There are several arguments you can use i.e. setcookie(‘name’, ‘value’, expiration, ‘path’, ‘domain’, secure); Passing the name and value arguments to the setcookie function will suffice most cookie uses.
3
Argument Explanation Expiration: used to set a specific length for a cookie to be functional. This line of code sets the expiration time of the cookie to one hour from the current moment: setcookie(‘name’, ‘value’, time()+3600); Path and domain: used to limit a cookie to a specific folder in a website (path) or to a specific domain. This line of code limits the life of the cookie so it becomes active while the user is in the user folder: setcookie(‘name’, ‘value’, time()+3600, ‘/user/’); Secure: dictates wheather or not the cookie should be sent over a secure HTTPS connection (value of 1 indicates that a secure connection must be used while the value of 0 means the opposite: setcookie(‘name’, ‘value’, time()+3600, ‘/‘, ‘‘, 1);
4
Creating Cookies Script 9.1
5
Reading from Cookies Script 9.2
6
Deleting Cookies setcookie(‘bg-color’, ‘ ‘, time()-60, ‘/’, ‘’, 0); Note: the expiration argument may be set to a time in the past. (optional and not required) Another way for deleting cookies: setcookie(‘bg-color’, ‘ ‘);
7
Sessions Sessions are like cookies in which they provide a way for a server to track user’s data over a series of pages. The difference between the two is that cookie information is stored on the client side while session information is stored on the server side. When a session is started PHP generates a random session ID. By default this session ID is sent to the web browser as a cookie.
8
Sessions and Cookies SessionsCookies More secure (data is not transmitted between server and client) Easier to create and retrieve Store more information than a cookies Require slightly less work from the server Sessions can work even if user does not accept cookies Persist over a longer period of time
9
Creating a Session PHP generates a default session name PHPSESSID and a value like: PHPSESSID=4bcc48dc87cb4b54d63f99da23fb41e1 If you want to change the default session name you can use the session_name() function. Use the session_start() function to start the session. Session data is stored in $_SESSION array.
10
Code Example for Creating a Session Script 9.5
11
Accessing Session Variables Script 9.6
12
Deleting a Session Script 9.7
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.