2014-06-28 102 views
0

請我需要您的幫助。如何在頁面刷新時防止空白張貼

我有一個在線表格(http://mmg2015.org/participationform.php),它將數據發佈到後端數據庫。它的作品非常好。但我決定添加一個電子郵件代碼,如下所示,這樣當表單提交時,表單內容的副本將被髮送到電子郵箱,以便管理員能夠確認並作出迴應。

現在,因爲我添加了電子郵件代碼,所以當頁面/表單刷新時,表單會向我的電子郵箱中留下空白字段。由於沒有內容,但我的電子郵件不斷收到表單中的空電子郵件,因此沒有任何內容發佈到數據庫。

任何人都可以幫我確定爲什麼它張貼空白。

<?php 
$emailSubject = 'A new application for participation!'; 
$webMaster = '[email protected]'; 

// $email and $message are the data that is being 
// posted to this page from our html contact form 
$nameoforganisation = $_POST['nameoforganisation'] ; 
$sector = $_POST['sector'] ; 
$address = $_POST['address'] ; 
$email = $_POST['p1email'] ; 
$p1name = $_POST['p1name'] ; 
$p1phone = $_POST['p1phone'] ; 
$p1designation = $_POST['p1designation'] ; 
$p1email = $_POST['p1email'] ; 
$p2name = $_POST['p2name'] ; 
$p2phone = $_POST['p2phone'] ; 
$p2designation = $_POST['p2designation'] ; 
$p2email = $_POST['p2email'] ; 
$signature = $_POST['signature'] ; 

$body = <<<EOD 
<br><hr><br> 
Name of Organisation: $nameoforganisation <br> 
Sector: $sector <br> 
Address: $address <br> 

First Participants Details:<br> 

Name: $p1name <br> 
Phone: $p1phone <br> 
Designation: $p1designation <br> 
Email: $p1email <br> 

Second Participants Details:<br> 

Name: $p2name <br> 
Phone: $p2phone <br> 
Designation: $p2designation <br> 
Email: $p2email <br> 

Signature: $signature <br> 
EOD; 
$headers = "From: $email\r\n"; 
$headers .= "Content-type: text/html\r\n"; 
$success = mail($webMaster, $emailSubject, $body, $headers); 
?> 

我用大量的感謝

邁克

+0

您的代碼易受頭部注入影響。 – PeeHaa

+0

那麼http://en.wikipedia.org/wiki/Post/Redirect/Get呢? – rkosegi

+0

@PeeHaa:請問我該如何處理?你能給我一個關於如何處理標題注入的提示 –

回答

0

您可以添加以下不同情況:

if(isset($_POST['user_form_submit_button_name'])) 
{ 
//put your insert code and send mail from here 
} 

另一個原因是: 後插入數據庫,你會得到最後插入的ID。把一個條件ID代碼得到最後插入ID然後發送郵件。

+0

我相信你的意思是提交按鈕正確?同時感謝您的評論。 –

+0

我只是添加了你發送的代碼,並用'Submit'取代了'user_form_submit_button_name',上傳了頁面,刷新了3次,並檢查了我的電子郵箱,沒有像往常一樣發送空郵件。謝謝你們。接下來要做的是標題注入 –

0

的欣賞你不檢查,看是否發生了表單提交,或者即使你有有效的數據您發送電子郵件之前。因此,每次加載頁面都會導致發送電子郵件。

總結這段代碼中的if語句,在一個表單提交至少檢查(雖然驗證收到的數據應該會繼續進行):這讓你的腳本弱勢

<?php 
if($_SERVER['REQUEST_METHOD'] === 'POST') { 
    $emailSubject = 'A new application for participation!'; 
    $webMaster = '[email protected]'; 

    // $email and $message are the data that is being 
    // posted to this page from our html contact form 
    $nameoforganisation = $_POST['nameoforganisation'] ; 
    $sector = $_POST['sector'] ; 
    $address = $_POST['address'] ; 
    $email = $_POST['p1email'] ; 
    $p1name = $_POST['p1name'] ; 
    $p1phone = $_POST['p1phone'] ; 
    $p1designation = $_POST['p1designation'] ; 
    $p1email = $_POST['p1email'] ; 
    $p2name = $_POST['p2name'] ; 
    $p2phone = $_POST['p2phone'] ; 
    $p2designation = $_POST['p2designation'] ; 
    $p2email = $_POST['p2email'] ; 
    $signature = $_POST['signature'] ; 

    $body = <<<EOD 
    <br><hr><br> 
    Name of Organisation: $nameoforganisation <br> 
    Sector: $sector <br> 
    Address: $address <br> 

    First Participants Details:<br> 

    Name: $p1name <br> 
    Phone: $p1phone <br> 
    Designation: $p1designation <br> 
    Email: $p1email <br> 

    Second Participants Details:<br> 

    Name: $p2name <br> 
    Phone: $p2phone <br> 
    Designation: $p2designation <br> 
    Email: $p2email <br> 

    Signature: $signature <br> 
    EOD; 
    $headers = "From: $email\r\n"; 
    $headers .= "Content-type: text/html\r\n"; 
    $success = mail($webMaster, $emailSubject, $body, $headers); 
} 
?> 

你也不要sanitize the submitted data發送垃圾郵件。

+0

感謝您的意見。可以使用$ editFormAction = $ _SERVER ['PHP_SELF']; (isset($ _ SERVER ['QUERY_STRING'])){........因爲它在我的代碼系統中已經有了我在.Dreamweaver生成的代碼。如果沒有,我可以在那裏使用嗎? –

+0

我真的不明白你的意思是「REQUEST_METHOD」,還是應該按照你寫的方式使用代碼? –