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
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);
}
} 

Link to comment
Share on other sites

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.