What are sessions in PHP?

  • The session is a superglobal variable and it is an alternative way to store data across multiple pages or it preserve data across subsequent HTTP requests.
  • The session can be defined as a method to store information on the server, which can be used in the future.
  • When you work with a web server, the webserver don't know who you are, when you last work with it, what you do, etc. That means the HTTP address does not maintain the address.
  • The session introduces a problem for this problem by storing information across multiple pages.
  • If you set a session value to a variable, it can be used across multiple pages.
  • The session variable holds the information in one variable and it can be used all the web pages of the same application until the session is destroyed.
  • Session and Cookies are used to store user information.
  • Sessions are stored in the server-side machine, whereas cookies are stored in a client-side machine.
  • For example when a user login to the server, a session will create in the webserver.

Start a session in PHP


Create a session variable

  • Syntax:

    $_SESSION[variable_name] = $value;
  • Example:
    $_SESSION['user_name'] = "Ashok";

Get the session variable

   echo $_SESSION['user_name'];

To show all the session variable


To destroy a session

    //To destroy a single variable

    //To remove all the session variables

    //To destroy the session