0% found this document useful (0 votes)
137 views9 pages

PHP Cookies

Uploaded by

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

PHP Cookies

Uploaded by

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

Introduction to

PHP Cookies
PHP cookies are small piece of data stored on the
user’s computer by the web browser while
browsing a website. They are commonly used to
remember information about the user.
What is a cookie?
A cookie is a small piece of data sent from a website and stored on the user’s computer by the user’s
web browser. It can store user preferences, login information, and other data.

Usage
Cookies are commonly used for session management, personalization, and tracking user behavior.

Duration
They can have an expiry time, after which they will be automatically removed from the user’s
computer.

Security
Cookies are restricted by the same origin policy for security reasons
How to create cookies with PHP
In PHP, cookies are created using the setcookie() function. This function sets a cookie to be sent
along with the rest of the HTTP headers.

setcookie()
This function accepts parameters like name, value, expiry time, path, domain, secure, and httponly.

Response Headers
When a cookie is set using setcookie(), it becomes a part of the HTTP headers returned by the web
server to the client.

Example
Setcookie(“user”, “Abebe”, time() + (86400 * 30), '/');
Managing cookie expiration and deletion
Cookie expiration can be managed by specifying an expiry time when creating the cookie. Cookies
can also be deleted by setting their expiry time to a past date.

Expiry Time

Specify an expiry time to manage when the cookie will expire.

Deletion

To delete a cookie, set its expiry time to a time in the past.


The syntax for creating cookies
The syntax for creating a cookie with the setcoookie() function is straightforward and consists of
setting the necessary parameters and values to define the cookie’s properties.

Syntax

setcookie(name, value, expire, path, domain, secure, httponly);


PHP: Create and retrieve a cookie
PHP provides ways to create and retrieve cookies. With the setcookie() function, new cookies can be
set, and with $_COOKIE[] , the stored cookies can be retrieved.

Create Cookie

Use setcookie() to create a new cookie and set its properties.

Retrieve Cookie

Access the stored cookies using the $_COOKIE[] array.


Storing and accessing cookie data in PHP
Cookie data is stored on the client's system and sent with every request, making it accessible via the
$_ COOKIE[] array. The stored data can be used for various purposes.

Data Storage

Cookie data is stored on the user’s system and can be accessed for various functionalities.

Server-Side Access

The data stored in cookies can be accessed and utilized on the server side.
How do we use cookies in PHP?
It’s essential to follow best practices when working with cookies. This includes setting secure and
HTTP only cookies, validating and sanitizing cookie data, and being transparent about cookie
usage through a privacy policy.

Data Security
Ensure cookies carry only necessary information and are secure.

Privacy Policy

Be transparent about cookie usage through a well-defined privacy policy.


Group Members
• Biniyam Feyissa ETS 0361/14
• Birhan Aklilu ETS 0363/14
• Biruk Lemma ETS 0368/14
• Dagmawit Gebrewold ETS 0435/14
• Etsub Girma ETS 0563/14
• Hana Abiyu ETS 0728/14
• Hemenawit Girma ETS 0772/14

You might also like