PHP Session Variables - php basics -

PHP Session Variables

PHP Summer Training/Internship with 2 Live Projects in Noida Fee is only 5500/-Click here to Book Your Seat


Use of Session and Cookie in PHP

You already know that the Hypertext Transfer Protocol(HTTP) is the standard protocol used to transfer data between your browser and the various Web sites you visit.

What you may not know, however, is that HTTP is a “Stateless” Protocol, which treats each request for a Web page as a unique and independent transaction, with no relationship.

To work around this problem, most Web sites use cookies or sessions to “maintain state,” in order to offer enhanced services.

for example, shopping carts or automatic restoration of personal settings — to site users.

Why Session is more secure than cookies

You know what cookies are: text files stored on a user’s system that help a Web site recognize the user and retrieve specific information about him or her.

The problem with cookies is that they’re not very secure: because ther are stored on the client, it’s possible for any user to open the cookie file and read or modify the information stored within it.

That’s why many Web sites prefer to use sessions . sessions work much like cookies, except that the information used to maintain state is stored on the server, rather than on the client.

Creating Session environment and Session Variables

In a Session-based environment, every client is identified through a unique number called session identifier and this unique number is used to link each client with its information on the server.
Every time the client visits the Web site the site reads the client’s session identifier and restores state information.

The session_start( ) function is used to start session environment. Session variable always works its own(session) environment or its associated environment

Note : The session_start( ) function must appear BEFORE the <html> tag.
Try to use session_start( ) at top of the PHP script.

Storing a Session Variable

To store and retrieve session variables use the $_SESSION [ ].

save it stroreSession.php

check output on retrieveSession.php
Enter your user name
Enter your profile

Retrieving a Session Variable

To retrieve session variables first start session environment.

save it retrieveSession.php

Welocme abhi
Your profile is developer

Destroying a Session Variable

If you want to delete some session data, you can use the unset( ), session_unregister( )
or the session_destroy( ) function.

The unset( ) and session_unregister( ) function is used to free the specified session variable.
while session_destroy( ) function completely destroy the session environment.

save it stroreSession.php

check output on retrieveSession.php
Enter your user name
Enter your profile


Your profile is developer

Latest Trending Technologies