Welcome to YLOAN.COM
yloan.com » other Phones » Mobile Web Development using jQTouch:Session Management
Nokia Sony Ericsson Motorola HTC Sam Sung Panasonic Acer LG Sharp Toshiba BlackBerry Google android ringtone other Phones

Mobile Web Development using jQTouch:Session Management

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.

For more information, refer to my book: "Beginning Web Development for Smartphones: Developing Web Applications with PHP, MySQL and jQTouch" available at http://www.amazon.com/Beginning-Web-Development-Smartphones-ebook/dp/B0042X9CHI/ref=sr_1_2?ie=UTF8&m=AH9CGK6QR37LL&s=digital-text&qid=1284780488&sr=8-2

Mobile Web Development using jQTouch:Session Management

By: B.M. Harwani
Mobile Phone Contracts: Communication And Entertainment At Reasonable Rates 3 Mobile Offers Pleasing Your Pocket And Mind Instant Text Loans - Keep The Phone For Extra Credit Support Cordless Phones: Communicate Freely Without The Cords Contract Phone Deals - Attracts the Crowd Cell Phone Registry Free Mobile Phone Deals: Compare To Get The Best Aarkstore Enterprise--- Mobile Handsets And Applications September 2010 Apple Iphone 4 White Deals : Free With Vodafone, T-mobile And Orange Mobile Phones Make Your Christmas D Best Best Mobile Phone Deals: Reduces The Stress of The User Mobile Phone Deals With The Maximum Utility What is the difference between Common cell phone system and GSM system?
print
www.yloan.com guest:  register | login | search IP(216.73.217.87) California / Rosemead Processed in 0.020250 second(s), 5 queries , Gzip enabled , discuz 5.5 through PHP 8.3.9 , debug code: 62 , 4440, 97,
Mobile Web Development using jQTouch:Session Management Rosemead