From Computer Science Wiki
PHP Programming Language[1]

In computer science, in particular networking, a session is a semi-permanent interactive information interchange, also known as a dialogue, a conversation or a meeting, between two or more communicating devices, or between a computer and user (see Login session). A session is set up or established at a certain point in time, and then torn down at some later point. An established communication session may involve more than one message in each direction. A session is typically, but not always, stateful, meaning that at least one of the communicating parts needs to save information about the session history in order to be able to communicate, as opposed to stateless communication, where the communication consists of independent requests with responses. An established session is the basic requirement to perform a connection-oriented communication. A session also is the basic step to transmit in connectionless communication modes. However any unidirectional transmission does not define a session.[2]

used in PHP[edit]

Sessions allow the PHP script to store data on the web server that can be later used, even between requests to different PHP pages. Every session has a different identifier, which is sent to the client's browser as a cookie or as a $_GET variable. Sessions end when the user closes the browser, or when the web server deletes the session information, or when the programmer explicitly destroys the session. In PHP it's usually called PHPSESSID. Sessions are very useful to protect the data that the user wouldn't be able to read or write, especially when the PHP developer doesn't want to give out information in the cookies as they are easily readable. Sessions can be controlled by the $_SESSION superglobal. Data stored in this array is persistent throughout the session. It is a simple array. Sessions are much easier to use than cookies, which helps PHP developers a lot. Mostly, sessions are used for user logins, shopping carts and other additions needed to keep browsing smooth. PHP script can easily control the session's cookie which is being sent and control the whole session data. Sessions are always stored in a unique filename, either in a temporary folder or in a specific folder, if a script instructs to do so.[3]


This example includes 2 files, session.php and session2.php. In order to understand how sessions work, you need to pass data between files.


<form action ="session2.php" method="POST">
    <label for="studentName">Student name: </label>
    <input type="text" id="studentName" name="studentName">
    <button type="submit">Click here to add a student</button>


$studentName = $_POST['studentName'];

if(!isset($listOfStudents)) {

    $listOfStudents = $_SESSION['listOfStudents'];

$listOfStudents[] = $studentName;
$_SESSION['listOfStudents'] = $listOfStudents;

//echo "you are trying to add $studentName to our students";

foreach($listOfStudents as $i) {

    echo "$i is a student in our class <br>";

<a href="session.php">Click here to add another student</a>

See also[edit]