I have a big focus on security and I'm trying to secure my $_SESSION. I would like your advise.
When a user create account I generate a random value from 11111 - 99999 in their profile in database.
When a user login I create a session that contain most of user data (no password or any critical info). In that $_SESSION I also add a encrypted version of that random number that I've created on step 1 and I also create a $_COOKIE with the same encrypted number that I will use to check if the session is valid.
On my application then when I receive a request, I check if the $_SESSION exist in fact it should exist but to prevent hacker to (guess or hack) other people's session id I compare the encryped value of the key from the $_COOKIE with the one inside the $_SESSION.
The main quest is, how secure is this method? I can't see any way a hacker can steal the $_SESSION id and at the same time the encrypted key.
the encryption of the key is generated by a encryption value located on server and not with simple md5 that in my point of view are easy to reproduce. since hacker cannot know the encryption value that i use for encrypting that key.
then at final when user send a request on server it look like this (I simplified the code).
if(isset($_SESSION['key'])){
if($_SESSION['key'] == $_COOKIE['key']){
// do stuff here
}
else {
die();
}
}
else {
die();
}