2013-04-30 22 views
1

在頁面刷新或交的數據有沒有辦法知道,如果頁面刷新或數據被張貼在同一頁上的數據?PHP - 檢查同一頁

要更具體一點:

  • 我必須在同一頁上發佈的數據。

  • 這影響了查詢的WHERE條件。

  • 如果頁面刷新,那麼這裏的條件必須是1

  • 否則,在條件包含了一些ID,從 表中得到具體的數據。

+1

你無法知道這是否是一個刷新或第一個請求,除非你是第一次請求 – zerkms 2013-04-30 11:24:05

+0

之後加入一些標誌,在服務器端的頁面,我認爲你的問題是,當用戶刷新頁面不應該再次保存數據。 – Roopendra 2013-04-30 11:27:08

+3

也許這是一個[XY問題]的問題(http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)?你不是在尋找[PRG模式](http://en.wikipedia.org/wiki/Post/Redirect/Get)嗎? – PeeHaa 2013-04-30 11:28:26

回答

1
在頁面頂部

只包括

if(isset($_POST['name']) && $_POST['name']!=''){ 
//your code goes here 

} 
+0

-1代碼不起作用。它不回答這個問題。 – bestprogrammerintheworld 2013-04-30 14:10:47

0

我建議你檢查要求

//Here goes the code 
session_start(); 
$counter = 0; 
$counter = (isset($_SESSION['param'])) ? $counter++ : 0; 

if($counter == 0) 
    echo "data GET or POST"; 
else 
    echo "refreshed"; 

**如果你只想要POST PARAM,使用$ _POST而不是$的_REQUEST

+0

刷新完全相同的請求發送 – zerkms 2013-04-30 11:28:08

+0

好的,在這種情況下,將發送相同的參數。讓我改變這個.. – Prashanth 2013-04-30 11:29:02

2

最好的辦法是使用PHP sessions,以及您提交的數據$_POST。讓我們假設在這個例子中,你有以下形式:

<form action="this_page.php" method="post"> 
    <input type="text" name="important-info" /> 
    <input type="submit" value="Submit" /> 
</form> 

然後在其他地方在同一個頁面是PHP代碼:

<?php 
// example code 
session_start(); 

if (!isset($_SESSION['previousVisitor']) && isset($_POST['important-info'])) { 
    // this is a new visitor who has submitted the form 
    $_SESSION['previousVisitor'] = true; 
    // where is based on $_POST['important-info'] 
} else() { 
    // where is 1 
} 

// close the session after you do what you need - this stops large pages causing hang 
session_destroy(); 

請注意,他們可以通過刪除其cookie清除該會話變量。

+0

的注意事項有關'session_destroy()'是怪異 – zerkms 2013-04-30 11:29:15

+0

它使我頭痛了一段時間後(http://stackoverflow.com/questions/10911429/do-php-timeouts-stop-people-on-相同的網絡加載頁面) - 我認爲值得一提。 – LeonardChallis 2013-04-30 11:33:38

+0

感謝您的快速回復。 嗯,我想這個問題可能被誤解。我在頁面上有一個提交按鈕,在同一頁面上發佈數據。我需要知道按鈕是否被點擊或頁面被刷新。 使用會議previousvisitor,我總是得到1(在刷新和提交按鈕點擊的情況下) – 2013-04-30 11:40:52