Jump to content

filter_input_array with $_POST and $_GET


dave25

Recommended Posts

How to filter input in array and declare all variables to prevent undefined index. I know how to use filter_input_array(INPUT_POST, ...) and filter_input_array(INPUT_GET, ...) separately, but i donk know how to use them together. Current code for testing.

<?php

$errors = array();
/* FILTER AND DECLARE VARIABLES */ 
$input['a'] = isset($_POST['a']) ? filter_input(INPUT_POST, 'a', FILTER_SANITIZE_STRING) : NULL;
$input['b'] = isset($_POST['b']) ? filter_input(INPUT_POST, 'b', FILTER_SANITIZE_NUMBER_INT) : NULL;
$input['c'] = isset($_GET['c']) ? filter_input(INPUT_GET, 'c', FILTER_SANITIZE_STRING) : NULL;
$input['d'] = isset($_GET['d']) ? filter_input(INPUT_GET, 'd', FILTER_SANITIZE_NUMBER_INT) : NULL;
$input['e'] = isset($_POST['e']) ? filter_input(INPUT_POST, 'e', FILTER_SANITIZE_NUMBER_INT, FILTER_REQUIRE_ARRAY) : NULL;
/* 
$input = array(
    'a' => NULL,
    'b' => NULL,
    'e' => NULL
);
*/
if(filter_has_var(INPUT_POST, 'submit')) {
	
	/* 
	$defs = array(
	    'a' => FILTER_SANITIZE_STRING,
	    'b'	=> FILTER_SANITIZE_NUMBER_INT,
	    'e'	=> array('filter' => FILTER_SANITIZE_NUMBER_INT,
                         'flags'  => FILTER_REQUIRE_ARRAY
                   )
	);
	*/
	if(empty($input['a'])) {
	    $errors[] = 'Error a.';
	}
	if(empty($input['b'])) {
        $errors[] = 'Error b.';
    }
	
}

if(filter_has_var(INPUT_POST, 'submit') and empty($errors)) {
	
	/* QUERYES */
	print 'SUCCESS';
	
}

if(count($errors)) {
	
	foreach($errors as $error) {
		print $error;
	}
      	
	
}

print "
<form method='post'>
A: <input type='text' name='a' value='".$input['a']."'> POST STRING <br>
B: <input type='text' name='b' value='".$input['b']."'> POST INT <br>
C: <input type='text' readonly value='".$input['c']."'> GET STRING <br>
D: <input type='text' readonly value='".$input['d']."'> GET INT <br>
<input type='submit' name='submit' value='SUBMIT'>
</form>";

?>
Link to comment
https://forums.phpfreaks.com/topic/296067-filter_input_array-with-_post-and-_get/
Share on other sites

I have tried with this way and its working and also dosent display any undefined indexes.

$input_POST = array(
	'a' => NULL,
    'b' => NULL,
	'e' => NULL
);
$input_GET = array(
	'c' => NULL,
	'd' => NULL
);
$input = array_merge($input_POST, $input_GET);

if(!empty($_GET)){
	
	$defs_GET = array(
		'c' => FILTER_SANITIZE_STRING,
		'd'	=> FILTER_SANITIZE_NUMBER_INT
	);
	
	$input_GET = filter_input_array(INPUT_GET, $defs_GET);
	
}

if(!empty($_POST)){
	
	$defs_POST = array(
		'a' => FILTER_SANITIZE_STRING,
		'b'	=> FILTER_SANITIZE_NUMBER_INT,
		'e'	=> array('filter' => FILTER_SANITIZE_NUMBER_INT, 'flags' => FILTER_REQUIRE_ARRAY)
	);
	
	$input_POST = filter_input_array(INPUT_POST, $defs_POST);
	
}

$input = array_merge($input_POST, $input_GET);
	
if(filter_has_var(INPUT_POST, 'submit')){
	
	if(empty($input['a'])) {
	    $errors[] = 'Error a.';
..........................................................

You could try something like this:

//IF POST OR GET METHOD WAS USED
if(in_array($_SERVER['REQUEST_METHOD'], array('POST', 'GET'))) {
     //DETERMINE WHICH FILTER TO USE
     $howToFilter = ($_SERVER['REQUEST_METHOD']=='POST') ? INPUT_POST : INPUT_GET;
 
     //GET INPUT
     $input['a'] = isset($_REQUEST['a']) ? filter_input($howToFilter, 'a', FILTER_SANITIZE_STRING) : NULL;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.