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
Step 1: session_start()
The session is started using session_start().
Step 2: $_SESSION[name]=value;
To store user information in the session like set session name and value
Step 3: Retrieve Session Variables and value
echo $_SESSION[name];
It is also used to retrieve data from the session.
Delete Session:
Step 1: session_unset();
Remove all session variables
Step 2: session_distroy();
When you want to destroy a session.
Example of Sessions
Cookies in PHP
Cookies in PHP are small files that a web server stores on a client computer. Cookies are used to identify a user and provide personalized content.
- Cookies allow a website to remember information about your visit.
- This can make it easier for you to use the website next time.
- Cookies also help analyze web traffic.
- In PHP, you can set cookies using the setcookie() or setrawcookie() function.
To set cookies in PHP:
Syntax:
setcookie(name, value, expire, path, domain, secure, httponly)
where,
- $name – The name of the cookie to create to store the cookie data.
- $value – The value to set or update in the cookie.
- $expires_or_options – The expiration time for how long the cookie data will be accessible.
- $path – The path or URL where to access cookie data.
- $domain – The domain for which to set the cookie.
- $secure – Whether cookie data will be accessed via http or https.
- $httponly – Whether the cookie can only be accessed via http requests.
Example of Cookies
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