processing GET and POST variables

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • DH

    processing GET and POST variables

    Below is a function adapted from one found in phpMyEdit.class .php

    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($na me, $default_value = null)
    {
    // From the phpMyEdit project
    // Usage: $name = get_cgi_var('na me');
    static $magic_quotes_g pc = null;
    if ($magic_quotes_ gpc === null) {
    $magic_quotes_g pc = get_magic_quote s_gpc();
    }
    global $HTTP_GET_VARS;
    $var = @$HTTP_GET_VARS[$name];
    if (! isset($var)) {
    global $HTTP_POST_VARS ;
    $var = @$HTTP_POST_VAR S[$name];
    }
    if (isset($var)) {
    if ($magic_quotes_ gpc) {
    if (is_array($var) ) {
    foreach (array_keys($va r) as $key) {
    $var[$key] = stripslashes(tr im(strip_tags($ var[$key])));
    }
    return $var;
    } else {
    $var = stripslashes(tr im(strip_tags($ var)));
    }
    }
    } else {
    $var = @$default_value ;
    }
    return $var;
    // If data is displayed/posted using htmlspecialchar s($var)
    // return @$HTTP_POST_VAR S ? html_entity_dec ode($var, ENT_QUOTES) :
    $var;
    };



    $name = get_cgi_var('na me');

  • Janwillem Borleffs

    #2
    Re: processing GET and POST variables

    DH wrote:[color=blue]
    > 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.
    >[/color]

    Can be written in less lines with the aid of PHP's superglobals:

    <?php

    function get_cgi_var($na me, $default_value = null) {
    if ($var = @$_REQUEST[$name]) {
    if (get_magic_quot es_gpc()) {
    if (!is_array($var )) {
    $var = stripslashes($v ar);
    } else {
    foreach (array_keys($va r) as $key) {
    $var[$key] = stripslashes($v ar);
    }
    }
    }
    } else {
    $var = $default_value;
    }
    return $var;
    }

    ?>


    JW


    Comment

    Working...