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.
- 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.
echo "Number of views on page= ".$_SESSION['pageviews'];?>
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.
If you want to completely finish a session, use session_destroy() function.