0% found this document useful (0 votes)
13 views16 pages

IWD - Unit 4 - Part 2 - Cookies & Sessions

The document provides an introduction to PHP cookies and sessions, detailing their creation, access, and deletion. It explains cookies as small pieces of information stored in the client browser and sessions as server-stored data used across multiple pages. The document outlines the necessary PHP functions and syntax for managing cookies and sessions effectively.

Uploaded by

anshkathrecha13
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views16 pages

IWD - Unit 4 - Part 2 - Cookies & Sessions

The document provides an introduction to PHP cookies and sessions, detailing their creation, access, and deletion. It explains cookies as small pieces of information stored in the client browser and sessions as server-stored data used across multiple pages. The document outlines the necessary PHP functions and syntax for managing cookies and sessions effectively.

Uploaded by

anshkathrecha13
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

PHP Cookies & Sessions – Introduction to

Webpage Development (IWD)(4340704)


- Made by:
- Hardik N. Talsania (Lecturer – RCTI, Ahmedabad)
Contents
⚫ Learning Outcomes
⚫ What is a Cookie?
⚫ Creating, Accessing & Deleting a Cookie Variable
⚫ What is a Session?
⚫ Working of Session in PHP
⚫ Creating, Accessing & Deleting a Session Variable

2 H N TALSANIA (RCTI) 7 June 2023


Learning Outcomes
⚫ After completion of this session, students will be able to:

⚫ Create dynamic web pages in PHP using cookies


⚫ Create dynamic web pages in PHP using sessions

3 H N TALSANIA (RCTI) 7 June 2023


What is a Cookie?
⚫ A cookie is a small piece of
information which is stored at client
browser for future use.

⚫ Each time when client sends request


to the server, a small text file (cookie)
is embedded with the request.

⚫ This way, cookie can be received at the


server side and can be used to identify
the user in future.

4 H N TALSANIA (RCTI) 7 June 2023


What is a Cookie?
⚫ E.g.: ‘Username’ & ‘Password’ Cookies are created and used in future,
when we ‘Save Username and/or Password’ while logging in a website.

⚫ Cookies (that are saved in browser on the client system) can be viewed
through ‘Internet Settings’ in the browser.

⚫ Cookies can be created on the server side (through PHP script). They are
sent to the client system, where they can be saved and accessed in future.

⚫ Cookies can also be deleted from the client system (through PHP script).

5 H N TALSANIA (RCTI) 7 June 2023


Creating, Accessing & Deleting a Cookie Variable
1. Creating / modifying a Cookie Variable:
⚫ A Cookie variable can be created / modified through PHP script using
setcookie( ) function.

⚫ Syntax:
setcookie(name, value, expiry, path, domain, security);

⚫ Only the name parameter is required. All other parameters are optional.

⚫ Example:
setcookie(‘user’, ‘hnt’, time()+3600, ‘/’, ‘’, 1);

6 H N TALSANIA (RCTI) 7 June 2023


Creating, Accessing & Deleting a Cookie Variable
⚫ setcookie( ) parameters:
• name: The name by which the cookie is stored and accessed.
• value: The value (content) of the named cooked variable.
• expiry: Time (in seconds) (since 1/1/1970) after which the cookie will become
inaccessible. If this parameter is not set, then cookie will automatically expire when
the Web Browser is closed.
• path: The directories for which the cookie is valid. A single forward slash character
permits the cookie to be valid for all directories.
• domain: The (sub) domain, the cookie is available to. All cookies are only valid for
the host and domain which created them.
• security: 1 = secure transmission using HTTPS; 0 = sent by regular HTTP.

7 H N TALSANIA (RCTI) 7 June 2023


Creating, Accessing & Deleting a Cookie Variable
2. Accessing a Cookie Variable:
⚫ A cookie variable (saved on the browser) is accessed through PHP script using
the superglobal variable $_COOKIE or $HTTP_COOKIE_VARS.

⚫ $_COOKIE and $HTTP_COOKIE_VARS are also in-built associative arrays just


like other superglobal arrays, in which name of the cookie is passed as a key.

⚫ Syntax:
$_COOKIE[‘cookiename’]; or $HTTP_COOKIE_VARS[‘cookiename’];

⚫ Example:
echo ‘Hello ’ . $_COOKIE[‘user’];

8 H N TALSANIA (RCTI) 7 June 2023


Creating, Accessing & Deleting a Cookie Variable
3. Deleting a Cookie Variable:
⚫ As such, there is no in-built function in PHP to delete a stored cookie.

⚫ However, we can delete the cookie by setting the ‘expiry’ parameter to some
time in the past (instead of future) in the setcookie() function.

⚫ Example:
setcookie(‘user’, ‘’, time() – 3600);

⚫ Here, cookie is set to expire before 1 Hour. So, it automatically gets deleted.

9 H N TALSANIA (RCTI) 7 June 2023


What is a Session?
⚫ A session is another way to store information (in variables) to be used
across multiple pages.

⚫ Usually, a session (or a session variable) is created for a user when he/she
logs in (or access) a website and remains alive till he logs out (or the
browsing session ends).

⚫ Unlike a cookie, the information is not stored on the client’s system.


Instead, it is stored on the server in a file in temporary directory.

10 H N TALSANIA (RCTI) 7 June 2023


Working of Session in PHP
⚫ Starting a Session in PHP:
⚫ A session is started in PHP by calling session_start() function at the start of a
PHP page.

⚫ This function first checks if a session is already started and refers to it. If no
session is started, then it starts a new one.

⚫ This function needs to be called on all the pages where a session variable is
needed to be created, modified, accessed or destroyed.

11 H N TALSANIA (RCTI) 7 June 2023


Working of Session in PHP
⚫ Whenever a session is started by a user, PHP first creates a unique session id for
that particular session, which is a random string of 32 hexadecimal numbers
such as 3c7foj34c3jj973hjkop2fc937e3443.

⚫ Then, a cookie called PHPSESSID is automatically sent to the user's computer


(client system) to store this unique session id.

⚫ Then, a file is created in the temporary directory on the server with name
‘sess_uniquesessionid’ (for e.g. ‘sess_3c7foj34c3jj973hjkop2fc937e3443’).

⚫ The location of this temporary file is determined by setting a variable in the


‘php.ini’ file called ‘session.save_path’.

12 H N TALSANIA (RCTI) 7 June 2023


Working of Session in PHP
⚫ When value from a session variable is requested, PHP looks into the
PHPSESSID cookie for the unique session id and searches for the file
‘sess_uniquesessionid’ in the temporary directory.

⚫ If a file is found, it searches for the requested session variable in it and


return its value. If the file or the session variable is not found, PHP raises
an error.

13 H N TALSANIA (RCTI) 7 June 2023


Creating, Accessing & Deleting a Session Variable
1. Creating / modifying / accessing a Session Variable:
⚫ A session variable can be created / modified / accessed through PHP script
using the superglobal variable $_SESSION.

⚫ Syntax:
$_SESSION[‘sessionvariablename’];

⚫ Example:
$_SESSION[‘username’] = ‘hnt’; //creates or modifies the session variable
echo $_SESSION[‘username’]; //prints the value of the session variable

14 H N TALSANIA (RCTI) 7 June 2023


Creating, Accessing & Deleting a Session Variable
2. Deleting a Session or a Session Variable:
⚫ Separate PHP functions can be used for deleting a single session variable or
deleting all session data as listed below:

Sr. No. Function Description Example

1 session_destroy(); Destroys the complete session session_destroy();

Frees all currently registered


2 session_unset(); session_unset();
session variables

3 unset(variablename); Deletes a single session variable unset($_SESSION[‘user’]);

15 H N TALSANIA (RCTI) 7 June 2023


THANK YOU

16 H N TALSANIA (RCTI) 7 June 2023

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy