Welcome: Guest
Intro to Stateless Web and Sessions
By: Eli
3/26/10 (87 months ago)
Avatar
Eli Administrator
Joined: 3/17/10
Posts: 80

Sessions


One of the unique challenges while programming on the web compared to desktop programming is that the web is stateless. That means that on every page view throughout a user’s visit on a website the server forgets who the user is and everything they were doing previously. In a desktop application, a standard program flow would be:

Log in
Do some things that require being logged in
Log out


The web is stateless, so that makes this task difficult. Sessions were created to help provide this ability to web applications. We need some way to store information about a user while they are browsing the site. So how does a session help? Let’s take a look at what a session is.


What is a session?



A session is a way to store information about a user across many page requests. Can’t this be done with GET? Can’t we store user information in the url:
index.php?name=Bill&favcolor=Green
? Yes, user information could be stored like this, although it significantly limits our ability to store bigger items. For example, Microsoft’s Internet Explorer limits URL length to 2,083 characters so we would be unable to store a large amount of information in the URL. The URL also does not allow us to store objects and arrays. If instead of one favorite color Bill had a list of favorite colors, we would have a hard time storing the list, as well as accessing it in our code. Enter Sessions.

How do sessions work?


While using sessions, PHP creates a unique file on the server that you can use to store information. These files are temporary, and have a unique name that you need to know to access them. Now instead of keeping track of a large amount of information across many page requests, we can store that information on the server and keep track of only one string. There are two ways to store this string, in the URL or in a cookie. Most sessions are cookie based and servers generally have cookie based sessions set as default. We will talk more about cookies later.

How do sessions work?


While using sessions, PHP creates a unique file on the server that you can use to store information. These files are temporary, and have a unique name that you need to know to access them. Now instead of keeping track of a large amount of information across many page requests, we can store that information on the server and keep track of only one string. There are two ways to store this string, in the URL or in a cookie. Most sessions are cookie based and servers generally have cookie based sessions set as default. We will talk more about cookies later.


Using $_SESSION


To start using a session, you must add
session_start()
at the top of the files you want to have access to the session information. It must be called before any output is sent to the screen or PHP will give you a header error saying that output has already been sent.

Set and Get information from the Session Array


<?php
session_start();

$_SESSION['name'] = "Fred";
echo $_SESSION['name'];
?>


As a simple example, let’s make a page that displays two different strings on every page refresh. It will do this reliably and for each user independently.

<?php
session_start();

if (isset($_SESSION['string']) && $_SESSION['string'] == "pizza")
    $_SESSION['string'] = "cookies";
else
    $_SESSION['string'] = "pizza";

echo "I like ".$_SESSION['string']."!";
?>


Here we check if we have defined a variable called ‘string’ in our session, and if the string is set to ‘pizza’, we change it to ‘cookies’ and vice versa. We then echo the current value of the session variable. If two people are accessing the page at the same time, it won’t mess up the script since each user has their own unique session.

We can store whatever we want in a session variable, not just strings.

<?php
session_start();

$favColors = array("green", "blue", "teal", "white");

$_SESSION['colors'] = $favColors;

print_r($_SESSION['colors']);
?>


This concludes our introduction to sessions and how to combat the stateless web. Hopefully you are now able to harness the ability to save information across multiple page views to create an interactive application.
Last Edited: 3/26/10 (87 months ago)