Trending September 2023 # Php Session &Amp; Php Cookies With Example # Suggested October 2023 # Top 16 Popular |

Trending September 2023 # Php Session &Amp; Php Cookies With Example # Suggested October 2023 # Top 16 Popular

You are reading the article Php Session &Amp; Php Cookies With Example updated in September 2023 on the website We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 Php Session &Amp; Php Cookies With Example

What is Cookie?

A cookie is a small file with the maximum size of 4KB that the web server stores on the client computer.

Once a cookie has been set, all page requests that follow return the cookie name and value.

A cookie created by a user can only be visible to them. Other users cannot see its value.

Most web browsers have options for disabling cookies, third party cookies or both.

If this is the case then PHP responds by passing the cookie token in the URL.

The diagram shown below illustrates how cookies work.


1) A user requests for a page that stores cookies

2) The server sets the cookie on the user’s computer

3) Other page requests from the user will return the cookie name and value

In this tutorial, you will learn-

Why and when to use Cookies?

Http is a stateless protocol; cookies allow us to track the state of the application using small files stored on the user’s chúng tôi path were the cookies are stored depends on the browser.Internet Explorer usually stores them in Temporal Internet Files folder.

Personalizing the user experience – this is achieved by allowing users to select their chúng tôi page requested that follow are personalized based on the set preferences in the cookies.

Tracking the pages visited by a user

Creating Cookies

Let’s now look at the basic syntax used to create a cookie.



Php“setcookie” is the PHP function used to create the cookie.

“cookie_name” is the name of the cookie that the server will use when retrieving its value from the $_COOKIE array variable. It’s mandatory.

“cookie_value” is the value of the cookie and its mandatory

“[expiry_time]” is optional; it can be used to set the expiry time for the cookie such as 1 hour. The time is set using the PHP time() functions plus or minus a number of seconds greater than 0 i.e. time() + 3600 for 1 hour.

“[cookie_path]” is optional; it can be used to set the cookie path on the server. The forward slash “/” means that the cookie will be made available on the entire domain. Sub directories limit the cookie access to the subdomain.

“[Httponly]” is optional. If it is set to true, then only client side scripting languages i.e. JavaScript cannot access them.

Note: the php set cookie function must be executed before the HTML opening tag.

Let’s now look at an example that uses cookies.

We will create a basic program that allows us to store the user name in a cookie that expires after ten seconds.

The code below shows the implementation of the above example “cookies.php”.

<?php setcookie("user_name", "Guru99", time()+ 60,'/'); echo 'the cookie has been set for 60 seconds';


the cookie has been set for 60 seconds Retrieving the Cookie value

Create another file named “cookies_read.php” with the following code.

<?php print_r($_COOKIE);


Note: $_COOKIE is a PHP built in super global variable.

It contains the names and values of all the set cookies.

The number of values that the

$_COOKIE array can contain depends on the memory size set in php.ini.

The default value is 1GB.

Testing our application.

Let’s assume you have saved your PHP files in phptus folder.

Note: Only an empty array has been displayed

Delete Cookies

If you want to destroy a cookie before its expiry time, then you set the expiry time to a time that has already passed.

Create a new filed named cookie_destroy.php with the following code

<?php setcookie("user_name", "Guru99", time() - 360,'/');

Repeat steps 1 through to 3 from the above section on retrieving cookie values.

What is a Session?

A session is a global variable stored on the server.

Each session is assigned a unique id which is used to retrieve stored values.

Whenever a session is created, a cookie containing the unique session id is stored on the user’s computer and returned with every request to the server. If the client browser does not support cookies, the unique php session id is displayed in the URL

Sessions have the capacity to store relatively large data compared to cookies.

The session values are automatically deleted when the browser is closed. If you want to store the values permanently, then you should store them in the database.

Just like the $_COOKIE array variable, session variables are stored in the $_SESSION array variable. Just like cookies, the session must be started before any HTML tags.

Why and when to use Sessions?

You want to store important information such as the user id more securely on the server where malicious users cannot temper with them.

You want to pass values from one page to another.

You want the alternative to cookies on browsers that do not support cookies.

You want to store global variables in an efficient and more secure way compared to passing them in the URL

You are developing an application such as a shopping cart that has to temporary store information with a capacity larger than 4KB.

Creating a Session

In order to create a session, you must first call the PHP session_start function and then store your values in the $_SESSION array variable.

Let’s suppose we want to know the number of times that a page has been loaded, we can use a session to do that.

The code below shows how to create and retrieve values from sessions

<?php session_start(); if(isset($_SESSION['page_count'])) { $_SESSION['page_count'] += 1; } else { $_SESSION['page_count'] = 1; } echo 'You are visitor number ' . $_SESSION['page_count'];


You are visitor number 1 Destroying Session Variables

The session_destroy() function is used to destroy the whole Php session variables.

If you want to destroy only a session single item, you use the unset() function.

The code below illustrates how to use both methods.


session_destroy(); <?php


Session_destroy removes all the session data including cookies associated with the session.

Unset only frees the individual session variables.

Other data remains intact.


Cookies are small files saved on the user’s computer

Cookies can only be read from the issuing domain

Cookies can have an expiry time, if it is not set, then the cookie expires when the browser is closed

Sessions are like global variables stored on the server

Each session is given a unique identification id that is used to track the variables for a user.

Both cookies and sessions must be started before any HTML tags have been sent to the browser.

You're reading Php Session &Amp; Php Cookies With Example

Update the detailed information about Php Session &Amp; Php Cookies With Example on the website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!