Solved

$_POST values 'disappear on 2nd read ?

Posted on 2013-05-29
5
370 Views
Last Modified: 2013-05-30
Hi,

I having problems understanding what is going on in my function copied below. This function is part of a wordpress functions.php file.

What appears to be happening is that if I read the $_POST values once in my function they contain the values I expect ('valuefield1' and 'valuefield2') but if I read them a second time, straight afterwards in the same function these values disappear - but the count of the $_POST['item_meta'] contains 2 both times !

function mycustomvalidation($errors, $field, $value)
{
	addlog('Check 1 $_POST Values');
	$count = count($_POST['item_meta']);
	while (list($key,$val)=each($_POST['item_meta']))
	{
	   addlog($key . '=>' . $val);
	}
	addlog('$_POST Contains ' . $count . ' Values');

	addlog('Check 2 $_POST Values');
	$count = count($_POST['item_meta']);
	while (list($key,$val)=each($_POST['item_meta']))
	{
	   addlog($key . '=>' . $val);
	}
                 addlog('$_POST Contains ' . $count . ' Values');

    return $errors;
}

Open in new window


The log file (written to by the 'addlog' function seen in the code above) shows

Check 1 $_POST Values
84=>valuefield1
91=>valuefield2
$_POST Contains 2 Values
Check 2 $_POST Values
$_POST Contains 2 Values

Open in new window


(So between lines 5 and 6 in the log I'm expecting to see lines 84=>valuefield1 and 91=>valuefield2 again)

Really strange - be grateful for assistance in resolving this.

FYI
Running UniServer web package (latest version) on Windows 7 PC.
0
Comment
Question by:rcscs
  • 2
  • 2
5 Comments
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 39206224
Maybe 'addlog' doesn't allow duplicate values.
0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 39206231
To double check, you can put var_dump($_POST['item_meta']); right after addlog('$_POST Contains ' . $count . ' Values'); both times.  I think you will see the same thing twice when you do that.
addlog('$_POST Contains ' . $count . ' Values');
var_dump($_POST['item_meta']);

Open in new window

0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39206241
Just on the outside chance that I am wrong about this, please ask a moderator to add this to the WordPress Zone.

The PHP each() function returns the current key and value pair from an array and advances the array cursor.  So my recommendation is to use reset($_POST) after line 10 and before line 11.
0
 

Author Comment

by:rcscs
ID: 39206880
Thanks for the replies.

Ray - you got it.

I initially added the while each loop to check on the contents of the S_POST variable - but in doing that I wasn't aware of the behaviour which you describe with the array cursor.

Adding

reset($_POST(['item_meta'])

Did the trick.

Many thanks (been looking at this for hours...)
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39207223
Thanks for the points.  Another way of processing arrays is to use foreach(), but with external data, I prefer to know the names of the keys beforehand and only process the keys that are known to my script.  It's a security thing that helps to avoid attack data.  Not sure if you can do that with WordPress.

Best regards, ~Ray
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now