0% found this document useful (0 votes)
15 views9 pages

PR 10

This document outlines a practical session on using PHP for managing sessions and cookies. It includes objectives, expected outcomes, skills to be developed, and detailed PHP scripts for creating, updating, retrieving, and deleting cookies and sessions. Additionally, it provides quizzes, references, and assessment rubrics related to the practical activities.

Uploaded by

lathiyaparth61
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views9 pages

PR 10

This document outlines a practical session on using PHP for managing sessions and cookies. It includes objectives, expected outcomes, skills to be developed, and detailed PHP scripts for creating, updating, retrieving, and deleting cookies and sessions. Additionally, it provides quizzes, references, and assessment rubrics related to the practical activities.

Uploaded by

lathiyaparth61
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Web Development using PHP (4341604)

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.

B. Expected Program Outcomes (POs)

Basic and Discipline specific knowledge: Apply knowledge of basic mathematics,


science and engineering fundamentals and engineering specialization to solve the
engineering problems.
Problem analysis: Identify and analyse well-defined engineering problems using
codified standard methods.
Design/ development of solutions: Design solutions for engineering well-defined
technical problems and assist with the design of systems components or processes to
meet specified needs.
Engineering Tools, Experimentation and Testing: Apply modern engineering tools
and appropriate technique to conduct standard tests and measurements.
Project Management: Use engineering management principles individually, as a team
member or a leader to manage projects and effectively communicate about well-
defined engineering activities.
Life-long learning: Ability to analyze individual needs and engage in updating in the
context of technological changes in field of engineering.

C. Expected Skills to be developed based on competency:

“Develop a webpage using PHP”


This practical is expected to develop the following skills.
1. Understand the difference between session and cookies.
2. Develop an application to start session, get session and destroy session.
3. Demonstrate the use of cookies by creating, deleting, updating, retrieving and
passing data with it.

116 | Page
Web Development using PHP (4341604)

D. Expected Course Outcomes(Cos)

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.

F. Expected Affective domain Outcome(ADos)

1) Follow safety practices.


2) Follow Coding standards and practices.
3) Demonstrate working as a leader/ a team member.
4) Follow ethical practices.
5) Maintain tools and equipment.

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

5 Web Browser Edge, Firefox, Chrome or similar

119 | Page
Web Development using PHP (4341604)

I. Safety and necessary Precautions followed

NA

J. Source code:

A) Write a PHP script to demonstrate creating, deleting, updating, retrieving and


passing data with Cookie.
<?php
// Set a cookie (Create)
if (isset($_POST['set_cookie'])) {
setcookie("username", $_POST['username'], time() + (86400 * 7), "/"); // 7 days
header("Location: ".$_SERVER['PHP_SELF']); // Refresh page
exit;
}

// Retrieve Cookie (Read)


$username = isset($_COOKIE['username']) ? $_COOKIE['username'] : "Not set";

// Update Cookie (Modify)


if (isset($_POST['update_cookie'])) {
setcookie("username", $_POST['username'], time() + (86400 * 7), "/");
header("Location: ".$_SERVER['PHP_SELF']); // Refresh page
exit;
}

// Delete Cookie (Remove)


if (isset($_POST['delete_cookie'])) {
setcookie("username", "", time() - 3600, "/"); // Expire the cookie
header("Location: ".$_SERVER['PHP_SELF']); // Refresh page
exit;
}
?>

<!DOCTYPE html>
<html>
<head>
<title>PHP Cookie Demo</title>
</head>
<body>
<h2>PHP Cookie Handling</h2>

<p><b>Current Cookie Value:</b> <?php echo $username; ?></p>

<!-- Form to Set Cookie -->


<form method="post">
<label>Enter Username:</label>
<input type="text" name="username" required>
<button type="submit" name="set_cookie">Set Cookie</button>

120 | Page
Web Development using PHP (4341604)

</form>

<!-- Form to Update Cookie -->


<form method="post">
<label>Update Username:</label>
<input type="text" name="username" required>
<button type="submit" name="update_cookie">Update Cookie</button>
</form>

<!-- Form to Delete Cookie -->


<form method="post">
<button type="submit" name="delete_cookie">Delete Cookie</button>
</form>

</body>
</html>

A) Input/Output:

B) Write PHP script to demonstrate passing information using Session.

<?php
session_start(); // Start the session

// Set Session (Create)


if (isset($_POST['set_session'])) {
$_SESSION['username'] = $_POST['username'];
$_SESSION['email'] = $_POST['email'];
header("Location: ".$_SERVER['PHP_SELF']); // Refresh the page
exit;
}

121 | Page
Web Development using PHP (4341604)

// Retrieve Session Data (Read)


$username = isset($_SESSION['username']) ? $_SESSION['username'] : "Not set";
$email = isset($_SESSION['email']) ? $_SESSION['email'] : "Not set";

// Update Session Data


if (isset($_POST['update_session'])) {
$_SESSION['username'] = $_POST['username'];
$_SESSION['email'] = $_POST['email'];
header("Location: ".$_SERVER['PHP_SELF']); // Refresh the page
exit;
}

// Destroy Session (Logout)


if (isset($_POST['destroy_session'])) {
session_destroy();
header("Location: ".$_SERVER['PHP_SELF']); // Refresh the page
exit;
}
?>

<!DOCTYPE html>
<html>
<head>
<title>PHP Session Demo</title>
</head>
<body>
<h2>PHP Session Handling</h2>

<p><b>Current Session Data:</b></p>


<p>Username: <?php echo $username; ?></p>
<p>Email: <?php echo $email; ?></p>

<!-- Form to Set Session -->


<form method="post">
<label>Enter Username:</label>
<input type="text" name="username" required>
<label>Enter Email:</label>
<input type="email" name="email" required>
<button type="submit" name="set_session">Set Session</button>
</form>

<!-- Form to Update Session -->


<form method="post">
<label>Update Username:</label>
<input type="text" name="username" required>
<label>Update Email:</label>
<input type="email" name="email" required>
<button type="submit" name="update_session">Update Session</button>
</form>

<!-- Form to Destroy Session --> 122 | Page


<form method="post">
<button type="submit" name="destroy_session">Destroy Session (Logout)</button>
</form>
</body>
Web Development using PHP (4341604)

<!-- Form to Destroy Session -->


<form method="post">
<button type="submit" name="destroy_session">Destroy Session (Logout)</button>
</form>
</body>
</html>

B ) Input/Output:

123 | Page
Web Development using PHP (4341604)

K. Practical related Quiz.

1. In PHP, cookies are set with setcookie() function.


2. The session_start() function must appear.
a. after HTML tag c. after BODY tag
b. before HTML. Tag d. before BODY tag
3. session_unset() function is used to erase all session variables stored in the
current session.
4. Cookie is stored at Client side and Session is stored at Server side.
5. $_COOKIE superglobal variable is used to fetch information stored cookies.
6. $_SESSION supergloabl variable is used to fetch information stored in session.
7. session_destroy() function deletes only specified session.

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

You might also like