PR 10
PR 10
Date: _________________
Practical No.10: Session and Cookies
a. Write a PHP script to demonstrate creating, deleting, updating,
retrieving and passing data with Cookie.
b. Write PHP script to demonstrate passing information using
Session.
A. Objective:
A cookie is often used to identify a user. It is a small file that the server stores on the
user's computer. Each time the same computer requests a page with a same browser, it
will send the cookie too A session is a way to store information which is to be used
across multiple pages. In this practical student will learn how to create cookie, modify
it and delete it. Also, they will learn how to start a session, fetch session variables and
destroy a session.
116 | Page
Web Development using PHP (4341604)
CO4: Debug the Programs by applying state management concepts and error handling
techniques of PHP.
E. Practical Outcome(PRo)
Students will be able to create, store, fetch, delete cookies and session.
G. Prerequisite Theory:
Cookies:
Cookies are small files that are stored on the client's computer by the web server.
Cookies are used to store small amounts of data that can be accessed by the web server
when the client requests a page from the website. Cookies are commonly used to store
user preferences, shopping cart items, login credentials, and other user-specific
information.
To set a cookie in PHP, you can use the setcookie() function.
Syntax:
setcookie(Name, value, Expire Time)
To retrieve a cookie value in PHP, you can use the $_COOKIE superglobal array. The
$_COOKIE array contains all of the cookies that have been set by the web server.
Syntax:
$variable_name = $_COOKIE['cookie_name'];
To delete a cookie create a cookie using setcookie() function without specifying any
Expire Time or give past time as an expiry time.
Syntax:
setcookie (‘Cookie_Name’, "") /OR/ setcookie ("name","",time()-60)
117 | Page
Web Development using PHP (4341604)
Example:
<?php
setcookie("user", "IT");
?>
<html>
<body>
<?php
if(!isset($_COOKIE["user"])) {
echo "Sorry, cookie is not found!";
} else {
echo "<br/>Cookie Value: " . $_COOKIE["user"];
}
?>
</body>
</html>
Session
Session is a way to store data on the server side that is associated with a specific user
or client. Sessions are used to maintain stateful information across multiple requests
from the same client. To start a session in PHP, you need to call the session_start()
function at the beginning of your script. This function creates a new session or
resumes an existing session if one exists.
Syntax:
session_start();
Once the session has been started, you can store data in the session by setting values in
the $_SESSION superglobal array.
Syntax:
$_SESSION['session_name'] = 'value';
To retrieve data from the session, you can simply access the $_SESSION superglobal
array.
Syntax:
$variable_name = $_SESSION['session_name'];
You can destroy a session and all of its associated data by calling the session_destroy()
function. This function will remove all session.
To unset specific session variables without destroying the entire session, you can use
the unset() function.
118 | Page
Web Development using PHP (4341604)
Syntax:
session_destroy();
unset($_SESSION['session_name']);
Example: session1.php
<?php
session_start();
?>
<html>
<body>
<?php
$_SESSION["user"] = "IT";
echo "Session information are set successfully.<br/>";
?>
<a href="session2.php">Visit session2.php</a>
</body>
</html>
session2.php
<?php
session_start();
?>
<html>
<body>
<?php
echo "User is: ".$_SESSION["user"];
?>
</body>
</html>
H. Resources/Equipment Required
Sr. Instrument/Equipment/
Specification Quantity
No. Components/Trainer kit
Computer (i3-i5 preferable), RAM
1 Hardware: Computer System
minimum 2 GB and onwards
2 Operating System Windows/ Linux/ MAC
XAMPP server (PHP, Web server, As Per
3 Software Batch Size
Database)
Notepad, Notepad++, Sublime Text or
4 Text Editor
similar
119 | Page
Web Development using PHP (4341604)
NA
J. Source code:
<!DOCTYPE html>
<html>
<head>
<title>PHP Cookie Demo</title>
</head>
<body>
<h2>PHP Cookie Handling</h2>
120 | Page
Web Development using PHP (4341604)
</form>
</body>
</html>
A) Input/Output:
<?php
session_start(); // Start the session
121 | Page
Web Development using PHP (4341604)
<!DOCTYPE html>
<html>
<head>
<title>PHP Session Demo</title>
</head>
<body>
<h2>PHP Session Handling</h2>
B ) Input/Output:
123 | Page
Web Development using PHP (4341604)
L. References / Suggestions
1) https://www.w3schools.com/php/default.asp
2) https://www.guru99.com/php-tutorials.html
3) https://www.tutorialspoint.com/php/
4) https://tutorialehtml.com/en/php-tutorial-introduction/
5) www.tizag.com/phpT/
6) https://books.goalkicker.com/PHPBook/
7) https://spoken-tutorial.org/tutorial-
search/?search_foss=PHP+and+MySQL&search_language=English
8) https://codecourse.com/watch/php-basics
9) https://onlinecourses.swayam2.ac.in/aic20_sp32/preview
M. Assessment-Rubrics
Faculty
Marks Obtained Date
Signature
Program Implementation Student’s engagement
Correctness and Presentation in practical activities Total
(4) Methodology (3) (3) (10)
R1 R2 R3
124 | Page