Sessions

So if you want to access a website, you need a browser, the URL or IP of the website and that is it from your side. Once you pass the URL on to the address bar and press ENTER, you are navigated to the desired page, which is loaded with your information, you think to yourself the website really knows you. Any idea how that happened? If ‘no’ then keep reading further you will surely get an idea, and if ‘yes’ still keep reading I promise it will be worth it.

Browser is a client that request server to serve requests. This request is from browser to the server is sent over HTTP( Hyper Text Transfer Protocol).

HTTP is STATELESS

Now, what do I mean by STATELESS. I mean that the server does not retain any information about the status or communication between the client and the server.

blog

So if you want to keep a record of you who did what on your website you will have to implement it, and you do so with Sessions.

Sessions keep a track of state between the activity in the browser(client) and the site(server). The session attribute is a dictionary like object and stores data in a ‘key:value’ pair and is stored on the server-side.

To access session attribute you can use request parameter. Session attribute represents the current user on the website.  Below are ways to set the session value, retrieve or delete a value.

# Set a session value:
request.session["fav_color"] = "red"

# Get a session value 
fav_color = request.session["fav_color"]

# Clear an item from the session:
del request.session["fav_color"]

You can also use dictionary operations like keys(), items() on request.sessions. Sessions are global variables, they contain information that you want to persist throughout the users interaction with the website. Each session has an ID its call Session ID which is passed to the server every time the user make requests.  The web application pairs this session id with it’s internal database and retrieves the stored variables for use by the requested page.

Capture

Every time a new user hits the application a session id is assigned to him. Closing the browser destroys the session and reopening creates a new session.

Session: How it works

  1. User hits url with a session id if available – and tells “Hi it’s me”. Session ID’s are mostly stored in cookies or sent via GET or POST requests.
  2. The server checks if the ID is available in the SESSION table if yes it goes forward and gives it access to the code engine(Django code/ php)
  3. Then server processes the user requests by hitting the application and passing the user id.
  4. The app then uses the user id to get data from the database
  5. The app then loads the data in the required way on the front-end
  6. And finally user can see the data.UntitledThis is Session.

 

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s