Session security risks come from three different possibilities:
- Forecasting
- Capture
- Fixation
A prediction would mean that someone who is not the user for whom the session was created guessed their session identifier. The chances of this are almost 0, although they are growing, as more and more users use the site at the same time.
Using your code, you risk even less, because it will only work if the attacker has separated the user agent and the ip of the forecasted session. But in this case, the difference is trivial.
Commit means that an attacker can create a session and then force another user to use his session. In this case, it will depend: if the attacker knows that you are doing this, and they fake the user agent and client ip, they can record the session. Or if they share ip and user agent.
And finally, we have session capture, probably the most common method of the three. In this case, the attacker will somehow gain access to the session ID of the valid registered user, and then will use it to log in to his account. As in the previous method, this will only work for them if they know that you are checking the ip and user agent and faking the same ones as the user. The technique you use is not unique, and some attackers may attack them just in case.
As they say, is it safe? Yes and no
If you are obsessed with security, the answer is always the same: Use SSL
If your code is not open source, almost everything you do that modifies the behavior of php sessions will be fairly safe.
The only exception is really popular sites that will attract the attention of hackers.
There is very good documentation in this thread:
aurbano
source share