简体   繁体   English

用Javascript制作用户名和密码

[英]Making a Username and Password with Javascript

I've seen other questions similar to this, but I've tried answers and it doesn't appear to do what I want (if it does and I just can't see that it does, please just let me know in the comments. I'm pretty new to this). 我看过其他与此类似的问题,但是我尝试了答案,但似乎没有达到我想要的效果(如果可以,但是我只是看不到它可以,请在评论中告诉我我对此很陌生)。

I want to password protect a website (this is a personal family website, so I'm not worried about encryption or anything, unless it's super easy to do with javascript) so that only my family can get on. 我想用密码保护一个网站(这是一个个人家庭网站,所以我不担心加密或其他任何事情,除非使用javascript超级容易),这样只有我的家人才能上班。 I want the passwords to be personalized. 我希望密码个性化。

HTML CODE HTML代码

<form id="usr-psswd">
  Name: <input type="text" name="name" id="name">
  Username: <input type="text" name="usr" id="usr">
  Password: <input type="text" name="psswd" id="psswd">
  <input type="button" onclick="submission()" value="Submit">
</form>

Script Code 脚本代码

function submission(){
  var name = document.getElementByID = "name";
  var psswd = document.getElementByID = "psswd";
  var usr = document.getElementByID = "usr";

  var name = new Array();
  var usr = new Array();
  var psswd = new Array();
}

My Question: how do I get javascript to put the input into an array that stays on the page so that when you try to get to index.html (which is where the password protection is) so that they don't have to set this up every time? 我的问题:我如何获取javascript以将输入放入留在页面上的数组中,以便当您尝试访问index.html(这是密码保护所在的位置)时,他们不必设置此设置每次都向上

Also, if it's simple enough, how can I run multiple passwords through encryption and keep it on the webpage so that if someone happens to get on here the passwords are safe? 另外,如果它很简单,我如何通过加密运行多个密码并将其保存在网页上,这样如果有人碰巧进入此处,密码是安全的?

Yes, I realize that the var name var psswd and var usr are repeated twice. 是的,我意识到var name var psswdvar usr被重复了两次。 That's because I'm too lazy to name them different things right now. 那是因为我现在懒得给它们起不同的名字。 I just need to know how to put var name = document... into var name = new... . 我只需要知道如何将var name = document...放入var name = new... Thanks! 谢谢!

I know you said that you wanted to do a simple authentication for the site, but doing it in JavaScript is unsafe and the usernames/passwords are easily retrieved. 我知道您说过您想对网站进行简单的身份验证,但是用JavaScript进行身份验证是不安全的,而且用户名/密码很容易检索。

If you want something very simple and fast to do.. I suggest you do this by using .htaccess for authentication: 如果您想做一些非常简单和快速的操作。.我建议您使用.htaccess进行身份验证:

To do this, The system requires two files -- the .htaccess file and .htpasswd file: 为此,系统需要两个文件.htaccess文件和.htpasswd文件:

The .htaccess Code: .htaccess代码:

AuthType Basic
AuthName "restricted area"
AuthUserFile /home/user/html/protect-me-dir/.htpasswd
require valid-user

The above code protects a directory called protect-me-dir at root level. 上面的代码在根级别保护一个名为protect-me-dir的目录。 The AuthUserFile value is always specific to your hosting configuration. AuthUserFile值始终特定于您的托管配置。 If you don't know what the value should be, do a phpinfo() and find the DOCUMENT_ROOT value. 如果您不知道该值是什么,请执行phpinfo()并找到DOCUMENT_ROOT值。


The .htpasswd Code: .htpasswd代码:

your_username:your_password
another_user:another_password
you_get:the_idea

The .htpasswd file contains the usernames and passwords of allowed users. .htpasswd文件包含允许用户的用户名和密码。 One per line. 每行一个。


Otherwise, I'd probably do something fancier by maybe having a proper login using a combination of PHP and a database for example. 否则,我可能会做的更好,例如使用PHP和数据库的组合进行正确的登录。

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM