RahulB
RahulB

Reputation: 21

Codeigniter : Create a new session whenever internet connection breaks at client end

We have developed a small application for conducting online exams. We are using CodeIgniters session handling capabilities to manage examinee’s session and it works perfectly fine if the internet connection doesn’t break during the examination. However in India in certain cities the internet connectivity is not that reliable and the internet connection breaks during the exams. At this time although the application stores the exam’s state, we would like to have the user session to expire as soon as there is any loss in internet connectivity.

With codeigniters session handling it compares the session information in client cookie with the one in database and if they match it allows the user to continue in the same session.

Is there a way by which we can create a new session as soon as there is break in internet connection, similar to what we have for banking applications? I’m a novice as far as php & codeigniter is considered, so please pardon if I ask some very basic questions.

Upvotes: 2

Views: 598

Answers (1)

thpl
thpl

Reputation: 5910

The Problem

Sessions are created and maintained serverside. That means that as soon as your client is not able to connect to the server anymore there is no way to manipulate session information.

Local Storage

With HTML5 a new interface was introduced that is able to take care of your problem. Basically local storage is a key-value storage that persists data. So speaking, it will be available beyond page refreshs.

Now, whenever your application notices that it will not be able to reach the server you need to allocate your data flow to the local storage.

When the server can be reached again, you can send the data from your local storage to the server and check if there were no manipulation attempts on the client side.

Using the Local Storage

In it's most basic form working with the local storage is really simple. You're just setting key/value pairs and you are retrieving the keys.

localStorage.setItem('name', 'thomas'); //Drop something into the local storage
localStorage.getItem('name'); //Retrieve something from the local storage

Of course you need to check for availability of the local storage. Also the implementations differ in various browsers (so the maximum size of data that can be stored can be different for different browsers).

Wrapping it up

It's really all about allocationg your data flow. So the basic approach would be:

  1. Before submitting data from client to server check if internet connection is available
  2. If everything is alright, there is no need to use the local storage.
  3. However, if you cannot reach the server, store the data you were about to send to the server into the local storage
  4. The next time you're going to submit data check for connectivity again. If you can reach the server now. Send the data from the local storage to your server.
  5. The server processes the data and does some validation on it (so you can be sure it hasn't been manipulated)
  6. In the best case your users will not even notice that the server was not reachable.

Upvotes: 1

Related Questions