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.
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("Wele to BOOKSIS.COM");
var userid = document.getElementById("userid").value;
document.write("Wele " + userid);
}
else
{
alert("Error Password or Username");
}
}
</script>
</body>
</html>
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.
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("Wele to BOOKSIS.COM");
var userid = document.getElementById("userid").value;
document.write("Wele " + userid);
}
else
{
alert("Error Password or Username");
}
}
</script>
</body>
</html>
Share
Improve this question
asked Sep 21, 2013 at 20:15
AkimichiAkimichi
111 gold badge1 silver badge4 bronze badges
5
- 1 Javascript is client side and most of the time only used for effects/interactivity on a webpage. User systems are written using PHP/mysql so that's why it doesn't remember it. You could try to find a work-around by using cookies but I think the PHP/mysql route is better. – Anton D Commented Sep 21, 2013 at 20:16
- Try using PHP and Ajax. – Praxis Ashelin Commented Sep 21, 2013 at 20:29
- you can set cookie to set user name gloabal and you can display it on every page – Ankit Agrawal Commented Sep 21, 2013 at 20:55
- You can't trust the client to authenticate itself. If you want to do your auth in JS then use a server side JavaScript implementation such as Node.js. – Quentin Commented Sep 21, 2013 at 22:38
- I was wondering how I would go about setting a cookie just to remember the username and display it on every page?i could not do the server side at the moment because i am still beginner in JavaScript. – Akimichi Commented Sep 22, 2013 at 2:36
2 Answers
Reset to default 2You 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.
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.
You need to perform this check server-side and use cookies/sessions to store the "Logged in" state.
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.
I'd strongly remend you look into ASP.Net, PHP or maybe Python to get you started
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742318715a4421368.html
评论列表(0条)