Jump to content

foreach on $_POST or $_GET ?


magicmoose

Recommended Posts

Hi,

I'm trying to filter all input on my site but I'm having some trouble with the syntax.

 

I tried to use this..

 

foreach($_POST as $key => $val) {
$_POST[$key] = htmlentities($_POST[$key]);
}

foreach($_GET as $key => $val) {
$_GET[$key] = htmlentities($_GET[$key]);
}

 

But get the error

Warning: htmlentities() expects parameter 1 to be string, array given

 

 

Can anyone give me any advice on how to do this?

 

Thanks

 

Link to comment
https://forums.phpfreaks.com/topic/164372-foreach-on-_post-or-_get/
Share on other sites

This is untested, but it is how I would probably tackle the problem.  I created a recursive htmlentities function that will accept arrays of strings. 

 

<?php

$globals = array($_POST, $_GET);

foreach ($globals as &$global)
{
foreach ($global as $key => $val)
{
	$global[$key] = rhtmlentities($val);
}
}

function rhtmlentities($mixed)
{
if (is_array($mixed))
{
	foreach ($mixed as $key => $value)
	{
		$mixed[$key] = rhtmlentities($value);
	}
}
else if (is_string($mixed))
{
	return htmlentities($mixed);
}
} 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.