`$_SESSION` variables are stored in the server and hence cannot be modified by the user.
One of the best practices is that we must keep changing the session id
from our end.
Thats why we use a function called session_regenerate_id()
.
This function that will replace the current session ID with a new one, and keep the current session information, so the use will not be logged out.
To answer your question in comment:
Whenever you start a session, server will send back a cookie which will contain the session id
, the cookie name will be PHPSESSID
which is the default name. You can also choose to change it. So this id is what that keeps changing when you use the session_regenerate_id
function.
REASON WHY TO USE IT:
This mainly helps in preventing session fixation
attacks.In this attack a malicious user will try to fix the session ID (SID) of another user. If he gets successful,the user will get all the access of the original user and will be able to do anything that the legitimate user can do.
So if you regenerate the session id
the previous old session id
will be no longer valid
You can get more info about session fixation IN OWASP WEBSITE