2012-05-08 93 views
0

我想實現一個用戶無法發送兩次相同的輸入。我使用php腳本來提交用戶輸入。php會話驗證

我的想法是將他的輸入保存在會話數組中,並且每次他提交某些數組檢查時,如果它匹配他之前提交的其中一件事情。

的代碼看起來是這樣的:

//Compare post with what user has posted so far; if the same, exit (spam protection) 
    foreach($_SESSION['postarray'][] as $postarray) if($post=$postarray) exit; 

    //Save post in session of all other posts 
    $_SESSION['postarray'][]=$post; 

我收到以下錯誤:

致命錯誤:無法使用[]在/應用程序讀取/ XAMPP/xamppfiles/htdocs中/ postish /動作/第32行的post.php(指向foreach()循環的行)

即使將函數更改爲僅$ _SESSION ['post array'],也不起作用。

任何幫助非常感謝:)

丹尼斯

+0

錯誤消息表明''[]「語法」是錯誤的。嘗試刪除空括號。而'='是用於分配的。 '=='是爲了條件。 –

+0

粗心的錯誤...謝謝! – weltschmerz

+0

作爲實施方法,前提存在根本性缺陷。你如何處理不斷增長的會議?爲什麼不在存儲層?我懷疑你真的想阻止用戶多次提交同一個表單的實例 - 在這種情況下,這應該在客戶端進行管理。另見CSRF預防。 – symcbean

回答

4

操作[]增加了新的元素數組。

爲了讓你不得不使用它的foreach循環元素,而[]

foreach($_SESSION['postarray'] as $postarray) 
    if ($post == $postarray) exit; 

並注意比較操作==這是用來檢查變量的平等。

更好的選擇,以檢查是否存在$_SESSION['postarray']$post是使用in_array功能:

if (in_array($post, $_SESSION['postarray'])) exit; 
+0

工程就像一個魅力!謝謝:) – weltschmerz

+0

不客氣;) – VisioN

2

您不小心使用了賦值運算符=,而不是水平的研究操作=====

因此,你需要用if($post == $postarray)

您還需要去掉[]foreach($_SESSION['postarray'][] as $postarray)[]更換if($post=$postarray)插入一個新的數組時才使用。