Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regenerating session id

I am thinking of using this code on every page to reduce the possibility of session hijacking. By renewing the session_id on every request

if(!empty($_session)){ 
   session_start(); 
}

Another way to achieve so would be to do this:

if(!empty($_session)){ 
  session_regenerate_id(true);
}

However, I heard criticisms of that function that say that if the page is refreshed too fast for some reason, the session id becomes invalid.

Another way to use the session id is to have more control over how a session is generated.

There are other ways to achieve so.. Whats the best practice?

like image 270
Dmitry Makovetskiyd Avatar asked Nov 15 '11 10:11

Dmitry Makovetskiyd


People also ask

What is session regenerate ID?

Definition and Usage. Sessions or session handling is a way to make the data available across various pages of a web application. The session_regenerate_id() function generates a new session id and updates the current one with the newly created one.

How can you generate a session ID?

Every time an Internet user visits a specific Web site, a new session ID is assigned. Closing a browser and then reopening and visiting the site again generates a new session ID.

How does PHP generate session ID?

The session id is a random value generated when a session is started. The session id is stored as a cookie in the browser such that on subsequent visits the data stored in the session can be loaded and reused. This issue is about the session id (cookie value) and not about the session name (cookie name).


4 Answers

Calling session_regenerate_id on every page is an unnescessary overhead.

You should only be calling it at the point of login or any time you re-authorize a user.

If you want additionally you could store the last regenerated time in a session and then call session_regenerate_id after say 30 minutes, but there's definetly no need for this to be done on every page.

like image 121
fire Avatar answered Sep 21 '22 06:09

fire


I had problems indeed (on page refresh or inside ajax requests), using session_regenerate_id(true); on each request.

But not with session_regenerate_id();

So, according to

Renew the Session ID After Any Privilege Level Change https://www.owasp.org/index.php/Session_Management_Cheat_Sheet#Renew_the_Session_ID_After_Any_Privilege_Level_Change

Regenerate SID on each request http://en.wikipedia.org/wiki/Session_fixation#Regenerate_SID_on_each_request

i use

  • session_regenerate_id(); on each request
  • session_regenerate_id(true); on login, logout etc (any privilege level change)
like image 29
Christos Pontikis Avatar answered Sep 22 '22 06:09

Christos Pontikis


Best practise is to use SSL (and apply the usual defences against other security attack vectors such as XSS and SQL injection). Cycling session ids is just begging for race conditions.

like image 36
Quentin Avatar answered Sep 19 '22 06:09

Quentin


Instead of generating session IDs,why don't you encrypt and use the already generated one.It can be used and destroyed when the intended action is complete.

like image 41
Cheruiyot Felix Avatar answered Sep 21 '22 06:09

Cheruiyot Felix