PHP Session

Posted on Updated on

PHP-WEB-FEATURES-SESSIONSv1What is a PHP Session?

When you work with an application, you open it, do a few changes, and after that you close it. This is much like a Session. The PC knows who you are. It knows when you begin the application and when you end. Be that as it may, on the web there is one issue: the web server does not know who you are or what you do, in light of the fact that the HTTP address doesn’t keep up state.

Session variables take care of this issue by putting away client data to be utilized over numerous pages (e.g. username, most loved shading,and so forth). As a matter of course, session variables last until the client shuts the program. So; Session variables hold data around one single client, and are accessible to all pages in one application.

Note Tip: In the event that you require a perpetual stockpiling, you might need to store the information in a database.

Begin a PHP Session

A session is begun with the session_start() capacity.

Session variables are set with the PHP worldwide variable: $_SESSION.

Presently, how about we make another page called “demo_session1.php”. In this page, we begin another PHP session and set some session variables:

<?php

/Begin the session

session_start();

?>

<!DOCTYPE html>

<html>

<body>

<?php

/Set session variables

$_SESSION[“favcolor”] = “green”;

$_SESSION[“favanimal”] = “feline”;

reverberation “Session variables are set.”;

?>

</body>

</html>

Note: The session_start() capacity must be the first thing in your archive. Prior to any HTML labels.

Get PHP Session Variable Qualities

Next, we make another page called “demo_session2.php”. From this page, we will get to the session data we set on the first page (“demo_session1.php”).

Notice that session variables are not passed separately to each new page, rather they are recovered from the session we open toward the start of every page (session_start()).

Additionally see that all session variable qualities are put away in the worldwide $_SESSION variable:

Sample

<?php

session_start();

?>

<!DOCTYPE html>

<html>

<body>

<?php

/Reverberation session variables that were determined to past page

reverberation “Most loved shading is ” . $_SESSION[“favcolor”] . “.<br>”;

reverberation “Most loved creature is ” . $_SESSION[“favanimal”] . “.”;

?>

</body>

</html>

Another approach to demonstrate all the session variable qualities for a client session is to run the accompanying code:

Illustration

<?php

session_start();

?>

<!DOCTYPE html>

<html>

<body>

<?php

print_r($_SESSION);

?>

</body>

</html>

Note How does it work? How can it know it’s me?

Most sessions set a client key on the client’s PC that looks something like this:

765487cf34ert8dede5a562e4f3a7e12. At that point, when a session is opened on another page, it examines the PC for a client key. In the event that there is a match, it gets to that session, if not, it begins another session.

Adjust a PHP Session Variable

To change a session variable, simply overwrite it:

Sample

<?php

session_start();

?>

<!DOCTYPE html>

<html>

<body>

<?php

/to change a session variable, simply overwrite it

$_SESSION[“favcolor”] = “yellow”;

print_r($_SESSION);

?>

</body>

</html>

Annihilate a PHP Session

To uproot all worldwide session variables and wreck the session, use session_unset() and session_destroy():

<?php

session_start();

?>

<!DOCTYPE html>

<html>

<body>

<?php

/uproot all session variables

session_unset();

/demolish the session

session_destroy();

?>

</body>

</html>

For more details and queries please feel free to email, visit or call us. Wishing you the very best for all your future endeavors.

Helpline: 9814666333, 8699444666

Email:info@technocampus.co.in

**************************************************************************************************************

Please fill the FORM and we shall contact you soon.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s