Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should PHP session be created before login or after successful login

If PHP session is created before login, there will be one session file created for each request to login page.

The problem is if user makes multiple requests to server through a script then those many session files will be created.
If user wants to attack server,he can send abnormally huge number of requests creating so many session files eating up all the temporary space and making the service unavailable.
I am not sure if this kind of attack is really possible/feasible.

Please share your comments on this and implications if PHP sessions is created before/after successful login.

like image 608
Naga Kiran Avatar asked Aug 05 '09 13:08

Naga Kiran


2 Answers

I think you are misunderstanding session_start()

What happens with session_start is, yes, it will create a file for the individual user. But the next time you call session_start(), it is going to use that same file for that same user because the user has a cookie on their system that tells it what ID to use. In order to have the $_SESSION array available, you must call session_start() on every page.

It is very possible that someone could whip up a scenario like you just described.

In reality, yes, a hacker could always have a robot that clears its cookies after every attempt, make 10,000 requests, and possibly create a disk writing problem, but I really wouldn't worry about it too much, because the files are tiny, much smaller than the script you are writing. You'd have to write a lot more files (on the size of millions or billions) to actually create a problem.

If you really want to see what the effects would be on your server. Write a script that creates files in a directory with the equivalent of 2 paragraphs of text. and put it in a loop for 10,000 files.

If you are then worried about the affects it would have, I suggest implementing a tracker that can see an large amount of hits coming to the site from a single IP address and then either temporarily ban the IP address, or do what Google does and just provide them with a static captcha page that doesn't take many resources to serve.

So, going back to the actual 'question':

I set a session for every single user that ever visits my site, because I use sessions for not only User Authentication, but for tracking other variables on my site. So, I believe that you should set it even if they aren't logged in.

like image 111
Tyler Carter Avatar answered Sep 21 '22 00:09

Tyler Carter


If you're worried about a session fixation attack, think about using session_regenerate_id() function.

like image 44
mere-teresa Avatar answered Sep 20 '22 00:09

mere-teresa