Jump to content

Recommended Posts

Hi

 

I am making a validation function which will strip any dangerus code from an array or string. The problem is when i create a 2D array it screws up

 

 

    function cleanInput($Data) 
{
	$Data = mysql_real_escape_string(stripslashes($Data));
	$search = array(
		'@<script[^>]*?>.*?</script>@si',   // Strip out javascript
		'@<[\/\!]*?[^<>]*?>@si',            // Strip out HTML tags
		'@<style[^>]*?>.*?</style>@siU',    // Strip style tags properly
		'@<![\s\S]*?--[ \t\n\r]*>@');         // Strip multi-line comments
	return preg_replace($search, '', $Data);
}

function libStripInputSlashes($Data)
{
    if (is_array($Data)) 
	{
        foreach($Data as $var=>$val) 
	{
            $output[$var] = cleanInput($val);
        }
    }
    else 
	{
       return cleanInput($Data);
    }
    return $output;
}

$Data = libStripInputSlashes(array(array('<HTML> \' REALINPUT', '<HTML> \' REALINPUT', '<HTML> \' REALINPUT', '<HTML> \' REALINPUT', '<HTML> \' REALINPUT','<HTML> \' REALINPUT'),array('<HTML> \' REALINPUT', '<HTML> \' REALINPUT', '<HTML> \' REALINPUT', '<HTML> \' REALINPUT', '<HTML> \' REALINPUT','<HTML> \' REALINPUT')));
Echo $Data; 

 

 

Any ideas how I could fix this?

 

Thanks

Kmaid

Link to comment
https://forums.phpfreaks.com/topic/143452-solved-validating-2d-and-3d-arrays/
Share on other sites

try this

<?php
function libStripInputSlashes($Data)
{
    if (is_array($Data)) 
    {
        foreach($Data as $var=>$val) 
{
	$output[$var] = libStripInputSlashes($val);
        }
    }else{
       return cleanInput($Data);
    }
    return $output;
}
?>

This will handle deep array structures without using recursion...

 

 

function libStripInputSlashes ( $global )
{
while ( list ( $n, $v ) = each ( $global ) )
{
	foreach ( $v AS $name => $value )
	{
		if ( is_array ( $value ) )
		{
			$global[] =& $global[$n][$name];
		}
		else
		{
			$global[$n][$name] = cleanInput ( $value );
		}
	}
}

return $global;
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.