TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Script Giveaway (http://www.talkphp.com/script-giveaway/)
-   -   automatically clean form posted variables. (http://www.talkphp.com/script-giveaway/3020-automatically-clean-form-posted-variables.html)

drewbee 06-27-2008 08:34 PM

automatically clean form posted variables.
 
I use this as a simple cleanup of posted variables (I only do post automatically), as well as a nice trim to the data. This is nice because even if an array is given, it will follow it...

This is a basic piece of my Form class. This form class is simply the superclass to all of my sub-classes. There is more stuff I use within this as well, but you get the idea with this I think.

only strip_slashes() if you have the lovely magic quotes on.

This does pass by ref as well, so you can throw anything you want into it and it modifies the original variable without returning anything.

PHP Code:

<?php
class Form extends Page
{
 
    var 
$isPostBack;
 
    function 
Form()
    {
          
$this->isPostBack count($_POST) > true false;
          if (
$this->isPostBack)
          {
              
array_walk_recursive($_POST, array($this,'cleanGlobal'));
          }
    }
 
    function 
cleanGlobal(&$var$key null)
    {
        if(
is_array($var))
        { 
            
array_walk_recursive($var, array($this'cleanGlobal'));
        } 
        else 
        {
            
// How do you want each variable handled?
            
$var trim(stripslashes(($var)); 
        }
    }
 
}
?>


drewbee 06-28-2008 03:24 PM

Procedural:

PHP Code:

function cleanGlobal(&$var$key null)
{
    if(
is_array($var))
    { 
        
array_walk_recursive($var'cleanGlobal');
    } 
    else 
    {
        
// How do you want each variable handled?
        
$var trim(stripslashes(($var)); 
    }
}
 
if (
count($_POST) > 0)
{
    
array_walk_recursive($_POST'cleanGlobal');




All times are GMT. The time now is 02:41 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0