Skip to main content

Practical-17

Write a PHP Program for Session and Cookies.

Meta Description

Create session and cookes program with PHP. Strongly secure your confidential data with state of the art programing.

Introdution

We programmers and developers use PHP to create web applications and websites. Usually the web applications have confidential information - and it's a must that we protect this private data and make sure it is only available to authorized parties. For this purpose, and to confidently store user-specific data securely, we use session and cookies in PHP. 🤓

We can store data on the client's computer securely and encrypted by creating session and cookies in PHP. PHP Session stores the data on the server, and cookies store the data on the client's computer. Through this, we can create a secure application and make sure only authorized parties can access the confidential information. Before delving further let's understand what session and cookies in PHP are.

Session and cookies are different in their working, but together they ensure secure storage of data. To store the data in session and cookies, you need to understand the basics of PHP coding. It's important to create a secure application and use the right tools to do it. 🔑

Code

<?php
session_start();
$value = "Value to be stored as a cookie";

// Set cookie
setcookie("cookieName", $value);

// Store session data
$_SESSION["username"]= "JohnDoe";
?>

Output

No Output

Explanation

The above PHP program uses the setcookie() function to set a cookie, and stores a pre-defined session data in the respective session.

Get Help