processing GET and POST variables

This is a discussion on processing GET and POST variables within the PHP Language forums, part of the PHP Programming Forums category; Below is a function adapted from one found in phpMyEdit.class.php http://platon.sk/projects/release_li...p?project_id=5 ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 12-31-2004
DH
 
Posts: n/a
Default processing GET and POST variables

Below is a function adapted from one found in phpMyEdit.class.php
http://platon.sk/projects/release_li...p?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');

Reply With Quote
  #2 (permalink)  
Old 12-31-2004
Janwillem Borleffs
 
Posts: n/a
Default Re: processing GET and POST variables

DH wrote:
> 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.
>


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

<?php

function get_cgi_var($name, $default_value = null) {
if ($var = @$_REQUEST[$name]) {
if (get_magic_quotes_gpc()) {
if (!is_array($var)) {
$var = stripslashes($var);
} else {
foreach (array_keys($var) as $key) {
$var[$key] = stripslashes($var);
}
}
}
} else {
$var = $default_value;
}
return $var;
}

?>


JW


Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 10:01 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0