0% found this document useful (0 votes)
18 views2 pages

Sessions

Uploaded by

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

Sessions

Uploaded by

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

In PHP, a session is a way to store and retrieve data across multiple requests from the same user.

It
allows you to maintain user-specific information, such as login status, shopping cart items, or user
preferences, throughout their browsing session on your website.

Here's a basic overview of how sessions work in PHP:

Starting a Session: To start a session, you need to call the session_start() function at the beginning of
each PHP page where you want to use session variables. This function initializes or resumes a session
and makes the session data available for use.

Storing Session Data: Once a session is started, you can store data in session variables. Session variables
are accessed using the $_SESSION array. For example, to store a user's name in a session variable, you
can do:

$_SESSION['username'] = 'Abhishek';

You can store any type of data in session variables, including strings, numbers, arrays, and objects.

Accessing Session Data: To access session data on subsequent pages, you need to start the session using
session_start() and then access the session variables through the $_SESSION superglobal array. For
example, to retrieve the stored username:

session_start();
echo $_SESSION['username'];

If the user has not started a session or the session data is not set, accessing the session variables will
return NULL.

Destroying a Session: When a user logs out or when a session is no longer needed, you can destroy the

session_destroy(); // Destroy the session

Sessions in PHP provide a simple and convenient way to store user-specific information. However, it's
important to handle session security properly to prevent session hijacking or session fixation attacks.
You should use secure session handling techniques, such as regenerating the session ID on
authentication or after a certain period, using HTTPS for secure communication, and properly sanitizing
and validating session data. Here's an example of a PHP program that demonstrates how to start a
session:

<?php
session_start(); // Start the session
// Store data in session variables
$_SESSION['username'] = 'Abhishek';
$_SESSION['email'] = '[email protected]';
echo 'Session started.';
// Display session data
echo 'Username: ' . $_SESSION['username'];
echo 'Email: ' . $_SESSION['email'];
?>
In this program, we start the session by calling session_start(). Then, we store the user's username and
email address in session variables $_SESSION['username'] and $_SESSION['email'], respectively. Finally,
we display the session data by echoing the values of the session variables.
……………………………..
To access stored session data from another page in PHP, you need to follow these steps:

● Ensure that you have started the session using session_start() at the beginning of both pages.

● Store session data on one page, and access it on another page.

Here's an example of how you can access stored session data from another page:

Page 1: store_session_data.php

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

// Store data in session variables


$_SESSION['username'] = 'Abhishek';
$_SESSION['email'] = '[email protected]';
echo 'Session data stored successfully.';
?>

Page 2: access_session_data.php

<?php
session_start(); // Start the session
// Access session data from the previous page
$username = $_SESSION['username'];
$email = $_SESSION['email'];
// Display session data
echo 'Username: ' . $username;
echo 'Email: ' . $email;
?>

In this example, both pages start the session by calling session_start() at the beginning. On
"store_session_data.php," we store the session data by assigning values to $_SESSION['username'] and
$_SESSION['email']. On "access_session_data.php," we access the session data by retrieving the values
of $_SESSION['username'] and $_SESSION['email'] into local variables $username and $email,
respectively. Finally, we display the session data by echoing the values of these variables.

Access "store_session_data.php" first to store the session data, and then access
"access_session_data.php" to see the stored session data being displayed on the second page.

You might also like