Sessions in PH1pdf Sep 2024
Sessions in PH1pdf Sep 2024
Sessions is used to store and pass information from one web page to another
temporarily until the user close the website. In case of cookie, the in formations are stored in
user computer. But in case of session information is not stored on user computers.
PHP Sessions technique is widely used in shopping websites where we need to store
and pass Cart information. For example, user name, product name, product price, etc. from
one page to another.
PHP Sessions creates unique user ID for each browser to recognize the user and avoid
conflict between multiple rowsers.
User 1 Session ID
On Client 1 1. 2 Creates unique
1.1 Request
Session ID
Container
SERVER
User 2 Session ID
On Client 2
A Session is started with the session_start() function. Session variable are set with the
PHP Global variable $_session.
PHP session_start() function:
PHP session_start() function is used to start the session. It starts a new or resumes
existing session. It returns existing session if created already. If session is not available, it
creates and returns new session.
Syntax:
Bool sesson_start(void) ;
Example:
Session_start();
PHP $_session :
PHP $_session is an associative array that contains all session variables. It is used to
set and get session variable’s value.
Example:
To store information:
$_session[“user”] = “SRI” ;
To get information:
echo $_session[“user”] ;
Note: The session_start() function must be the very first thing in our document before
any HTML tags.
<?PHP
Session_start() ;
?>
<HTML>
<BODY>
<?PHP
$_session[“user”] = “SRI” ;
?>
</BODY >
</HTML>
<?PHP
Session_start() ;
?>
<HTML>
<BODY>
<?PHP
</BODY >
</HTML>
<?PHP
$_session[“user”] = “DEEPIKA” ;
Destroying Session:
<?PHP
Session_start();
Session_destroy();
?>
To remove all Global Session Variable and destroy the Session, We use session_unset()
function and session_destroy() function to destroy all session variables completely.
<HTML>
<BODY>
<?PHP
Session_unset() ;
Session_destroy() ;
?>
</BODY>
</HTML>
***
- Cookies
- URL parameter
The Session module supports both methods. Cookies are optional, but because they
are not always available. We also provide an alternative way. The second method
embeds the Session ID directly into URLs.
PHPSESSID = b123123123123123123123123
- This Means that we can code the links in our pages to include the session ID, if
available:
<?PHP
Session_start();
?>
<A href=”MyScript.PHP ? <?PHP echo SID ; ?> “ > Home Page </A>
If the session ID was successfully stored in a browser cookie, the preceding code will
output:
If PHP can’t create the Session Cookie, the code will output something along the lines
of
<A href = “MyScipt.PHP ? PHP SES SID = “b123123123123123123123123”> Home Page </ A>
When the user clicks to view MyScript.PHP, the PHPSESSID query string value is
automatically picked up by the PHP engine and the Session data is made available to the
script.
We need to have called session_start() before trying to access the SID Constant.
***
Ref
Sessions:
Ref
- Sessions are Sever Side Files that store the user information, whereas the
Cookies are Client Side Files that contain user information on a local computer.
- Sessions are cookies dependent. Whereas cookies are not dependent on session.
- The Session end when the user closes the browser or logout from the
application, whereas Cookies expire at the set time.
- A Session can store as much data as a user want, whereas Cookies have a limited
size of 4 KB.
Sessions Cookies
1. A Session stores the variable and their 1. Cookies are stored on the user’s
values within a file in a temporary computer as a text file.
directory on the Server.
2. The Session ends when the user 2. Cookies end on the life-time set by
logout from the application or closes the user.
his web browser
8 Sessions are more secured compared 8. Cookies are not secured, as data is
to cookies, as they save data in stored in a text file and if any un-
encrypted form. authorized user gets access to our
system and he can temper the data.
Conclusion:
From the above discussions, we can have a better understanding of Cookies and
Sessions, and the differences between them. Hence, we can conclude that Sessions is a way
to temporarily store the user information on the Server side, whereas Cookies store the
information on the user’s computer until it expires.
***
Suppose that we have created an online community, or portal, or some other type of
application that user can join. The process usually involves a Registration Form, whereas the
user crates a User Name, and Password, and completes an identification profile. From this
point, forward each time when a Registered User lays into the system, we can grab the user’s
identification information and store it on the user’s session.
The items ae decide to store to user’s session should be those items we can imagine
using quite a bit and that could be inefficient to continually extract from the database. For
example, suppose that we have created portal in which user’s are assigned a certain level,
such as Administrators, Register User, Guest and so on. Within our display modules, we would
always want to check to verify that the user accessing the module has the proper permissions
to do so. Thus, user level is an example of a value stored in the user’s session. So, that the
authentication script used in the display of the required module only has to check a session
variable, and there is no need to connect to select and query the database.
We can store each of those functional elements in a session. When the user logs in
the application loads all relevant values into the user’s session and reacts accordingly for each
subsequently requested page. Should the user decode to change their preferences, they could
do so while logged-in. We could even pre-populate preferences from based on the items
stored in the session instead on the items stored in the session instead of going back to the
database to retrieve them. If the user changes a preferences while the user logged-in, simply
replace the value stored in the $_session super global with the new session. No need to force
the user to logout and then log-back in again.
***