2012-01-18 67 views
0

可能重複:
Avoid resending forms on php pages當PHP頁面被刷新,瀏覽器會彈出一條消息,用戶

的Index.html

<form method="post" action="demo.php"> 

      <input type="text" name="fname"/> 
      <input type="text" name="lname"/> 
</form> 

演示。 PHP

<?php 
     $firstname = $_POST['fname']; 
     $lastname = $_POST['lname']; 
     //some more php code to fill the webpage 
    ?> 

因此,用戶輸入他的名字和姓氏,並提交表單,然後demo.php它的工作,其工作正常,但是當我按F5或下一次我得到這個刷新demo.php從瀏覽器彈出

 // this message is from google chrome 
    the page that you're looking used information that you entered. 
    Returning to that page might cause any action to be repeated. 
    Do you want to continute? 

    // this message is from IE 7 or 8 
    To display the webpage again,Internet Explorer needs to resend information 
    you've previously submitted. If you were making a purchase, 
    you should click cancel to avoid duplicate transaction,else click retry. 

爲什麼我得到這個?我只想刷新頁面。我不希望該消息從瀏覽器中彈出。根據之前提交的值刷新頁面,因爲它在我的數據庫中創建重複值。

回答

2

您需要使用HTTP重定向響應,推薦的流量是處理髮布數據然後重定向到另一個網頁的處理的數據的內部標識符。

如果刷新網頁剛剛從POST返回,那麼預期的行爲是再拍了POST,作爲最後的時間相同的值。

header功能可用於這一目的在PHP http://php.net/manual/en/function.header.php

爲了保持狀態,你可以使用PHP $_SESSION或重定向的URL的查詢字符串中傳遞的數據重定向。

0

你得到這個消息,因爲刷新頁面發送相同的請求,因爲當先前未加載的頁面一樣。就你而言,這是表格的提交。瀏覽器會發出警告,如果你刷新頁面,POST數據將再次發送,如果腳本沒有被保護,這也許可以在數據庫或類似的東西重複值。

一個解決這個問題的辦法是用戶重定向到同一頁面(POST數據總是在失去後重定向)。如果你想成爲安全的,即使用戶按下「返回」重定向後重新提交數據,here的解決方案。

相關問題