/    Sign up×
Community /Pin to ProfileBookmark

Some useful simple code for you

Hi,

Following allows to use $_GET, $_POST, simply $_REQUEST elements in more simpler way.

Produces separate variable for each element of those.

So, I think it has many uses.

[code=php]while (list($key,$val)=each($_REQUEST)) { $$key = addslashes(strip_tags($val)); }[/code]

Say passed [url]http://www.mysite.com/index.php?ids=1&ide=30&page=1[/url]

if index.php have above code snippet.

URL parameters ids, ide, page becomes single variables holding respective values.

Common for Form Submission, normal $_GET url params , obviously also with AJAX.

Thought it would be useful to somebody.

Thanks and Best Regards

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogFeb 05.2009 — Or just use [url=http://www.php.net/import_request_variables]import_request_variables[/url](). ?
Copy linkTweet thisAlerts:
@GUIRauthorFeb 05.2009 — ? I wasnt aware of such function. May be it have same code inside ?
Copy linkTweet thisAlerts:
@SodbusterFeb 05.2009 — More or less the same. ?[CODE]/* {{{ proto bool import_request_variables(string types [, string prefix])
Import GET/POST/Cookie variables into the global scope */
PHP_FUNCTION(import_request_variables)
{
zval **z_types, **z_prefix;
char *types, *prefix;
uint prefix_len;
char *p;

switch (ZEND_NUM_ARGS()) {

case 1:
if (zend_get_parameters_ex(1, &z_types) == FAILURE) {
RETURN_FALSE;
}
prefix = "";
prefix_len = 0;
break;

case 2:
if (zend_get_parameters_ex(2, &z_types, &z_prefix) == FAILURE) {
RETURN_FALSE;
}
convert_to_string_ex(z_prefix);
prefix = Z_STRVAL_PP(z_prefix);
prefix_len = Z_STRLEN_PP(z_prefix);
break;

default:
ZEND_WRONG_PARAM_COUNT();
}

if (prefix_len == 0) {
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "No prefix specified - possible security hazard");
}

convert_to_string_ex(z_types);
types = Z_STRVAL_PP(z_types);

for (p = types; p && *p; p++) {
switch (*p) {

case 'g':
case 'G':
zend_hash_apply_with_arguments(Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_GET]), (apply_func_args_t) copy_request_variable, 2, prefix, prefix_len);
break;

case 'p':
case 'P':
zend_hash_apply_with_arguments(Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_POST]), (apply_func_args_t) copy_request_variable, 2, prefix, prefix_len);
zend_hash_apply_with_arguments(Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_FILES]), (apply_func_args_t) copy_request_variable, 2, prefix, prefix_len);
break;

case 'c':
case 'C':
zend_hash_apply_with_arguments(Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_COOKIE]), (apply_func_args_t) copy_request_variable, 2, prefix, prefix_len);
break;
}
}
}
/* }}} */[/CODE]
×

Success!

Help @GUIR spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...