程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> 防止多個PHP表單提交

防止多個PHP表單提交

編輯:關於PHP編程

當提交一個HTML表單,它可能需要幾秒鐘前成功提交,並顯示響應頁面的形式。人們可以得到空閒,並單擊提交按鈕幾次,這可能會導致重復的表單提交。通常並不真的是一個問題,但在某些情況下,你可能要防止這種情況的發生。
下面你會發現兩個簡單的技巧,以防止重復提交,您可以使用這些或兩者的結合。
»防止多個表單提交使用Javascript
使用Javascript塊重復提交可能是最簡單的方法。當有人提交表單我們簡單地禁用Submit按鈕,可能改變它的值為更具描述性的東西,比如“提交,請稍候……”
試著點擊這個按鈕,例如。它仍將禁用,直到你重新加載此頁面:
第一步是要給你的提交按鈕一個惟一的id,例如id =“myButton“:
<input type="submit" value="Submit" id="myButton" />
第二個(也是最後一次)的步驟是給兩個Javascript命令<form>標記。第一個將告訴浏覽器禁用submit按鈕的表單被提交之後,第二個將更改按鈕的文本來給用戶一些知道發生了什麼。這是代碼添加到你的表單標記:
onsubmit="document.getElementById('myButton').disabled=true;
document.getElementById('myButton').value='Submitting, please wait...';"
你的表單標記將類似於:
<form action="contact.php" method="post"
onsubmit="document.getElementById('myButton').disabled=true;
document.getElementById('myButton').value='Submitting, please wait...';"
>
就是這樣。這種方法應該於大多數的浏覽器(IE  +,FireFox、Opera、…)。
»防止多個表單提交使用cookie
如果你想避免重復提交的整個浏覽器會話(或更長),你可以考慮使用Cookie。例如編輯自己的表單處理腳本的浏覽器發送cookie的形式後,已被處理,但在此之前的任何HTML或重定向打印頭被。將這段代碼的mail()命令後,應在大多數情況下:
setcookie('FormSubmitted', '1');
然後在處理之前檢查cookie。如果有這個訪問者已經提交了表單在活躍的浏覽器會話。將下列代碼添加到窗體的開始處理腳本:
if (isset($_COOKIE['FormSubmitted'])
{
    die('You may only submit this form once per session!');
}
就是這樣!
如果修改,以阻止重復提交與cookies。注意:在上面的腳本和cookie的打印代碼調用後添加一些代碼的mail()函數:
<?php
/* Prevent duplicate submissions */
if (isset($_COOKIE['FormSubmitted'])
{
    show_error('You may only submit this form once per session!');
}

/* Set e-mail recipient */
$myemail  = "[email protected]";

/* Check all form inputs using check_input function */
$yourname = check_input($_POST['yourname'], "Enter your name");
$subject  = check_input($_POST['subject'], "Write a subject");
$email    = check_input($_POST['email']);
$website  = check_input($_POST['website']);
$likeit   = check_input($_POST['likeit']);
$how_find = check_input($_POST['how']);
$comments = check_input($_POST['comments'], "Write your comments");
//www.software8.co 原文
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
    show_error("E-mail address not valid");
}

/* If URL is not valid set $website to empty */
if (!preg_match("/^(https?:\/\/+[\w\-]+\.[\w\-]+)/i", $website))
{
    $website = '';
}

/* Let's prepare the message for the e-mail */
$message = "Hello!

Your contact form has been submitted by:

Name: $yourname
E-mail: $email
URL: $website

Like the website? $likeit
How did he/she find it? $how_find

Comments:
$comments

End of message
";

/* Send the message using mail() function */
mail($myemail, $subject, $message);

/* Set a cookie to prevent duplicate submissions */
setcookie('FormSubmitted', '1');

/* Redirect visitor to the thank you page */
header('Location: thanks.htm');
exit();

/* Functions we used */
function check_input($data, $problem='')
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    if ($problem && strlen($data) == 0)
    {
        show_error($problem);
    }
    return $data;
}

function show_error($myError)
{
?>
    <html>
    <body>

    <b>Please correct the following error:</b><br />
    <?php echo $myError; ?>

    </body>
    </html>
<?php
exit();
}
?

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved