Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

php practical 13

The document outlines a laboratory experiment for a Mobile Application Development course, focusing on PHP programming for managing cookies and sessions. It includes practical questions about initiating sessions, registering variables, and differences between sessions and cookies, along with example PHP code for both functionalities. Additionally, it discusses session expiration and cookie management techniques.

Uploaded by

aafu202
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

php practical 13

The document outlines a laboratory experiment for a Mobile Application Development course, focusing on PHP programming for managing cookies and sessions. It includes practical questions about initiating sessions, registering variables, and differences between sessions and cookies, along with example PHP code for both functionalities. Additionally, it discusses session expiration and cookie management techniques.

Uploaded by

aafu202
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

DEPARTMENT OF COMPUTER ENGINEERING

Subject: Mobile Application Development Subject Code: 22619


Semester: 6 Course: CO6I-A
Laboratory No: Name of Subject Teacher:
Name of Student: AFIF IRFAN NAZIR Roll Id: 21203A1005
Experiment No: 13
Title of Experiment Write simple PHP program to 1. Set cookies and read it 2.
Demonstrate session management

 Practical Related Question


1. How to initiate a session in
PHP ?
To initiate a session in PHP, you can use the session_start() function at the beginning of your
script.

2. How to register a variable


in PHP session ?
To register a variable in PHP session, you can simply assign a value to a key in the
$_SESSION superglobal array.
3. What is the difference
between PHP session and
Cookie ?
Both PHP sessions and cookies are used to maintain state and store information about a user
between page requests, but there are some differences between them:

Data Storage: Cookies store data on the client-side, whereas sessions store data on the server-
side. This means that cookies can be accessed and modified by the user, while session data is
more secure and can only be accessed by the server.

Data Size: Cookies have a size limit of around 4KB, while session data can be much larger.
Expiration: Cookies have an expiration time that can be set, so they can persist across
multiple sessions. Session data, on the other hand, is destroyed when the session ends (either
because the user logs out or the session times out).

Security: Session data is more secure than cookies because it's stored on the server and can't
be tampered with by the user.

Accessibility: Cookies can be accessed from any browser on any device, while session data
is only accessible on the server that created it.
4. What is the difference
between session_unregister
() and session_unset()?
The session_unregister() and session_unset() functions are both used to clear session
variables in PHP, but they work in slightly different ways.

The session_unregister() function is used to unregister a specific session variable, so that


it is no longer available in the session. It takes one argument, which is the name of the
variable to unregister.

5. What is the default session


time in PHPH?
The default session time in PHP is 24 minutes (1440 seconds). This means that if a user is
inactive for more than 24 minutes, their session will expire and they will need to log in again
to continue using the website or application.

This default value can be changed by modifying the session.gc_maxlifetime directive in the
php.ini configuration file or by using the ini_set() function in your PHP code to set the value
dynamically.
6. Is it possible to destroy a
cookie ()?
Yes it is possible you need set the expiration date of the cookie.
 Exercise Related Question
1. Write a program that demonstrate use of cookies
<?php
// Set a cookie with a name, value, and expiration time of one
hour
setcookie("username", "John Doe", time()+3600);

// Check if the cookie is set


if(isset($_COOKIE["username"])) {
// Print the value of the cookie
echo "Welcome " . $_COOKIE["username"] . "!<br>";
} else {
// Print a message indicating that the cookie is not set
echo "Sorry, we couldn't find your username.<br>";
}

// Modify the value of the cookie


$_COOKIE["username"] = "Jane Doe";

// Print the new value of the cookie


echo "Your username has been updated to " .
$_COOKIE["username"] . ".<br>";

// Delete the cookie by setting its expiration time to the past


setcookie("username", "", time()-3600);

// Check if the cookie is deleted


if(isset($_COOKIE["username"])) {
// Print a message indicating that the cookie still exists
echo "Sorry, we couldn't delete your username.<br>";
} else {
// Print a message indicating that the cookie has been deleted
echo "Your username has been deleted.<br>";
}
?>
2. . Write a PHP program that demonstrate use of session
<?php
session_start();

if(isset($_SESSION['count'])) {
$_SESSION['count']++;
} else {
$_SESSION['count'] = 1;
}

echo "You have visited this page ".$_SESSION['count']."


time(s).";
?>

You might also like