2014-04-28 55 views
0

我一直在編寫一個腳本,而我被困在一小部分。我正在檢查以確保用戶提交的表單是安全的。但我不知道如何處理下拉框,這是我到目前爲止的代碼:如何檢查下拉框是否匹配if語句[PHP]

if ($_POST['type'] != '1' || $_POST['type'] != '2' || $_POST['type'] != '3') // Checks to see if the submitted type matches, prevents exploiting. 
{ 
    $supportErrors[] = "Support type you've chosen was not found, possibly trying to exploit this script?"; 
} 

我不知道這是否是檢查其價值的正確方法。這是我對腳本的這一部分HTML端:現在

<select name="type" id="type" size="1" style="border: 1px solid #000000; background-color: #FFFFFF;"> 
     <option value="1" selected>Support</option> 
     <option value="2">Question</option> 
     <option value="3">Complaint</option> 
</select> 

,我的理解,價值是被張貼VIA「類型」數據 - 所以,在我看來,應該不是張貼價值(1或2或3),然後在PHP方面,它檢查它是否不是1,2或3?

+1

什麼錯誤? –

+0

有什麼錯誤?請添加錯誤 – lagbox

+0

沒有錯誤,它返回$ supportErrors [] - 所以它不能正常工作。 – Reverb

回答

2

in_array()是一個很好的解決方案,如果一樣東西相匹配的多個選項

if (!in_array($_POST['type'],array(1,2,3))){..} 

你一個應該在開啓時出現錯誤:

error_reporting(E_ALL); 
ini_set('display_errors', 1); 
+0

不起作用。你看,空白頁面。 – Reverb

+0

我的壞額外)需要,修復。但空白頁意味着你的錯誤檢查開發,這是一個壞主意 –

+0

乾杯,迄今的作品! – Reverb

1

您需要的條件邏輯從或更改AND:檢查時

if($_POST['type'] != '1' && $_POST['type'] != '2' && $_POST['type'] != '3')...

0

請務必使用ISSET檢查的形式張貼

<?php 
if (isset($_POST['type']) and $_POST['type'] != '1' || $_POST['type'] != '2' || $_POST['type'] != '3') // Checks to see if the submitted type matches, prevents exploiting. 
{ 
    $supportErrors[] = ("Support type you've chosen was not found, possibly trying to exploit this script?"); 
} 
?> 
+0

某些原因,它仍然返回$ supporterrors .. – Reverb

0
// Assumimg type as a numeric value , I might achieve it like this 

    $type = 0; 

    if(isset($_POST['type'])) $type = trim($_POST['type']) * 1; 

    if($type < 1) 
    { 
     $supportErrors[] = "Please specify type"; 
    } 
    elseif($type > 3) 
    { 
     $supportErrors[] = "Invalid type"; 
    }