What is session variable in php?

How to create session in php?

What is the use of session?

How to use session in php?

Description about session is bellow:

A PHP session is also One type of variable..
Which is used to store information.
Session variables Store the information about single user, And In the one Application all pages are available.

PHP session variable

Always we working with any application,
We just open it,do something work and closed it.
computer knows evrything what you do.
But server has a oine problem.
It’s don’t know actually what yoiu do? It’s doesn’t metter.
That’s why server need session.

sessions work by creating a unique id for each visitor and store variables based on this uniqe id.
The UID is stored in a cookie or either is propagated in the URL..

How to start php session?

Note: The session_start() function must appear before the tag:


<?php session_start(); ?>

<html>
<body>

</body>
</html>

How to store session variable?



<?php
session_start();
// store session data
$_SESSION['views']=1;
?>

<html>
<body>

<?php
//retrieve session data
echo "Pageviews=". $_SESSION['views'];
?>

</body>

Below example, we create a simple page-views counter.
The isset() function checks if the “views” variable has already been set.

<?php session_start(); if(isset($_SESSION['views'])) $_SESSION['views']=$_SESSION['views']+1; else $_SESSION['views']=1; echo "Views=". $_SESSION['views']; ?>

How to Destroying a Session?

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

The unset() function is used to free the specified session variable:



<?php
session_start();
if(isset($_SESSION['views']))
unset($_SESSION['views']);
?>

If you want to completly destroy the session So, you can
By calling session_destroy() function:



<?php
session_destroy();
?>

About the Author

avatar