簡體   English   中英

同一頁面上的PHP表單驗證-此邏輯正確嗎?

[英]PHP form validation on same page - is this logic correct?

我正在嘗試構建一個簡單的注冊表單,該表單將通過一些檢查,然后將數據保存到數據庫中。 最初,只有注冊表格和處理表格,而未經驗證的表格數據已保存在數據庫中。 當我嘗試對過程進行驗證時,麻煩就開始了。

    <Edit>

這里只有三個目標:

  1. 為了防止已登錄的用戶嘗試注冊。
  2. 只要瀏覽器中沒有運行活動的用戶帳戶,請首先確保沒有空字段,
  3. 發送表格數據進行處理。

對於#1,發布到php self似乎是理想的選擇,因為它為我避免了很多額外的編碼,並且對用戶具有不必再次鍵入所有內容的優點。

對於#2,驗證僅限於現在檢查空白字段,僅此而已。 這是一張注冊表格,正在收集用戶數據-完全不對用戶進行身份驗證。

對於#3,我設計了一種將數據發送到另一個php的方法,這不再是問題。

問題是邏輯。 為什么分開工作的零件放在一起會失效? 我對流程應用的邏輯中是否有錯誤?

其中一條評論談到了elseif,我也嘗試過。 我沒有在代碼中出現任何錯誤-沒有解析錯誤,沒有語法錯誤,沒有致命錯誤-但也沒有發生任何事情-只是刷新了注冊表。 系統的各個部分分別在測試頁上工作,但是當與完整表單放在一起時,它將不起作用。

    </Edit>

在SE上,我找到了將表單發布到php self的方法,並在發布后的示例代碼中進行了嘗試。 它按預期工作,一切似乎都很好,因此我將其添加到了表單頁面。 但是在實際的表單頁面上,除了重新加載之外,它什么也沒做。

再次,在SE上,我發現了一個帖子,展示了如何將所有錯誤捕獲到數組中並顯示出來。 它似乎可以在一個示例文件上工作,我在變量注冊頁面中添加了對變量名進行適當更改的代碼。 如果沒有用戶登錄,並且用戶單擊“提交”,則應顯示捕獲到的空字段錯誤。 沒有。 即使所有字段都留為空白,也會顯示任何錯誤。 此后一切都崩潰了。

現在發生的一切就是重新加載了注冊表-錯誤或沒有錯誤!

我已經嘗試了很多事情,以至於我不再確定自從昨晚以來我一直在試圖做的事情就是代碼現在正在做的事情。 因此,我僅從邏輯和相關問題開始。

這是每個階段的邏輯和問題。

    <php session_start() ;?>
    <?php
    //check if the user is logged in
    if (isset($_SESSION['validuser'])) {
          //catch this first - before user spends time filling out the 12 fields!
          //send to message page saying "you cannot register - you are already a member and     logged in"
          //WORKING PERFECTLY - CONFIRMS FORM POSTING TO SAME PAGE CORRECTLY!
          //time for other checks....
    }
    else (!isset($_SESSION['validuser']) && isset($_POST['submit'])) {

          //if there is no logged in user and form has been submitted, start checking the      fields to see if any fields are empty
          //collect all errors in array and display?
          //direct back to the form with appropriate messages, form fields retained
          //exit here? or no?
          //focus  now has to pass to the form again - need any code for this? or will it happen automatically?
    }
    if isset($_POST['submit'])) {
          //Should this part be within the else block?
          //If it is out side, will it get rendered on load?
          //if there are no errors proceed to process the form
          //there are further checks that need talking to the database
          //once those checks and approvals are over the data gets saved to the database

    }


    ?>
    <html>
       <body>
        <form action="<?=$_SERVER['PHP_SELF']?>" method = "POST">
            <!--  12 fields to be filled out by user     -->
            <input type = "submit" name = "submit" value = "submit" />
        </form>
        </body>
    </html>

我想念什么? 請記住,這是我要清理的工作流程。 現在,原始代碼已變得如此繁瑣,以至於我不想在這里公開它-很有可能我將獲得有關輸入衛生和輸出轉義的很多建議! 在此開發環境中,我故意省略了那些位,在這些環境中,只有我才能訪問表單和數據庫。 在這一點上進行消毒和逃逸只會增加混亂。

就是說,一旦我擁有正確的工作流程,我也將添加這些位:-)

我將其重組如下:

if (isset($_SESSION['validuser'])) 
{
    //user is authenticated
}
elseif (!isset($_SESSION['validuser']) && isset($_POST['submit'])) 
{
    //user is not authenticated, proceed
    if (isset($_POST['submit'])) //checking if form was submitted
    {
        //process 12 input fields
    }
    else
    {
        //form wasn't submitted, display error
    }
}

這是一個應根據您的描述而起作用的解決方案。

<?php
    session_start();

    // If we have a "valid users"
    if (isset($_SESSION['validuser']))
    {
        // Display stuff for registered users
    }
    else // If we don't have a valid user
    {
        // If there was data submitted to the server
        if (isset($_POST))
        {
            // Handle validation for ... whatever
            // print_r($_POST); To see data
        }
    }
?>

<html>
     <body>
        <!-- Don't display the form to "valid users". -->
        <?php if (!isset($_SESSION['validuser'])): ?>
             <form action="<?=$_SERVER['PHP_SELF']?>" method = "POST">
                 <!--  12 fields to be filled out by user     -->
                 <input type = "submit" name = "submit" value = "submit" />
             </form>
        <?php endif; ?>
     </body>
</html>

您基本上是對的,我會這樣做的

 if (isset($_SESSION['validuser'])) {

}else {

    if isset($_POST['submit'])) {
         //Check errors here
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM