javascript 仅使用javascript成功登录后如何在所有页面中显示用户名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18937139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to display username in all pages after successful login by using only javascript?
提问by Akimichi
I am trying to create a login form using html,css and javascript and i have done it.But, the problem here is i could not display the username in all pages after the user has succeeded in login. And also it seems that after the user has succeeded in login,it stays at the same page which is supposed to be at another page.Can anyone please help me with this code or give me a hint so that i could do the rest.
我正在尝试使用 html、css 和 javascript 创建一个登录表单,我已经完成了。但是,这里的问题是在用户成功登录后我无法在所有页面中显示用户名。而且似乎在用户成功登录后,它停留在应该在另一个页面上的同一页面上。任何人都可以帮助我使用此代码或给我一个提示,以便我可以完成其余的工作。
Here is the code i am using
这是我正在使用的代码
<head>
<title>Login page</title>
</head>
<body>
<h1 style="font-family:Comic Sans Ms;text-align="center";font-size:20pt;
color:#00FF00;>Simple Login Page</h1>
<form name="myForm" method="post" action="target.html">
Username<input type="text" name="userid" id="userid"/>
Password<input type="password" name="pswrd"/>
<input type="button" onclick="check(this.form)" value="Login"/>
<input type="reset" value="Reset"/>
</form>
<script language="javascript">
function check(form)
{
if(form.userid.value && form.pswrd.value)
{
alert("Welcome to BOOKSIS.COM");
var userid = document.getElementById("userid").value;
document.write("Welcome " + userid);
}
else
{
alert("Error Password or Username");
}
}
</script>
</body>
</html>
回答by user2699706
You need a server to store your login data in the session. HTTP is a stateless protocol and cannot store data. Try to learn PHP, or another serverside language, and send your variables for example via Ajax to your server.
您需要一个服务器来在会话中存储您的登录数据。HTTP 是无状态协议,不能存储数据。尝试学习 PHP 或其他服务器端语言,并通过 Ajax 等方式将变量发送到您的服务器。
回答by Basic
Wrong tool for the job... Javascript is easily bypassed by anyone with only a moderate working knowledge of scripting. There is nothing your JS can do that a user following the code couldn't (eg determine what password it's checking for). There is no way you can hide that code from a user as it has to be sent to them to execute.
错误的工作工具... Javascript 很容易被任何只有中等脚本工作知识的人绕过。没有什么是您的 JS 可以做的,跟随代码的用户不能做的(例如,确定它正在检查的密码)。您无法向用户隐藏该代码,因为必须将其发送给他们才能执行。
You need to perform this check server-side and use cookies/sessions to store the "Logged in" state.
您需要在服务器端执行此检查并使用 cookie/会话来存储“登录”状态。
It would be possible to set a cookie from Javascript and use that but as mentioned above, the security provided would be so negligible that it's not worth the time to implement it.
可以从 Javascript 设置 cookie 并使用它,但如上所述,所提供的安全性可以忽略不计,以至于不值得花时间来实现它。
I'd strongly recommend you look into ASP.Net, PHP or maybe Python to get you started
我强烈建议您查看 ASP.Net、PHP 或 Python 以帮助您入门