Thursday 5 June 2014

Session & Cookies in php.


Session in php-

A “session” is set for maintaining the user data as the person is browsing through the site. A session is very useful in e-commerce websites, social networking sites etc. In PHP, session variables are used to set the sessions. Anything can be set / stored in a session like the user’s id, username, some encrypted password string etc.

A session is always stored in the server. You lose all the session data once you close your browser, as every time you re-open your browser, a new session starts for any website.

Example: $_SESSION[‘customer_name’] = ‘Amit’;


Cookies in php-



A “cookie” is however different from a session. It stores some information like the username, last visited pages etc. So that when the customer visits the site again, he may have the same environment set for him. You can store almost anything in a browser cookie.

A cookie is stored in the client’s browser.

Like, when you check the ‘Remember Password’ link on any website, a cookie is set in your browser, which exists there in the browser until manually deleted. So, when you visit the same website again, you don’t have to re-login.

But, it can also be problematic at times, if the user has blocked cookies in his browser and you have a shopping site that utilizes cookies to store the data. Then, that person will never be able to shop from your website.

setcookie("username", "John", time()+2400);

There are some basic points in  session and cookies that are following:-

1) Session are temporary and Cookies are parmanent.
2)  Session data is store on server while Cookies are store on user's computer.
3) Cookies contents can be easily modify but to modify Session contents is very hard.
4 ) Cookies could be save for future reference but Session couldn't when user close the browser Session data also lost.

Posted by Amit Tiwari


No comments:

Post a Comment