2017-05-06 53 views
0

我創建了一個非常非常基本的登錄系統。代碼工作正常,但如果您檢查元素,您可以看到所有讀取用戶名和密碼的if。我的代碼是:如何保護登錄系統

` 
<html> 
<body> 
<form name="login"> 
Username: <input type="text" style="height:20px;width:150px;" name="un" id="un" 
<input type="button" onclick="login();" value="login" />/><br> 
    Password: <input type="password" style="height:20px;width:150px;" name="ps" id="ps" /><br> 
</form> 
</body> 
</html> 

and my checking function is:

<script language="javascript"> 
function login() 
{ 
if(login.un.value == "user1" && login.ps.value == "user1") { 
     alert("user1"): 
    } else { 
     if(login.un.value == "user2" && login.ps.value == "user2") { 
      alert("user2"); 
     } else { 
      alert("Unknown details"); 
      } 
} 
</script> 
+1

您不應該使用JavaScript或任何其他客戶端語言創建登錄系統。考慮使用PHP或Rails來代替。 –

+0

基本上你試圖在javascript上創建完整的登錄信息,這是從檢查元素或視圖源訪問的開放信息。你需要完全從服務器端處理。檢查這是一個PHP指南:http://phppot.com/php/php-login-script-with-session/ –

回答

1

正如已經在評論中提到的,這是不可能純粹創建一個登錄系統使用JavaScript在用戶不能夠讀取所需的用戶+從密碼組合源代碼。

因此,有必要在服務器端使用PHP,Ruby on Rails,Node或其他方式實現身份驗證。