Safely extracting variables in PHP
by Amit Arora
Published: August 7, 2003
User Rating: 7.7 (20 votes)

Page Page » 1 2 3 4 5

Code

The complete code of the SafeExtract() is as follows

/* SafeExtract() * Extract GET, POST, COOKIE variables into gloabal (scope) * © Amit Arora <digitalamit dot com> * Permission give to use this code for Non-Commericial, Commericial use * It would be appreciated if you could provide a link to the site */ if (phpversion() < '4.1.0') { $_GET = &$HTTP_GET_VARS; // For compatibility with PHP 3.x $_POST = &$HTTP_POST_VARS; $_COOKIE = &$HTTP_COOKIE_VARS; } function SafeExtract() { global $_GET, $_POST, $_COOKIE; $bQuote = get_magic_quotes_gpc(); foreach( func_get_args() as $v ) { if( is_array( $v ) ) while( list( $key, $value ) = each( $v ) ) { switch ( $key ) { case 'any': if ( is_array( $value ) ) foreach ( $value as $e ) { if ( isset($_COOKIE[$e]) ) $GLOBALS[$e] = ($bQuote ? stripslashes($_COOKIE[$e]) : $_COOKIE[$e]); if ( isset($_POST[$e]) ) $GLOBALS[$e] = ($bQuote ? stripslashes($_POST[$e]) : $_POST[$e]); if ( isset($_GET[$e]) ) $GLOBALS[$e] = ($bQuote ? stripslashes($_GET[$e]): $_GET[$e]); } break; case 'get': if ( is_array( $value ) ) foreach ( $value as $e ) { if ( isset($_GET[$e]) ) { $GLOBALS[$e] = ($bQuote ? stripslashes($_GET[$e]): $_GET[$e]); } else { unset( $GLOBALS[$e] ); } } break; case 'post': if ( is_array( $value ) ) foreach ( $value as $e ) { if ( isset($_POST[$e]) ) { $GLOBALS[$e] = ($bQuote ? stripslashes($_POST[$e]) : $_POST[$e]); } else { unset( $GLOBALS[$e] ); } } break; case 'cookie': if ( is_array( $value ) ) foreach ( $value as $e ) { if ( isset($_COOKIE[$e]) ) { $GLOBALS[$e] = $_COOKIE[$e]; } else { unset( $GLOBALS[$e] ); } } break; } } } }

At the end ...

If you have any concerns, or query, do mail me. Oh! and if you like the article, do forward it to your friends :-)

Page Page » 1 2 3 4 5

Rate this article


  • 1
    Poor

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10
    Great
 

About

Amit Arora is web developer with expertise in developing eCommerce enabled websites for the businesses.

Contact | Resume

Subscribe to newsletter




Get Firefox

Monitored by Site24x7
Uptime