SysTools Logo SysTools


PHP: $_GET, $_POST, etc. helpers


<?php

/*

  Helpers function which return values if they are defined.
  Default return value could be specified as second parameter.
  Note that "null" value can't be returned via $_GET, $_POST and $_COOKIE -
  that's why it's used as default return value for all functions here.

  // Example #1:
  $q = valget('q');
  if (!is_null($q)) {
    // $q defined
  } {
    // $q undefined
  }

  // Example #2:
  // intval() and abs() - SQL-injection shall not pass!
  $articleid = abs(intval(valget('article', '0')));

*/

function valget($key, $value = null) {
  return(array_key_exists($key, $_GET) ? $_GET[$key] : $value);
}

function valpost($key, $value = null) {
  if (array_key_exists($key, $_POST)) {
    $value = get_magic_quotes_gpc() ? stripslashes($_POST[$key]) : $_POST[$key];
  }
  return($value);
}

function valcookie($key, $value = null) {
  return(array_key_exists($key, $_COOKIE) ? $_COOKIE[$key] : $value);
}

function valserver($key, $value = null) {
  return(array_key_exists($key, $_SERVER) ? $_SERVER[$key] : $value);
}

function valsession($key, $value = null) {
  return(array_key_exists($key, $_SESSION) ? $_SESSION[$key] : $value);
}

2015.04.15


[ Код ]