Session & Cookies

The server may start a session for a user when they visit a website. A session also enables the server to record data that is entered into forms and the user’s login status. Normally, when a user logs onto a website, the server starts a session. Sessions can help us give each user a customized experience. A website stores cookies, which are little text files, on your computer, smartphone, or other device when you surf the internet. They are made when a specific website loads in your browser and provides data to your browser, which generates a text file. In this article, we will learn about Session & Cookies, their use in PHP with the help of examples.

As we know that HTTP is a stateless protocol. If we want to track any user information on any website on browser. In that case, we have two options:

  • Session
  • Cookies

Session

A session is a global variable, which is stored on web server. Here, we can store unlimited amount of data. Here we can create more than one session and every sessions keeps a unique id, which is used to get stored values. When we close browser, it will remove session information. Suppose, we are going to purchase any product from shopping website. When we search that product on shopping site and click on Add To Cart. It stores product information in session.

For store any data in session, we have to start session by session_start(). After that, we stores values in PHP global variable $_SESSION.

session_start();
$_SESSION['name'] = 'Kamal';
$_SESSION['email'] = 'kamal.sinha@gmail.com';
$_SESSION['age'] = '23';

For display session variable values, we have to start session and use PHP global variable.

session_start();
echo "Name is: ".$_SESSION['name']. "";
echo "Email is: ".$_SESSION['email']. "";
echo "Age is: ".$_SESSION['age']. "";

For destroy session variable, we use session_destroy() function to destroy all session variable.

session_destroy(); //If you want to destroy all session variable.
unset($_SESSION['name']); //If you want to remove a single variable from session.

PHP String Related Functions

Cookies

It is also used to store users data, but stores on client computer. Here, we can store limited amount of data. We can store only 4KB data in cookies. Cookies keeps expiration time for cookies variable. Suppose, when we login gmail account with email and password then it display all our emails. If we close browser and again want to login with gmail, then it opens our all emails without asking email and password.

For create cookies,

Syntax

setcookie(cookies_name, cookies_value, expiry_time, cookies_stored_path, domain, secure, httponly);

setcookie('name', 'Kamal', time()+120, '/'); //It will expire 120 seconds.

For get cookies value, we use PHP global variable $_COOKIE.

echo "Your cookies value is: ". $_COOKIE['name'];

Answer is:

Your cookies value is: Kamal

For destroy cookies, we set any past date in place of expiration date.

setcookie('name', '', time()-3600);

Categorized in: