subject: Mobile Web Development using jQTouch:Session Management [print this page] Beginning Web Development for Smartphones: Developing Web Applications with PHP, MySQL and jQTouch Session Management
No web application is complete without session management a concept that helps track visitors to our web application.
HTTP is a stateless protocol, so the interaction between browsers and web servers is stateless. Each HTTP request a browser sends to a web server is independent of any other request. The stateless nature of HTTP allows users to browse the web by following hypertext links and visiting pages in any order. HTTP also allows applications to distribute or even replicate content across multiple servers to balance the load generated by a high number of requests.
Sometimes though, we need to remember specific data between web pages, for example, cart items, the user's area of interest, login information, and so on. Otherwise, the user will be asked to enter the same information each time a web page is revisited.
What Is a Session And How Does It Start?
A session is a combination of a server-side file containing all the data we wish to store, and a client-side cookie containing a reference to the server data. The file and the client-side cookie are created using the PHP function session_start().
session_start()
This function initializes session data. It creates a session or resumes the current one based on the current session id. It has no parameters, but informs the server that sessions will be used.
Syntax
session_start()
When we call session_start(), PHP checks to see whether the user sent a session cookie. if the answer is yes, PHP loads the session data. Otherwise, PHP creates a new session file on the server, and sends an ID back to the user to associate the user with the new file. Because each user has unique data locked away in their personal session file, we need to call session_start() before trying to read session variables. We will soon see the role of session variables in passing data from one page to another.
session_id()
This function is used to get or set the session id for the current session.
Syntax
string session_id ([ string $id ] )
session_id() returns the session id for the current session, or the empty string ("") if the session does not exists. If an id is specified, it will replace the current session id.
To make the concept clearer, have a look at the following code:
session_start();
$sessid=session_id();
$_SESSION['userid'] = $_POST['userid'];
$_SESSION['password'] = $_POST['password'];
?>
Here we see that first the session is started and the session id is stored in the $sessid variable . The session id will be used for tracking the user on different web pages of the same application. After that, we set the array elements of $_SESSION array. Data entered in the form's userid and password input fields in the web page calling the PHP script is collected using $_POST array. The data is then sent to the$_SESSION array and stored in the $_SESSION[userid'] and $_SESSION[password'] elements.
On any other web page of the same web application, we can fetch the information stored in the $_SESSION array. The following code further illustrates the concept:
session_start();
$uid=$_SESSION['userid'];
$pwd=$_SESSION['password'];
?>
Here, after starting the session, the data stored in the $_SESSION[userid'] and $_SESSION[password' elements of the $_SESSION array is retrieved and stored in the $uid and variables. These variables may be used in the current web page. This way, we can pass data across the web pages.