Close Search Box
Search Box

Search: From:

Close
Newsletter

9Tutorials to your Inbox



Preventing Session Hijacking in PHP

Preventing Session Hijacking in PHP
Author lv1 (3200/5000)
2,167 views
1 Star2 Star3Star4 Star5 Star (2 votes, average: 2 out of 5)

You want make sure an attacker can’t access another user’s session. The solution is to allow passing of session IDs via cookies only, and generate an additional session token that is passed via URLs. Only requests that contain a valid session ID and a valid session token may access the session:

If you’re using a PHP version earlier than 4.3.0, output_add_rewrite_var( ) is not available.

Adding a session token to links

The regular expression for matching hyperlinks in the inject_session_token( ) function isn’t bulletproof; it will not catch hyperlinks with href attributes quoted with single quotes.

Discussion
This example creates an auto-shifting token by joining the current week number together with a salt term of your choice. With this technique, tokens will be valid for a reasonable period of time without being fixed.

We then check for the token in the request, and if it’s not found, we prompt for a new login.

If it is found, it needs to be added to generated links. output_add_rewrite_var( ) does this easily. Without output_add_rewrite_var( ), we continue generating the page and declare an output buffer callback function that will make sure that any hyperlinks on the page are modified to contain the current token before the page is displayed.

Note that the inject_session_token( ) function in the example does not address imagemaps, form submissions, or Ajax calls; make sure that you adjust any such functionality on a page to include the session token that’s been generated and stored in the session.

del.icio.us:Preventing Session Hijacking in PHP digg:Preventing Session Hijacking in PHP spurl:Preventing Session Hijacking in PHP newsvine:Preventing Session Hijacking in PHP blinklist:Preventing Session Hijacking in PHP furl:Preventing Session Hijacking in PHP reddit:Preventing Session Hijacking in PHP blogmarks:Preventing Session Hijacking in PHP Y!:Preventing Session Hijacking in PHP magnolia:Preventing Session Hijacking in PHP segnalo:Preventing Session Hijacking in PHP

Post a Comment »








Safari hates me

Comment Guidelines

  • Hyperlinks are automatically generated.
  • <em>italic</em>
  • <strong>bold</strong>
  1. php programmer September 17, 2007

    nice tutorial http://www.w3answers.com