0% found this document useful (0 votes)
21 views8 pages

Session in PHP

Uploaded by

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

Session in PHP

Uploaded by

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

SESSION IN PHP

OVERVIEW
• Sessions in PHP are used to store and manage user-specific data
across multiple pages on a website.
• Unlike cookies, which are stored on the client-side, session data is
stored on the server, making it more secure for sensitive information.
Here's an overview of how to work with sessions in PHP.
• Session variables hold information about one single user, and are
available to all pages in one application.
Start a Session
• To use sessions in PHP, you must start a session using the
session_start() function at the beginning of each script that accesses
the session data.
• Example
<?php
// Start the session
session_start()
?>
Storing Data in a Session
• Once the session is started, you can store data in the session using the
$_SESSION superglobal array.
Example
<?php
session_start();
// Store session data
$_SESSION["username"] = "JohnDoe";
$_SESSION["user_id"] = 101;
?>
Retrieving Data from a Session
• you can retrieve session data the same way it was set:

Example

<?php
session_start();
// Access session data
if (isset($_SESSION["username"]))
{
echo "Username: " . $_SESSION["username"];
}
?>
Unsetting or Deleting Session Data
• you can remove specific session variables using the unset() function,
or you can destroy the entire session.
• Unset a session variable: Destroy the entire session:
<?php <?php
session_start(); session_start();
// Unset a session variable session_destroy();
unset($_SESSION["username"]); ?>
?>
Why Use Sessions?
• Security: Since session data is stored on the server, it's more secure
than cookies, which are stored on the user's browser.
• Efficiency: Sessions are useful for storing sensitive information such as
user authentication data (like login state).
• Cross-page tracking: Sessions make it easy to track data across
multiple pages without needing to pass information through query
strings or forms.

You might also like