0% found this document useful (0 votes)
9 views

PHP Cookies

PHP cookies are small pieces of information stored in the client's browser used to recognize users. Cookies are created and saved to the client's browser by the server and sent back with each request, allowing the server to access the cookie data. The setcookie() function is used to create cookies by specifying parameters like name, value, expiration, and path. Cookies can store things like shopping cart contents, user preferences, and login credentials across visits.

Uploaded by

deepblue5991
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

PHP Cookies

PHP cookies are small pieces of information stored in the client's browser used to recognize users. Cookies are created and saved to the client's browser by the server and sent back with each request, allowing the server to access the cookie data. The setcookie() function is used to create cookies by specifying parameters like name, value, expiration, and path. Cookies can store things like shopping cart contents, user preferences, and login credentials across visits.

Uploaded by

deepblue5991
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

PHP Cookies

PHP cookie is a small piece of information which is stored at client browser. It is used to recognize
the user.

Cookie is created at server side and saved to client browser. Each time when client sends request to
the server, cookie is embedded with request. Such way, cookie can be received at the server side.

They are typically used to keep track of information such as a username that the site can retrieve to
personalize the page when the user visits the website next time. A cookie can only be read from the
domain that it has been issued from. Cookies are usually set in an HTTP header but JavaScript can
also set a cookie directly on a browser.
Creating Cookies with setcookie()
Set Cookie 1
The setcookie() function is used to
create a new cookie on the client's
browser. It requires several 2 Store on Client
parameters, including the name, The cookie data is then stored as a
value, expiration time, path, domain, text file on the client's machine. On
and security settings. subsequent visits, the cookie is
included in the request header and
sent back to the server.
Access on Server 3
The server can then access the
cookie data through the $_COOKIE
superglobal variable, which contains
all the cookies present in the current
request.
Syntax:
setcookie(name, value, expire, path, domain, security);

Parameters: The setcookie() function requires six arguments in general which are:

Cookie Parameters
Name Value Expiration

The name of the cookie, The value to be stored in the The timestamp when the
used to identify it. cookie. cookie will expire and no
longer be accessible.
Cookie Parameters
Path Domain Security

It is used to specify the path It is used to specify the It is used to indicate that
on the server for which the domain for which the cookie the cookie should be sent
cookie will be available. is available. only if a secure HTTPS
connection exists.
Setting a Cookies:

In this example:

· $cookie_name is the name of the cookie.

· $cookie_value is the value you want to store in the cookie.

· time() + (86400 * 30) sets the cookie to expire in 30 days.

· "/" makes the cookie available across the entire website.


Checking for Cookies
1 isset() 2 $_COOKIE 3 Retrieving Values
The isset() function is The $_COOKIE Cookie values can be
used to check if a superglobal variable accessed through the
cookie has been set contains all the cookies $_COOKIE array, using
before accessing its sent by the client in the cookie name as the
value. the current request. key.
Deleting Cookies
Call setcookie()
To delete a cookie, call the setcookie() function again, passing the same name as before.

Set Expiration
However, this time set the expiration time to a value in the past, such as one day ago.

Cookie Removed
This will effectively delete the cookie from the client's browser.
Cookies in Action

Shopping Cart Personalization Authentication Analytics


Cookies can store Cookies allow Cookies can be used Cookies can track
the contents of a websites to to store login user behavior and
user's shopping cart, customize the user credentials, allowing preferences to
even if they leave the experience, such as users to stay signed improve the website
site and come back displaying a user's in across multiple and marketing
later. preferred language visits. strategies.
or theme.
Cookies and Security
Sensitive Data Expiration
Cookies should never store sensitive Cookies should have a reasonable
information like passwords or credit card expiration time to limit the window of
numbers, as they can be accessed by the potential misuse if the cookie is
client. compromised.

HTTPS HttpOnly
Cookies should be set with the "secure" flag Cookies should be set with the "HttpOnly"
to ensure they are only transmitted over a flag to prevent them from being accessed
secure HTTPS connection. by client-side scripts, reducing the risk of
XSS attacks.

You might also like