2013-07-05 102 views
0

我發現了一個密碼生成器,並希望有一個表格,允許用戶註冊,但如果他們想要生成密碼,他們可以按生成密碼,並填寫文本輸入框密碼。PHP密碼生成表格形式

然後,我希望他們能夠按下注冊,它將所有的表單數據與手動設置的密碼或生成的密碼發送到表單提交。

任何想法,我可以把窗體內的形式與它是有效的HTML?

我的代碼如下:

<form id="register" action="registerprocess.php" method="post"> 
    <span><strong>Please note all fields are required</strong></span><br /><br /> 
    <label for="fullname">Full Name: </label><input type="text" id="fullname" name="fullname" required><br /> 
    <label for="email">Email Address: </label><input type="email" id="email" name="email" required><br /><br /> 
    <label for="manualpass">User-Defined Password: </label><input type="text" id="manualpass" name="manualpass" required><br /> 
    <?php 

    $f_contents = file("..\sec\possiblewords.txt"); 
    $word = $f_contents[rand(0, count($f_contents) - 1)]; 

    function vowelreplacement() { 
     global $word; 
     $string = $word; 
     $trans = array("a" => "4", "e" => "3", "i" => "1", "o" => "0"); 

     return strtr($string, $trans); 
    }   

    $generatedpassword = vowelreplacement(); 

    ?> 
    <form id="vowelremover" method="post">   
     <label for="generatedpass">Generated Password: </label><input type="text" id="generatedpass" name="generatedpass" value=<?php echo $generatedpassword;?> required><input type="submit" value="Generate Password"> 
    </form> 
    <input type="submit" value="register"> 
</form> 
+0

,你能否告訴我們在一個表格形式的代碼?它可能被壓縮成「只是一種形式」。 –

+0

這已經爲你完成:) – AaronHatton

+0

這不會是非常安全的,因爲你公開所有你生成的密碼很容易! – Nanne

回答

0

實現jQuery插件,然後執行以下操作:

ajax.php(上如服務器test.com)

<?php 

$f_contents = file("..\sec\possiblewords.txt"); 
$word = $f_contents[rand(0, count($f_contents) - 1)]; 

function vowelreplacement() { 
    global $word; 
    $string = $word; 
    $trans = array("a" => "4", "e" => "3", "i" => "1", "o" => "0"); 

    return strtr($string, $trans); 
}   

echo vowelreplacement(); 
(在這裏您將您的形式PHP文件)

form.php的

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> 

<form id="register" action="registerprocess.php" method="post"> 
    <span><strong>Please note all fields are required</strong></span><br /><br /> 
    <label for="fullname">Full Name: </label><input type="text" id="fullname" name="fullname" required><br /> 
    <label for="email">Email Address: </label><input type="email" id="email" name="email" required><br /><br /> 
    <label for="manualpass">User-Defined Password: </label><input type="text" id="manualpass" name="manualpass" required><br /> 

    <form id="vowelremover" method="post">   
     <label for="generatedpass">Generated Password: </label><input type="text" id="generatedpass" name="generatedpass" value=<?php echo $generatedpassword;?> required><input type="submit" value="Generate Password"> 
    </form> 
    <input type="submit" value="register"> 
</form> 

<script type="text/javascript"> 

$(function() { 

    $("#vowelremover").submit(function() { 

     $("#generatedpass").load("http://test.com/ajax.php"); 

     return false; 
    }); 
}); 

</script> 
0

您可以通過AJAX新生成的密碼加載到密碼字段。用戶可以單擊密碼字段旁邊的鏈接以使用生成的密碼填寫該字段。

另一種選擇是用javascript生成密碼並將其放入字段中。那麼你不需要php腳本來生成js就可以簡化了。

1

如果你想用PHP生成你的密碼,你必須處理AJAX,然後把你的Ajax響應的結果放到使用Javascript的輸入中。

要麼也有很多JavaScript的密碼生成器,可以幫助您與此

-1

的jQuery插件有許多功能,可以簡化您的任務。看到HERE你正在尋找的樣品(刷新表單的一個元素)

-4

我已經遠遠好得多,功能齊全的VB代碼,隨機密碼生成器。這是我自己編寫的VB Express程序。用戶可以選擇密碼長度並從上限,下限,數字或特殊字符中選擇。您可以生成8至25個字符的無盡密碼。這裏是代碼和.RAR文件來下載完整的程序。最好的是...它的免費!

\\\======================================================================\\\ 
Option Explicit On 
'--------------------------------------------------------------------- 
'--------------------------------------------------------------------- 
'---------------------- Password Generator ® ------------------------- 
'---------------------- Author: Chirag Patel ------------------------- 
'---------------------- Current Version: 1.x ------------------------- 
'---------------------- Date: August, 14, 2015 ----------------------- 
'---------------------- License: FREE License Program ---------------- 
'--------------------------------------------------------------------- 
'--------------------------------------------------------------------- 

Public Class frmPassGen 
    'Declearing main intiger which will hold password 
    Dim Letters As New List(Of Integer) 

Public Function Info() 
    'This button provide information about author 
    Dim Infor As String 

    Infor = MsgBox("Program: Password Generator ®" & _ 
        vbCrLf & "---------------------------------" & _ 
        vbCrLf & "Version: 1.0" & _ 
        vbCrLf & "Programmer: Chirag Patel" & _ 
        vbCrLf & " " & _ 
        vbCrLf & "Legal Verbiage" & _ 
        vbCrLf & "-----------------" & _ 
        vbCrLf & "This program is FREE License program." & _ 
        vbCrLf & " " & _ 
        vbCrLf & "Thank you." & _ 
        vbCrLf & "Chirag Patel", MsgBoxStyle.Information, "Password Generator") 
    cmbLength.Focus() 
End Function 

Public Function Clear() 
    On Error Resume Next 
    'Clears ALL 
    cmbLength.Text = "000" 
    cmbLength.Focus() 
    txtPassword.Text = "... Your Password ..." 
    chkUpper.Checked = False 
    chkLower.Checked = False 
    chkNumber.Checked = False 
    chkSpecial.Checked = False 
    txtLPass.Text = "" 
    cmbNpass.Text = "000" 
End Function 

Public Function Upper() 
    'Add ASCII Codes For Upper Case Letters 
    For i As Integer = 65 To 90 
     Letters.Add(i) 
    Next 
End Function 

Public Function Lower() 
    'Add ASCII Codes For Lower Case Letters 
    For i As Integer = 97 To 122 
     Letters.Add(i) 
    Next 
End Function 

Public Function Numbers() 
    'Add ASCII Codes For Numbers 
    For i As Integer = 48 To 57 
     Letters.Add(i) 
    Next 
End Function 

Public Function Special() 
    'Add ASCII Codes For Special Characters 
    For i As Integer = 33 To 47 
     Letters.Add(i) 
    Next 
End Function 

Public Function uRemove() 
    Dim rRnd As New Random 
    Dim rSB As New System.Text.StringBuilder 
    Dim trPass As Integer 
    'Remove ASCII code for Upper case letters 
    For i As Integer = 65 To 90 
     Letters.Remove(i) 
    Next 
    'Remove ASCII Codes For Lower Case Letters 
    For i As Integer = 97 To 122 
     Letters.Remove(i) 
    Next 
    'Remove ASCII Codes For Numbers 
    For i As Integer = 48 To 57 
     Letters.Remove(i) 
    Next 
    'Remove ASCII Codes For Special Characters 
    For i As Integer = 33 To 47 
     Letters.Remove(i) 
    Next 
    'Remove cache 
    For count As Integer = -1 To Val(cmbLength.Text) 

     trPass = rRnd.Next(0, Letters.Count) 
     rSB.Append(Chr(Letters(trPass))) 

    Next 
    txtPassword.Text = rSB.ToString 
End Function 

Private Sub cmdExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExit.Click 
    On Error Resume Next 
    'Exit Program 
    Me.Close() 
    End 
End Sub 

Private Sub txtPassword_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtPassword.KeyDown 
    On Error Resume Next 
    'Doesn't allow to change password 
    txtPassword.Text = "" 
    MsgBox("You cannot change password here.", MsgBoxStyle.Information, "Password Generator") 
    txtPassword.Text = "... Your Password ..." 
    cmbLength.Text = "000" 
    cmbNpass.Text = "000" 
    cmbLength.Focus() 
End Sub 

Private Sub frmPassGen_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
    On Error Resume Next 
    'Default Settings 
    cmbLength.Text = "000" 
    cmbNpass.Text = "000" 
    cmdOK.Enabled = False 
    txtPassword.Text = "... Your Password ..." 
    cmbLength.Focus() 
    lbltTime.Text = TimeOfDay 
    Me.Text = "Password Generator - " & Format(Date.Now, "dddd, MM-dd-yyyy") 
End Sub 

Private Sub cmbLength_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbLength.SelectedIndexChanged 
    On Error Resume Next 
    'Enable Generate Button 
    If cmbLength.Text = "000" Then 
     cmdOK.Enabled = False 
    Else 
     cmdOK.Enabled = True 
    End If 
End Sub 

Private Sub cmdInfo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdInfo.Click 
    On Error Resume Next 
    'Display Program Info 
    Call Info() 
End Sub 
Private Sub cmdOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOK.Click 
    On Error Resume Next 
    For i As Integer = 0 To Val(cmbNpass.Text)    'Number of passwords need to generate 

     'Disabled OK (Generate) Button when no options are selected 
     If chkLower.Checked = False And chkNumber.Checked = False And chkSpecial.Checked = False And chkUpper.Checked = False Then 
      MsgBox("Please select at list one of the four options to generate new random password.", MsgBoxStyle.Information, "...Password Generator") 
      cmbLength.Focus() 
      cmdOK.Enabled = False 
     End If 

     'Decleration of Variables 
     Dim Rnd As New Random       ' Generates Random Strings 
     Dim SB As New System.Text.StringBuilder   ' Bings the String generated by Rnd 
     Dim tPass As Integer       ' Temp Variable to hold the generated string in temp continer 

     'Condition... 
     If cmbLength.Text = "000" And cmbNpass.Text = "000" Then 
      Beep() 
      MsgBox("You must select the length and numbers password.", MsgBoxStyle.Information, "Password Generator") 
     Else 
      cmdOK.Enabled = True 
     End If 
     'Creating password string... 

     For count As Integer = 1 To Val(cmbLength.Text)  'Length of password 

      tPass = Rnd.Next(0, Letters.Count) 
      SB.Append(Chr(Letters(tPass))) 
      Resume 
     Next 
     If Val(i) = 1 Then          'One Password Generated 
      txtPassword.Text = SB.ToString 
     Else 
      txtPassword.Text = SB.ToString      'More Password Generated 
      txtLPass.Text += txtPassword.Text & vbNewLine 
     End If 
    Next 


    'Disabled OK (Generate) Button when no options are selected 
    If chkLower.Checked = False And chkNumber.Checked = False And chkSpecial.Checked = False And chkUpper.Checked = False Then 
     cmbLength.Text = "000" And cmbNpass.Text = "000" 
    End If 
End Sub 

Private Sub cmbLength_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cmbLength.KeyDown 
    On Error Resume Next 
    'Doesn't allow users to alter the value in selection (drop down box) 
    cmbLength.Text = "" 
    cmbLength.Text = "000" 
    MsgBox("Sorry, you cannot alter the value in this area.", MsgBoxStyle.Information, "Password Generator") 
End Sub 

Private Sub chkUpper_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkUpper.CheckedChanged 
    If chkUpper.Checked = True Then 
     Call Upper() 
    Else 
     'Remove ASCII code for Upper case letters 
     For i As Integer = 65 To 90 
      Letters.Remove(i) 
     Next 
    End If 
End Sub 

Private Sub chkLower_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkLower.CheckedChanged 
    If chkLower.Checked = True Then 
     Call Lower() 
    Else 
     'Remove ASCII Codes For Lower Case Letters 
     For i As Integer = 97 To 122 
      Letters.Remove(i) 
     Next 
    End If 
End Sub 

Private Sub chkNumber_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkNumber.CheckedChanged 
    If chkNumber.Checked = True Then 
     Call Numbers() 
    Else 
     'Remove ASCII Codes For Numbers 
     For i As Integer = 48 To 57 
      Letters.Remove(i) 
     Next 
    End If 
End Sub 

Private Sub chkSpecial_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkSpecial.CheckedChanged 
    On Error Resume Next 
    'Checks if the Special Checkbox is checked 
    If chkSpecial.Checked = True Then 
     Call Special() 
    Else 
     'Remove ASCII Codes For Special Characters 
     For i As Integer = 33 To 47 
      Letters.Remove(i) 
     Next 
    End If 
End Sub 

Private Sub cmdClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClear.Click 
    On Error Resume Next 
    'Call Clear 
    Call Clear() 
End Sub 

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick 
    lbltTime.Text = TimeOfDay 
End Sub 

Private Sub txtLPass_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtLPass.KeyDown 
    On Error Resume Next 
    'Doesn't allow Edit... 
    MsgBox("You cannot alter the password here.", MsgBoxStyle.Information, "Password Generator") 
    Call Clear() 
End Sub 

Private Sub cmbNpass_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbNpass.SelectedIndexChanged 
    On Error Resume Next 
    'Enable Generate Button 
    If cmbNpass.Text = "000" Then 
     cmdOK.Enabled = False 
    Else 
     cmdOK.Enabled = True 
    End If 
End Sub 

末級

+0

「更好的解決方案」可能是您的意見。但是,我們不要放下別人的解決方案。另外,你發佈了這個問題,並在3年​​前回答了(接受*的答案)。而且......你不必談論你的解決方案是免費的(而且這不是推廣你的軟件的地方)。最後:您可能需要修正一些格式錯誤。 –