While it is true that there are similarities and differences between cookies and sessions , is there an alternative to these two options?
On the one hand, cookie information can be modified by the user, so storing, for example, a user's ID is not recommended. And on the other hand, the information of a session is deleted once the user exits the browser (if I'm wrong about something, correct me). So how can the user's identity be securely stored so that it cannot be changed or deleted when the browser is exited?
A session
PHP
securely saves the username, login state, and other things in the$_SESSION
array, since it's stored on the server. The only thing that is sent to the browser is acookie
unique one (called PHPSESSID unless changed by php.ini ) containing theID
session ID, which is a unique random number.Once your visitor has an active session every time they request a page you have
session_start()
at the top, itsession_start()
will watch the request for acookie
callPHPSESSID
, read the session file from the server (if the session exists and is valid) , and restore the$_SESSION
archived set . This array never needs to leave the server.The
cookie
session one is set without an expiration date (unless you mess with thesession.cookie_lifetime
option in php.ini ) , so the browser clears it on shutdown. The session file on the server has an expiration time, managed bysession.gc_maxlifetime
(in seconds).Path to more secure sessions:
If you need to implement a
cookie
"Remember Me", follow the instructions in the blog post.selector
andidentifier
selector
yidentifier
in an HTTP cookie, set thehttpOnly = true
ysecure = true
to only be accessible over HTTPS (and hidden from JavaScript)selector
and a hash (SHA256 is fine here) of theidentifier
token tableWhat not to do in a Secure Login Systems
random_bytes()
, and if you're using PHP 5, random_compat ).You can configure them
cookie
with thesession_set_cookie_params
function or within yourphp.ini
.Finally, you need to create a script to log users out of session (and encourage them to use it instead of just browsing). This is a sample script:
to log out:
font
Now you can investigate further.
Try using the browser's LocalStorage. With javascript it's pretty easy to implement and has saved my life several times when I want data to persist from session to session. Here is an example. Now, regarding security, well, if I tell you in advance that it is editable by the user with knowledge of the subject. But it's up to you to implement measures to validate that information before using it. Finally you can use the database to manage the user information and that is not access to it.
You cannot prevent the user from deleting or modifying their session cookie. The user can always modify their session identifier, or delete their cookies so that on the next visit they do not detect that it was the same user. Using LocalStorage does not fix this, as the data in LocalStorage can also be modified by the user.
And so it should be, for privacy reasons. You can't force a user to send his ID to your server if he doesn't want to.
For this reason, what is stored in the value of the cookie is only the session ID, and the data (such as the user ID associated with said session), is stored on the server. This way the user can only change the session ID, which should be random enough to avoid this.
Finally, a piece of advice: the session and cookie system is perfectly safe as long as it is used correctly. Do not reinvent the wheel and make sure you use these systems correctly.
I'm a newbie and I'm trying to put myself in the place of the questioner. That's why I kept thinking about your comment: "... for privacy reasons. You can't force a user to send his ID to your server if he doesn't want to..." But you can know the IP address from which he logs in. Is not true? The data of the IP of the "visitor" is not a minor data, to know from where we are creating security problems. I don't think the questioner is trying to reinvent the wheel. It would seem that he has an interest in beefing up security. It wouldn't change anything, but it would add more security. In the event of security attacks, I would create an "IP blacklist". But my proposal would exceed what is asked here.