Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why difference in localhost vs 127.0.0.1 regarding SESSIONS

I'm wondering why there's a difference in these two for sessions? If i have a login form and it passes sessions to a page (ie: settings.php). If i have localhost/settings.php the session would work, if I go to a different page and come back. but if it is 127.0.0.1/settings.php, the session will work on the first pass, then if I redirect somewhere else and go back, its not there anymore.

Does this happen to other people too? Or is this just me?

Thanks

like image 480
hellomello Avatar asked Dec 01 '25 05:12

hellomello


2 Answers

Maybe this will help: http://www.issociate.de/board/post/179979/Cookie_Problems_on_Localhost.html

both 'localhost' and any ip are not accepted as valid domain-identifiers in cookies (as per RFC).

and 127.0.0.1 != localhost for browser. browser will not send cookie that was set from 127.0.0.1 to localhost, because they are different domains.

p.s. in practice, there can be many domains on one ip. and for security reasons, of course, browser can't send all cookies altogether (imagine, that cookies from your website could be sent by browser to all websites on virtualhosting with the same ip).

like image 79
avasin Avatar answered Dec 03 '25 22:12

avasin


Due to the problems referenced in @true's answer, in our development we usually create a fake local domain like dev.localhost.net and use the hosts file to map this either to the machine ip address or to 127.0.0.1. This helps get around the session/cookie problem.

like image 37
davidethell Avatar answered Dec 03 '25 21:12

davidethell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!