+2 votes
191 views

Could you explain what is session and cookie and what is the difference between them?

asked Aug 22, 2015 in Computer Science - IT by T.PRATHIBA
edited Feb 28 by transformer | 191 views

2 Answers

+1 vote
Selected Ans.

In general we have : meeting session,class session,gossip session :) etc.

Also Cookie : a small flat sweet cake.

But how come they be related to computing ?

In computing both sessions and cookies store information.

Main difference between both are, Session is stored in Server but at the same time cookies are stored in Client system.

Both may contain various information like userid,location,login times,username  and other values which will be useful.

You can also delete the cookies stored by any website using web-browser settings.

Edit 1 :

You open browser ,logs into enggforum , posts query and logs out.Actually you have had a session with enggforum.com,right ?

From the time when you log in till close the application enggforum must know details regarding your username and other details.How those details are stored ?

We have options like saving values in a client side as some text file or we can save in server.

Cookies are saved in client side as some text files and if you delete those files all related details are lost.

In order to make them store ,we have codings to be done ,you can see some examples here:

Syntax : 

setcookie(name, value, expire, path, domain, secure, httponly);

PHP cookies :

$cookie_name = "student";
$cookie_value = "Raj";
setcookie($cookie_name, $cookie_value, time() + (864 * 30), "/");

PHP sessions :

session_start();
$_SESSION["favcolor"] = "yellow";

As cookies are stored in client systems ,you can view them from Browser security settings as below screenshot shows :

 

 

answered Aug 23, 2015 by slow_learner
edited May 26, 2016 by slow_learner
@slow_learner,can u explain some more points with examples...
see the edited version and let know if further query....
+1 vote

Differences:

1.Cookies are stored in browser as text file.

1.Sessions are stored in server side.


2.cookie is stored limit amount of data.

2.session is stored unlimited amount of data.


3.cookie doesn't hold multiple variables.

3.session holds multiple variables.


4.Accessing cookies is easy.so less secure.

4.session is more secure.


5.setcookie() function used.

5.session_start() function is the first thing in our document.

Example cookie:

<?php 

setcookie(name,value,expire,path,

domain,secure,httponly);

$cookie_name="shiva";

$cookie_value="12345";

//set cookie for 5minutes

setcookie($cookie_name,cookie_value,300,"/");

//expire cookies

setcookie($cookie_name,"",-300);

?>

session example :

<?php

session_start();

$_SESSION['user']='shiva';

session_destroy();

variable "user".unset($_SESSION['user']);

?>

answered Feb 28 by shiva9740