2013-08-24 71 views
0

我校下週做5天的HTML挑戰,我練我的想法,簡單的JavaScript登錄,重定向到用戶的頁面(多用戶)

要切入正題,

我想要使用支持多個用戶的外部JavaScript腳本創建登錄系統 ,並且當兩個登錄表單從代碼的某個部分有效時,它將重定向到用戶頁面: 我有兩個輸入設置:

<center> <h1> Join now! </h1> </center> 
</header> 
<p> Username </p> <input> </input><p> Password </p> <input> </input> 
<button> Submit </button> 

隨意讓需要這個代碼的任何調整,我寧願不使用數據庫的此項目

感謝您的幫助球員:)

哦PS, 我只需要三個用戶系統

而且你可以將用戶的jQuery藏漢如有必要

再次感謝

+0

我不想聽起來像一個混蛋。但是有一個關於stackoverflow的指南,它提出了一個問題,你必須對問題有一個最小的理解。這看起來不像你。嘗試閱讀一般HTML輸入標籤和用戶身份驗證的語法。 – Hless

+0

至少,你可以在一個php文件中定義3個用戶和密碼。 ajax到php頁面,並允許進入適當的PHP,如果用戶名/密碼匹配。否則,您將擁有客戶端上的所有用戶名和密碼,這些用戶名和密碼甚至不具有遠程安全性。 – carter

+0

我有一個最低限度的理解檢查了這一點:theslashclan.24.eu和goto管理員登錄 –

回答

1

我能想到的最簡單的登錄表單。顯然沒有充分的證據。您應該使用加密並將其存儲在數據庫中,並使用會話和所有爵士樂。但這很簡單。

HTML:

<!DOCTYPE html> 
<html> 
<head> 
</head> 
<body> 
<h1> Join now! </h1> 
<form name="login_form" method="POST" action="checkLogin.php"> 
    <p> Username </p> <input type="text" name="user"> 
    <p>Password </p> <input type="pass" name="pass"> 
    <input type="submit" value="Submit"> 
</form> 
</body> 
</html> 

PHP:

<?php 
//make an associative array of users and their corresponding passwords. 
$user_pass_list = array("Joe" => "1234", "Bob" => "4321", "Sally" => "super"); 

//assign the 'user' variable passed in the post from html to a new php variable called $user 
$user = $_POST['user']; 
//assign the 'pass' variable passed in the post from html to a new php variable called $pass 
$pass = $_POST['pass']; 

//check if the user exists in our array 
if(array_key_exists($user, $user_pass_list)){ 
    //if it does, then check the password 
    if($user_pass_list[$user] == $pass){ 
    echo "Login Success"; 
    }else{ 
    echo "Login Failure"; 
    } 
}else{ 
    echo "User does not exist"; 
} 

?> 
+0

然後下載WAMP,啓動它,創建一個php文件和一個html文件。 Wham,bam,謝謝你。你有一個用戶檢查服務器。 :) gl – carter

+0

那就是那個,謝謝你的回答 –

4

據我瞭解,你只會有3個可能登陸預定義的用戶。我認爲你只想顯示應用程序的功能,而不需要構建防彈登錄系統。要做到這一點,你可以有這樣的形式:

<form> 
    <legend>Log In</legend> 

    <fieldset> 
     <label for="username">Username: </label> 
     <input id="username" type="text"> 

     <label for="password">Password: </label> 
     <input id="password" type="password"> 

     <button id="login" type="button">Log In!</button> 
    </fieldset> 
</form> 

純JavaScript驗證和重定向:

var users = [ 
    { username: 'user1', password: 'pass1' }, 
    { username: 'user2', password: 'pass2' }, 
    { username: 'user3', password: 'pass3' } 
]; 

var button = document.getElementById('login'); 

button.onclick = function() { 
    var username = document.getElementById('username').value; 
    var password = document.getElementById('password').value; 

    for (var i = 0; i < users.length; i++) { 
     if(username == users[i].username && password == users[i].password) { 
     window.location.href = 'http://where/you/want/to/redirect/'; 
     break; 
     }else{ 
     alert('You are trying to break in!'); 
     } 
    } 
} 

PS:這個例子只是說明了如何在3個用戶存儲在一個陣列,以及如何驗證前端的輸入,以便只有這些用戶才能登錄。

+0

謝謝:)幫助 –

相關問題