It is pretty apparent that most of us PHP programmers don't want our published work to be hacked or exploited in ways we did not intend. I am therefore extra careful when asking about ways to counter session hijacking. I know there is the session_regenerate_id() function to partly counter session hijacking but I was more curious about another method I came across:
As a user logs in to the website you take their user_id (or another even more secret, predefined encrypted string) (which is unknown for common users) as a string and you salt it with random predefined symbols, md5() the string and set it as a $_SESSION['user_code'] = $that_string; and anytime this user goes to a page you salt repeat the procedure and match it with $_SESSION['user_code'], if they do not match; destroy the session.
So in code it would look something like this (for example):
//user credentials are correct, user data is fetched from db
$_SESSION['username'] = $row[3]; //username
$_SESSION['password'] = $row[2]; //password
$_SESSION['user_id'] = $row[4]; //user_id
$salt1 = 'uNs819';
$salt2 = 'J2i';
$user_code = $salt1 . $row[4] . $salt2;
$user_code = md5($user_code);
$_SESSION['user_code'] = $user_code;
And then you check if this is correct in the beginning of every available page with:
//fetch user credentials from db again
//$row4 is the user_id
if($_SESSION['user_code'] != md5($salt1 . $row[4] . $salt2){
session_destroy();
}
I do not think using the user_id as part of the encryption is optimal but it is only an example. Preferably I will use an md5 string of the timestamp of when the user was created. But if I was being unclear my main question is that is this method solid against session hijacking, why/why not?