What is the different between cookies and session in php?
Answer Posted / ankit
A cookie can keep information in the user's browser until
deleted. If a person has a login and password, this can be
set as a cookie in their browser so they do not have to
re-login to your website every time they visit. You can
store almost anything in a browser cookie. The trouble is
that a user can block cookies or delete them at any time.
If, for example, your website's shopping cart utilized
cookies, and a person had their browser set to block them,
then they could not shop at your website.
Sessions are not reliant on the user allowing a cookie. They
work instead like a token allowing access and passing
information while the user has their browser open. The
problem with sessions is that when you close your browser
you also lose the session. So, if you had a site requiring a
login, this couldn't be saved as a session like it could as
a cookie, and the user would be forced to re-login every
time they visit. You can of course get the best of both
worlds! Once you know what each does, you can use a
combination of cookies and sessions to make your site work
exactly the way you want it
The difference between sessions and cookies is that a
session can hold multiple variables, and you don’t have to
set cookies for every variable. By default, the session data
is stored in a cookie with an expiry date of zero, which
means that the session only remains active as long as the
browser. When you close the browser, all the stored
information is lost. You can modify this behavior by
changing the “session.cookie_lifetime” setting in “php.ini”
from zero to whatever you want the cookie lifetime to be.
session_start(); //starts or resumes a function
session_destroy(); //ends the session; comment this line and
the browser will output the same session ID as before If you
want to remove the registered variables, you need to use the
session_unset() function.
| Is This Answer Correct ? | 2 Yes | 0 No |
Post New Answer View All Answers
What does it mean when it says the csrf token is invalid?
Which function you can use in php to open a file for reading or writing or for both?
Where to put php files in apache server?
Tell me what is the use of isset() in php?
How to avoid the undefined index error?
Can a trait extend a class php?
What is the use of rand() in php?
What type of operation is needed when passing values through a form or an url?
What is difference between web service and api?
Which is the latest version of php?
What php framework does wordpress use?
What is the purpose of using php?
What is super keyword in c++?
What is memcache?
What is a lambda function in php?