Category: PHP

JSON Web Token

There are basically two different ways of implementing server side authentication for apps with a front end and an API:

Session Based System

In session based system like social networking system, e-commerce system or net banking system, the user data are stored on server side in a text file in temporary directory when user logged into the system which is called session data. The server generates the unique session ID for each user’s session data and send back to the user.

User can store that Session ID in cookies so that it can be used for further communications. When user requests for particular data after login, user has to send the session ID with each request from the cookie, so server can authenticate the user. And according to that session ID, server gives response to that user.

Session based system is STATEful as the server has to store the session for each user at server side and also user has to store session id in cookies. So if there are number of users then it is very difficult to maintain scalability and performance of the system and it will use extensive memory.

To overcome this scenario, token based system comes into the pictures.

Token Based System

Token based system is stateless, means no user data are stored on the server side, only user has to store token at client side which improves the performance and scalability of the system. When user sends the subsequent request after login, user has to provide token to the server. And from the token, server identify the user and give response to user.

Read More

Getting Started with Caching in PHP using memcached

Caching is used to store the data which are frequently used. The main advantage of caching is that it increases the performance of the site or an application by decreasing the load on to the server like database.

There are different caching methods to cache the data like caching content in file, memory or database. Memcached is one type of memory caching system. It is very useful when large number of data you want to cache.

It is distributed memory caching system. Memcached is key-value store for caching small chunks of data from result of database, API or page rendering. Basically it can cache anything if you can turn it into key => value. Memcached solves many problems related to large data caching.

Memcached is better because of the following reasons:

  1. It’s REALLY fast – Everything’s in memory, and it’s highly optimized for situations just like yours (and caching in general )
  2. It’s distributed – This means that if you have multiple web / app servers running, they can all access the same cache
  3. You can pool multiple servers for memcached – If you’ve got a few servers that are relatively underutilized (or several dedicated cache servers), you can pool them all together into one big cache
  4. It’s super-scalable (for the reasons mentioned prior)
  5. It’s got great PHP support – The PECL package for memcached was recently updated with a lot of new goodness
  6. You can even store your user sessions in memcached – just set it up in your php.ini file. This is much faster than storing sessions in databases, and allows your sessions to persist across multiple web hosts (if you’re in a load balanced situation). This will also give your site a bit of a performance boost as there’s no need to hit the file system / database for session info on every request.

What memcached does?

Read More