• Have Any Question?
  • contact@eitworld.com

PHP Sessions





PHP Session provides the facility for storing the information which can be used across the webpages. Session In PHP is that if you log in some site and spend some time on it and after some time you logged out from site then time duration from login and logout is nothing but it is a session. In PHP you can start a session by using the seesion_start() function and can use the session for storing the information in the session variables using $_SESSION[“variableName”];

You can learn PHP Session from eitworld.com in easy world.

  • The time between a user started a work and ended the work is called a session. The number of times he repeated this process i.e. starts the work and ends it is the number of sessions. When you start working on server, it may be assumed that a session is started and that particular session ends when you end your work on server.
  • To count the number of sessions, to store the information that the user changed or modified in that particular time, session variable is used. Session variables can be used to count the page views of a particular page.

Starting a Session

  • Before using session variables, a session must be started.
  • Once a session has been started, it keeps going until it is destroyed manually. Destroying a session is described further in this chapter.
  • session_start() function is used to start a session.
  • This function must be written above the <html> tag.

<?php
session_start();

?>

<html>

</html>

Storing Information

  • After a session has been started, information can be saved on the server for this session.
  • Session Variable is used to store the information.
  • $_SESSION[‘variableName’] is used to access the session variable.

<?php
session_start();

?>

<html>

<body>

<?php

if(isset($_SESSION[‘pageviews’]))

{

$_SESSION[‘pageviews’]=$_SESSION[‘pageviews’]+1;

}

else

{

$_SESSION[‘pageviews’]=1;

}

echo “Number of views on page= “.$_SESSION[‘pageviews’];?>

</body>

</html>

Output

session

Above program starts a session. Then it checks if the session variable pageviews has already been set. If it is already set, it increases the value of the variable by one. And if the variable has not been set, it initializes it to 1.

Destroying a Session

If you want to unset a particular session variable in a session, unset() function can be used.


<?php
session_start();

if(isset($_SESSION[‘pageviews’]))

{

unset($_SESSION[‘pageviews’]);

}

?>

If you want to completely finish a session, use session_destroy() function.



Real Time Web Analytics