Using Cookie to set username and password via Javascript

梦想的初衷 提交于 2019-12-05 22:30:15

I'll skip the lecture and give you what you asked for:

document.getElementById("password").value = password;

seems to work (tested in jsfiddle)

You should never store usernames and passwords as cookies, even if they're encrypted or hashed, this is bad practice as anyone could just come along and inspect the user's network traffic and steal their identity. Storing usernames and passwords as cookies essentially broadcasts them to anyone who may be looking in.

Instead you should create a login page which stores a session token and the user's IP in your database and sets that as the cookie, then when a user has your session token cookie all you need to do is compare that and the user's IP address with your tokens table in your database to check whether they are logged in or not.

If the same person comes along and inspects the network traffic, all they'll get is the session token. Sure, they could potentially use this to steal the session, but they'd need the same IP address.

This is why websites should also always require a password to be re-entered when letting users modify account information.

See this Wikipedia article on Session Hijacking for further reading.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!