Solved

$_POST values 'disappear on 2nd read ?

Posted on 2013-05-29
5
375 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 83

Expert Comment

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

896 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

14 Experts available now in Live!

Get 1:1 Help Now