1

I'm implementing my first user login and I've read some posts here on Stack about the convenience of store login infos in sessions or cookies, but my main question is still there: is it so unsafe storing the userID/userCode in a cookie to let the user stay logged in?

In other words, is it possible to download the cookie on a pen drive, downloading it into another PC, connect to the website and be recognized as logged in? If so, how giants like Facebook and Amazon had implemented this system avoiding security issues?

Should completely avoid, then, the use of cookies for this purpose in favor of sessions (although the user has to log in every time)?

Since I have to collect sensitive informations I would like to build the safest system I can.

11
  • 4
    Because of session hijacking and the possibility of modifying cookies, you should always be cautious. Never store any data in plain text, if you want it to be kept secure. Commented Dec 7, 2016 at 11:10
  • 3
    As @Hallur mentioned there is always some vulnerability. You can minimise this by cross checking your sessions, ensuring you incapacitate XSS attack and so on. Bare in mind to many of these checks can add considerable overhead to you pageloads Commented Dec 7, 2016 at 11:12
  • @Hallur Sure I could make a cookie "unreadable" hashing it, but in this case of user login, wouldn't be useless if the aim is to store the userdID/code that will be checked by a PHP script? In other words, if I have access to the cookie and I "steal" it, it's the same if it's readable or not 'cause the way the PHP script read it is always the same, no? Commented Dec 7, 2016 at 11:16
  • 2
    How come people always try to store data in a cookie. Have you never read any privacy statement or legal cookie notice? You should. Commented Dec 7, 2016 at 11:19
  • 2
    “if I have access to the cookie and I "steal" it, it's the same if it's readable or not 'cause the way the PHP script read it is always the same, no?” - yes, but that is not the issue. If you only stored an unencrypted user id, then I would not have to steal your cookie - all I would need to know is your user id, and then I can send my own cookie containing that id. Commented Dec 7, 2016 at 11:21

1 Answer 1

1

Safely variant: Store in Cookie a SESSION ID (32-64 symbols, random string).

By SESSION_ID (Stored in Cookie) open the session storage (Stored on server and not avaliable for user access) and work with that (You may write into session storage user id|token or another actions).

In PHP - See $_SESSION and $_COOKIE

And one more moment in PHP (7+): php.ini parameter - session.sid_length (Set session id string length)

Sign up to request clarification or add additional context in comments.

3 Comments

Nice tip, I'm gonna try it; although I didn't understand the random string part
random string - string with fixed length and random symbols.
... random string generate for new session - this string will ass session id.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.