In this article Sessions in PHP we give the information about Sessions are a way to store user information on the server-side. It is used to store user information in a secure and encrypted form, so that the information does not have to be sent every time the user visits the browser.
Sessions and cookies are used to improve website interactions. These are technical methods to store users’ data so that the information can be maintained on each page of the website.
Sessions in PHP:
Sessions are a way to store user information on the server-side. It is used to store user information in a secure and encrypted form, so that the information does not have to be sent every time the user visits the browser.
How Sessions Work
- Start Session: The session is started using session_start().
- Set Session Variables: Use $_SESSION[] to store user information in the session.
- Retrieve Session Variables: $_SESSION[] is also used to retrieve data from the session.
- Destroy Session: Use session_destroy() when you want to destroy a session.
Example of Sessions
<?php
// Start the session
session_start();
// store the username in the session
$_SESSION[‘username’] = ‘JohnDoe’;
// get username from session
echo “Hello, ” . $_SESSION[‘username’];
// end session
// session_destroy();
?>
Cookies in PHP
Cookies are small data files that are stored on the user’s browser. This information is stored on the browser and remains available within the browser until the user clears the browser.
How Cookies Work
- Set Cookie: Cookies are created using setcookie().
- Retrieve Cookie: Use $_COOKIE[] to retrieve the cookie.
- Expire Cookie: Cookies can be set for a specific time to expire.
Example of Cookies
<?php
// set cookies
setcookie(“user”, “JohnDoe”, time() + (86400 * 30), “/”);
// display cookies
if(isset($_COOKIE[‘user’])) {
echo “Hello, ” . $_COOKIE[‘user’];
} else {
echo “Welcome, Guest!”;
}
?>
Difference between Sessions and Cookies
Sessions | Cookies |
Server-side data storage | Client-side data storage |
Data lost after session ends | Remains until user clears cache |
Secure and encrypted | Less secure (client-side) |
Uses less storage | Uses more storage |
Using Both Sessions and Cookies Together
Sessions and cookies are usually used in combination so that user information is available to both the server and the browser.
Example of Using Both
<?php
// Start the session
session_start();
// set cookies
setcookie(“user”, “JohnDoe”, time() + (86400 * 30), “/”);
//Get username from session and cookies
echo “Hello, ” . $_SESSION[‘username’];
echo “and”. $_COOKIE[‘user’];
?>
Conclusion
- Sessions: Used to store server-side data. These are secure and encrypted.
- Cookies: Store client-side data. These are kept safe on the browser.
- By combining both, it is easier to maintain user information on the website.
Some More:
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System
Join Now: Data Warehousing and Data Mining