Welcome Guest, Not a member yet? Register   Sign In
isset: without or with?
#1

[eluser]Kenan[/eluser]
hi! nice to meet u! Smile

can u say which of the codes below is correct? should isset() to be existed or not?

Code:
$q = $this->input->post('q');

if (!isset($q)) {
exit();
}

Code:
$q = $this->input->post('q');

if (!$q) {
exit();
}

AND IS IT CORRECT TO CHECK THE EXISTING OF POST DATA AFTER DEFINING VARIABLE, OR I MUST CHECK BEFORE, THEN DEFINE?
#2

[eluser]CroNiX[/eluser]
isset() will always be true in that case because this->input->post() will return boolean false if the variable does not exist, so it will ALWAYS be set (either FALSE, or a value). So you want to check with the === operator.

Code:
$q = $this->input->post('q');
if ($q !== FALSE) //variable existed

You don't want to use (!$q), because what if the post value was "0", which is a legitimate post value in a lot of forms.
#3

[eluser]Kenan[/eluser]
[quote author="CroNiX" date="1404664228"]isset() will always be true in that case because this->input->post() will return boolean false if the variable does not exist, so it will ALWAYS be set (either FALSE, or a value). So you want to check with the === operator.

Code:
$q = $this->input->post('q');
if ($q !== FALSE) //variable existed

You don't want to use (!$q), because what if the post value was "0", which is a legitimate post value in a lot of forms.[/quote]

thanks for answer! but.. when i use cleaning such as

Code:
$q = htmlspecialchars(strip_tags(stripslashes(trim($this->input->post('q')))));

or such as

Code:
$q = $this->security->xss_clean($this->input->post('q'));

it doesn't work, i mean === doesn't work, and == works, that's why i confused

and my last question at the topic wasn't answered, pls
#4

[eluser]jonez[/eluser]
If you want to check and validate specific keys I usually do this;
Code:
$data = $this->input->post( );
if ( !is_array( $data ) ) $data = array( );

if ( array_key_exists( 'q', $data ) ) {
    //clean
}
#5

[eluser]Kenan[/eluser]
[quote author="jonez" date="1404687947"]If you want to check and validate specific keys I usually do this;
Code:
$data = $this->input->post( );
if ( !is_array( $data ) ) $data = array( );

if ( array_key_exists( 'q', $data ) ) {
    //clean
}
[/quote]

thanks for answer, good idea but the code will be more, because i must check a lot of keys of post data, let me wait for the other answers, if there is no other solution, i will use ur variant Smile
#6

[eluser]jonez[/eluser]
You can also define defaults, then merge the post data with those defaults to ensure every key exists with at least a default value.

Code:
$defaults = array(
'q' => null,
);

$data = $this->input->post( );
if ( !is_array( $data ) ) $data = array( );
$data = array_merge( $defaults, $data );

if ( $data[ 'q' ] !== null ) {
//clean
}

Or with some helpers in MY_array_helper.php:
Code:
function trim_array( $arr ) {
foreach ( $arr as $k => $v ) {
  if ( is_scalar( $v ) ) {
   $arr[ $k ] = trim( $v );
  }
  else if ( is_array( $v ) ) {
   $arr[ $k ] = trim_array( $v );
  }
  else {
   $arr[ $k ] = $v;
  }
}

return $arr;
}

function merge_array( ) {
$args = func_get_args( );
$result = array( );

foreach ( $args as $arr ) {
  $arr = ( is_array( $arr ) ) ? trim_array( $arr ) : array( );

  $result = array_merge( $result, $arr );
}

return $result;
}

Code:
$defaults = array(
'q' => null,
);

$data = merge_array( $defaults, $this->input->post( ) );

if ( $data[ 'q' ] !== null ) {
//clean
}
#7

[eluser]Kenan[/eluser]
[quote author="jonez" date="1404734475"]You can also define defaults, then merge the post data with those defaults to ensure every key exists with at least a default value.

Code:
$defaults = array(
'q' => null,
);

$data = $this->input->post( );
if ( !is_array( $data ) ) $data = array( );
$data = array_merge( $defaults, $data );

if ( $data[ 'q' ] !== null ) {
//clean
}

Or with some helpers in MY_array_helper.php:
Code:
function trim_array( $arr ) {
foreach ( $arr as $k => $v ) {
  if ( is_scalar( $v ) ) {
   $arr[ $k ] = trim( $v );
  }
  else if ( is_array( $v ) ) {
   $arr[ $k ] = trim_array( $v );
  }
  else {
   $arr[ $k ] = $v;
  }
}

return $arr;
}

function merge_array( ) {
$args = func_get_args( );
$result = array( );

foreach ( $args as $arr ) {
  $arr = ( is_array( $arr ) ) ? trim_array( $arr ) : array( );

  $result = array_merge( $result, $arr );
}

return $result;
}

Code:
$defaults = array(
'q' => null,
);

$data = merge_array( $defaults, $this->input->post( ) );

if ( $data[ 'q' ] !== null ) {
//clean
}
[/quote]

hmm thanks bro, i will use it when i will need it Smile
#8

[eluser]Kenan[/eluser]
waiting for the other ideas...




Theme © iAndrew 2016 - Forum software by © MyBB