Variable in $_POST returns as string instead of array - php

In my form i have fields with name photoid[] so that when sent they will automatically be in an array when php accesses them.
The script has been working fine for quite some time until a couple days ago. And as far as i can remember i havent changed any php settings in the ini file and havent changed the script at all.
when i try to retrieve the array using $_POST['photoid'] it returns a string with the contents 'ARRAY', but if i access it using $_REQUEST['photoid'] it returns it correctly as an array. Is there some php setting that would make this occur? As i said i dont remember changing any php settings lately to cause this but i might be mistaken, or is there something else i am missing.

I had the same problem. When I should recieve array via $_POST, but var_dump sad: 'string(5) "Array"'. I found this happens, when you try use trim() on that array!
Double check your code, be sure you're not doing anything else with $_POST!

Raise your error_reporting level to find any potential source. It's most likely that you are just using it wrong in your code. But it's also possible that your $_POST array was mangled, but $_REQUEST left untouched.
// for example an escaping feature like this might bork it
$_POST = array_map("htmlentities", $_POST);
// your case looks like "strtoupper" even
To determine if your $_POST array really just contains a string where you expected an array, execute following at the beginning of your script:
var_dump($_POST);
And following for a comparison:
var_dump(array_diff($_REQUEST, $_POST));
Then verifiy that you are really using foreach on both arrays:
foreach ($_POST["photoid"] as $id) { print $id; }

If you use an array in a string context then you'll get "Array". Use it in an array context instead.
$arr = Array('foo', 42);
echo $arr."\n";
echo $arr[0]."\n";
​
Array
foo

$_POST['photoid'] is still an array. Just assign it to a variable, and then treat it like an array. ie: $array = $_POST['photoid'];
echo $array[0];

Related

PHP $_POST array print_r

I have a general question. I wrote the below code to add an array of arrays to the $_POST variable. I know the post variable is an array too. So, after adding my multidimensional array to the post variable one by one, I tried to print_r the data to view it but, only one array would print out.
why is it that only one array would print out?
$x = 0;
for($x; $x < count($return_auth); $x++){
$_POST = $return_auth[$x];
}
print_r($_POST);
Although nerdlyist answer is absolutely right, but I think it is not the right way, in short, you are modifying $_POST variable and since $_POST variable has a meaning attached to it that it contains all the parameters sent in that POST request. If you overwrite it that change will be affected throughout your application and other modules running in your application will see an additional post parameter which is not actually sent in that request in $_POST array which is not right IMO.
What you are doing is overwriting post. Based on what you have here you could do something like this:
$_POST[return_auth] = $return_auth;
Unless you have a reason to loop an array to create an array...
use this in your for loop
$_POST[] = $return_auth[$x];
Edit
this will work better
$_POST['something' . $x] = $return_auth[$x];
now you can access to (foo) for example
$_POST['somethingfoo'];

json_encode Returning a PHP Array

So I am working with PHP to pass a PHP array over a jQuery Ajax request to another PHP page. This is quite the task. For some reason, my json_encode is returning an array instead of a string, I am not quite sure why. Here is my PHP code:
$new_spreadsheet = nl2br($_POST['spreadsheet']);
$new_spreadsheet = explode('<br />', $new_spreadsheet);
array_shift($new_spreadsheet);
$new_spreadsheet = array_values($new_spreadsheet);
echo json_encode($new_spreadsheet);
I would show you the output, but it is really long. Basically this is outputting a PHP array which consists of each row on the spreadsheet. This is what I want to have, but the problem is that I don't want the quotes and everything in the array. I am pretty sure I need to run json_decode, but when I do that my code returns an error saying that the parameter needs to be a string. I can tell something is not right, but am not quite sure what I need to change. I would appreciate any advice.
Update: At this point, when I try to loop through the array and print each value, the first array index is equal to a double quote like so: ". There are double quotes in random values throughout the area. I am not quite sure about what is causing this.
If I echo the rows from within the json_encoded PHP array onto the console, I get the following output:
"
correct value
correct value
correct value
"
You're using JSON, which means you have to adhere to somewhat more stringent syntax rules than Javascript's. e.g.
<?php
$arr = array('This' => 'is', 'an' => 'array in php');
echo json_encode($array);
?>
output:
{"This":"is","an":"array in PHP"}
There is NO way to avoid getting quotes on the values, as they're a fundamental requirement of JSON (and Javascript). If you don't want quotes, then don't use JSON.
try only br.
$new_spreadsheet = explode("<br>", $new_spreadsheet);
It will work. and json_enode can never return an array. try var_dump and check.Also make sure before you post, use htmlspecialcharacters.

print_r Return Value Incorrect

I am having a little trouble with the print_r function. Undoubtedly something I am misunderstanding in its operation... Basically, I have an array of objects in a class like so:
public $fields = array();
Assigned like so:
$oField = new Field();
/* property assignments to $oField omitted for brevity */
$this->fields[$i] = $oField;
Now in the primary class, I am attempting to capture debug information:
$this->debuginfo = print_r($this->fields, true);
When outputting the value of $this->debuginfo, it simply says "Array" - basically not exploding the array. If I do a regular print_r($this->fields);, it gives the expected results.
This is my first time attempting to use print_r with it returning results versus outputting to the screen so I am sure I am just missing something, but in reading the php documentation, this is how it would seem to be implemented. What am I missing?
Thanks for any assistance!
Update:
print_r($var, true) does indeed return the "exploded" variable properly as I had it written. Thanks to dev-null for their comment which gave me some food for thought that lead me to my problem.
Try var_export() instead. var_export() gets structured information about the given variable.
example:
$this->debuginfo = var_export($this->fields, true);
Reference: http://php.net/manual/en/function.var-export.php

Cookie Value not available, why?

I have tested this on my development computer, but now I have uploaded everything to the production server and I cant read out the value of the cookie.
I think the problem lies in the Serialization and Unserialization.
if (isset($_COOKIE['watched_ads'])){
$expir = time()+1728000; //20 days
$ad_arr = unserialize($_COOKIE['watched_ads']); // HERE IS THE PROBLEM
$arr_elem = count($ad_arr);
if (in_array($ad_id, $ad_arr) == FALSE){
if ($arr_elem>10){
array_shift($ad_arr);
}
$ad_arr[]=$ad_id;
setcookie('watched_ads', serialize($ad_arr), $expir, '/');
}
}
When I echo this: count($ad_arr) I receive the expected nr, 1 in this case, so there is a value there. But when I echo the value: echo $ad_arr[0]; I get nothing. Completely blank. No text at all.
Anybody have a clue?
if you need more info about something let me know...
Turns out it was stripslashes which was needed here.
Did a stripslashes() first and it worked unserializing the cookie.
You should understand that count returns 1 for most non-array values, including an empty string.
> php
<?php
echo count("");
^Z
1
So, to debug it, try var_dump'ing the $_COOKIE superglobal itself.
I would guess, that your $ad_arr is no array. You can check this with the "is_array()" function or by calling:
var_dump($ad_arr);
It sould have "array" in the output and not "string" (as Artefacto just already mentioned).
Another little tip:
If you want to store a associative array, you can use an encoded JSON String which use can save using the json_encode() gunction:
setcookie('watched_ads', json_encode($ad_arr), $expir, '/');
And loading the data you can use the opposite function json_decode():
$ad_arr = json_decode($_COOKIE['watched_ads'], true);
Adding true as a second paramter, you will get a associative array and not an object. Using the JSON format is also a good idea saving complex data within a database.
A and a last tip: "!in_array()" works just as fine as "in_array() == FALSE"

outputting all array values to file with php

Im working with a foreign API and im trying to get a fix on what all its sending to my file with the GET method.
How can i output something like print_r($_GET) to a file so i can read what all its sending?
If you have a hash, both of the listen solutions won't give you the keys. So here's a way to get your output formatted by print_r:
$var = print_r($your_array, 1);
file_put_contents("your_file.txt",$var);
The second option of print_r is boolean, and when set to true, captures the output.
It sounds like you need a log to store the $_GET variables being submitted to your script, for debug purposes. I'd do something like this appending values to the end of the file, so the file is not overwritten every request:
file_put_contents('path_to_log.txt', print_r($_GET, true), FILE_APPEND);
Writing to a file:
You could use file_put_contents() to create the file with your output. This function will accept a string, or an array, which releases you of the burden to convert your array into a writable-format.
file_put_contents("myget.txt", $_GET);
As stated in the comments, this option isn't ideal for multidimensional arrays. However, the next solution is.
Maintaining format:
If you'd like to maintain the print_r() formatting, simply set the second parameter to true to return the value into a variable rather than outputting it immediately:
$output = print_r($_GET, true);
file_put_contents("myget.txt", $output);

Categories