processing GET and POST variables

From: DH (doug861_at_comcast.net)
Date: 12/31/04


Date: Fri, 31 Dec 2004 14:41:15 GMT

Below is a function adapted from one found in phpMyEdit.class.php
http://platon.sk/projects/release_list_page.php?project_id=5
phpMyEdit is a form generator for PHP/MySQL.

I think this function works great, especially for forms where data is
redisplayed due to failed validation of user input. I wonder what the
opinion of other PHP users would be.

function get_cgi_var($name, $default_value = null)
{
    // From the phpMyEdit project
    // Usage: $name = get_cgi_var('name');
    static $magic_quotes_gpc = null;
    if ($magic_quotes_gpc === null) {
       $magic_quotes_gpc = get_magic_quotes_gpc();
    }
    global $HTTP_GET_VARS;
    $var = @$HTTP_GET_VARS[$name];
    if (! isset($var)) {
       global $HTTP_POST_VARS;
       $var = @$HTTP_POST_VARS[$name];
    }
    if (isset($var)) {
       if ($magic_quotes_gpc) {
          if (is_array($var)) {
             foreach (array_keys($var) as $key) {
                $var[$key] = stripslashes(trim(strip_tags($var[$key])));
             }
             return $var;
          } else {
             $var = stripslashes(trim(strip_tags($var)));
          }
       }
    } else {
       $var = @$default_value;
    }
    return $var;
    // If data is displayed/posted using htmlspecialchars($var)
    // return @$HTTP_POST_VARS ? html_entity_decode($var, ENT_QUOTES) :
$var;
};

$name = get_cgi_var('name');