$_GET Breaks XML - php

I'm using the SimpleViewer flash image gallery on a site, and it uses an XML file for information about the images it displays.
For the site, I need to dynamically generate the XML, so I'm using a PHP file with a text/xml Content-type declared. However, for some reason when I access one of the GET variables in the $_GET array SimpleViewer tells me that there are no images in the gallery, even though when I view the source it looks the exact same and is well-formed.
Here's the code:
$photos = array(
"1" => array("house1_1.JPG")
);
foreach($photos[$_GET["hid"]] as $p){
echo '';
}
If I replace $_GET["hid"] with "1" then it works fine, but when I make the reference to $_GET it returns the error.
Is there some reason as to why accessing a GET variable would cause scripts linking to the XML (the SimpleViewer flash) to malfunction, and is there a way to get around this?
*Note: The "hid" GET variable is 100% sure set to "1", and there is no PHP error.
Also, the output looks exactly the same for when I use $_GET["hid"] versus "1", the only difference is the SimpleViewer script refuses to see that the images are there.
Also, the stuff in the empty quotes is some XML, but I don't know how to get it to appear in the tags...
Var dump of $photos and $_GET, respectively:
array(1) {
[1]=>
array(1) {
[0]=>
string(12) "house1_1.JPG"
}
}
array(1) {
["hid"]=>
string(1) "1"
}

I would first check and make sure $_GET["hid"] is returning "1". If it's possible that it is not returning "1" then it should throw an error accessing a bad index of $photos.

Is the $_GET hid variable set in your request? If not this will trigger a PHP warning.
var_dump($_GET['hid']); to see the value of the $_GET variable and ensure it as you expect.
Also please ensure that you have error reporting set to at least E_ALL and display errors is set to yes/true to make your debugging easier.

I think you're probably having an issue with the difference between "1" and 1. When you use a get with something like ?hid=1, it's not coming through as a string, that's being converted to a number, whereas your actual array is using the string "1" as the key.
Either change your key to 1 instead of "1" or cast the hid to string.

Issue was never resolved -- I ended up having to just move on and go for a longer and less elegant solution. Oh well.

Related

PHP / Json String(20) before value

I currently have some json data that I am retrieving via PHP, however, my result has String(20) in front of it;
** Answer - Var_dump causes this, just echo.
The string(20) is because you asked for a var_dump() of the item. var_dump() does more than echo the item, it describes it.
From the docs -
This function displays structured information about one or more expressions that includes its type and value. Arrays and objects are explored recursively with values indented to show structure.
Extra Answer - yes, try str_replace()

Is it legal for a cookie value to be an array?

I always assumed that cookies may only hold strings, but the way PHP handles cookies, it is also possible to store an array in a cookie (and I'm not talking about serialized array, but a native array). All you need to do is this:
setcookie('a[1]', 'a');
setcookie('a[2]', 'b');
var_dump($_COOKIE);
The above will produce the following (remember to execute it twice):
array(1) {
["a"]=>
array(2) {
[1]=>
string(1) "a"
[2]=>
string(1) "b"
}
}
What's going on here? Clearly we managed to store an array to a cookie, which is supposed to hold strings only. Is this a bug?
It is certainly not a bug. As a matter of fact it is documented in PHP Documentation
You may also set array cookies by using array notation in the cookie name. This has the effect of setting as many cookies as you have array elements, but when the cookie is received by your script, the values are all placed in an array with the cookie's name:
A cookie value can only be a string.
When PHP parses the cookies into $_COOKIE, certain naming conventions (i.e. cookies with names that end in [] or [something]) will cause it to represent them as an array.

Associative array with 2 names that are the same?

I'm working on a PHP script that updates some tracking numbers based on an uploaded CSV file. The import worked fine for some time, then the exports started having quotation marks around the values. I thought this would be fine, but it started rejecting the files. Doing some debugging and var_dumps, I discovered a very strange situation I have never seen before - An associative array with two indices with the same name. I ran the code setting the fields (shown below) and added a line:
$v['order_id'] = '119205';
After running that line, the var_dump was as follows:
array(15) {
["order_id"]=>
string(6) "119205"
["Tracking Number"]=>
string(22) "6735675476254654756"
["Postage"]=>
string(4) "1.64"
["order_id"]=>
string(6) "119205"
}
Some fields removed for brevity. As you can see, there are two ["order_id"] indices. How is this even possible?
Here is the code that sets the values of the array dumped above:
$v = array();
foreach ($map as $k => $n) {
$v[$n] = #$data[$k];
}
with $map being the CSV header row. Trying to reference $v['order_id'] without running the $v['order_id'] = '119205'; line resulted in this error:
Notice: Undefined index: order_id in /dir/to/php/file/php_file.php</b> on line 29
Manually setting the index worked as expected, pulling the rest of the data from $v without issue.
EDIT:
Dumping the array_keys resulted in:
[0]=>
string(11) "order_id"
and:
[14]=>
string(8) "order_id"
making the first one three characters longer.
var_export still resulted in identical indices.
How can I get rid of these invisible characters? I've already tried $v[trim($n)] = #$data[$k]; in the foreach().
Try var_dump(array_keys($v)). Find the key that looks like order_id and make sure the string's length is exactly 8. I suspect there may be a NUL character in there, which would give it a length of 9 and cause it to not respond to order_id.
Quote:
In the output of var_dump(), the null bytes are not visible.
Technically you can not have two times the same key in PHP in an array. It might be that var_dump is not giving the right keys here (e.g. probably some null-chars or other non-displayable chars are dropped).
Instead you might want to check, what's going on:
var_dump(array_keys($data));
Maybe it helps, the following is a related question which demonstrates when var_dump hides some information:
Array to Object and Object to Array in PHP - interesting behaviour

php variable-less argument?

just wondering. i've seen sites with this kind of url http://www.something.com/?somedata with the 'somedata' is the value of some 'unmentioned' variable
how can i do something like that? all I know is the traditional http://www.something.com/index.php?arg=somedata
thanks a lot
This is just a variable without value. You can get that string using list($value) = array_keys($_GET); - assuming you have ensured there is exactly one value in the $_GET array (count($_GET) === 1), otherwise you will get an error (if $count === 0) or unwanted behavior (if $count > 1).
You can usually access the full query string using $_SERVER["QUERY_STRING"].
I think every main-stream web server provides that variable, I'm 100% sure about Apache and pretty sure about IIS.
It is simply a shortcut for a boolean value.
somedata is equivalent to somedata=1 or somedata=true
When it's checked server side, the presence itself of the variable is enough to write a condition.
if ( isset($_GET['somedata']) ) {
//do something
}
What you see as "the value of some 'unmentioned' variable" is more generally considered as a query string parameter without a value. So for foo=bar&baz, foo has the value bar and baz has no value (in PHP its value will be an empty string).
Since the other answers are providing different methods of accessing that parameter name, here are my two cents. You can get the first key of the $_GET array by using the key function. If no key is available, key will return NULL.
Visiting ?somedata=somevalue
var_dump(key($_GET), $_GET);
/*
string(8) "somedata"
array(1) {
["somedata"]=>
string(9) "somevalue"
}
*/
Visiting ?somedata
var_dump(key($_GET), $_GET);
/*
string(8) "somedata"
array(1) {
["somedata"]=>
string(0) ""
}
*/
Visiting ?
var_dump(key($_GET), $_GET);
/*
NULL
array(0) {
}
*/
They probably use some kind of URL rewriting (see Apache mod_rewrite) where the url is transformed from http://www.something.com/?somedata to http://www.something.com/index.php?arg=somedata
For your given URL you can just iterate over the $_GET array:
foreach ($_GET as $key => $value) {
if ($key == 'somedata') {
//do something
}
}
You'll find the parameter in the keys of $_GET.
You also could extract the keys from $_GET with array_keys($_GET).
Most web servers, since the beginning of the web, allow to define a default file name to be delivered for directories. So if you load http://example.com/ it serves such file (typically index.html). In PHP enabled systems, you normally use index.php for such purposes, although the exact name can be changed. In Apache:
http://httpd.apache.org/docs/2.2/en/mod/mod_dir.html#directoryindex
As about ?somedata, it's just a variable without a value (or, more exactly, its value is an empty string). You can use it if you only need to know whether the variable is set or not:
<?php
if( isset($_GET['somedata']) ){
// Do some stuff
}
?>

Facebook Stream.Get -- How to access data in array?

On stream.get, I try to
echo $feeds["posts"][$i]["attachment"]["href"];
It return the URL, but, in the same array scope where "type" is located (which returns string: video, etc), trying $feeds["posts"][$i]["attachment"]["type"] returns nothing at all!
Here's an array through PHP's var_dump: http://pastie.org/930475
So, from testing I suppose this is protected by Facebook? Does that makes sense at all?
Here it's full: http://pastie.org/930490, but not all attachment/media/types has values.
It's also strange, because I can't access through [attachment][media][href] or [attachment][media][type], and if I try [attachment][media][0][type] or href, it gives me a string offset error.
["attachment"]=> array(8) {
["media"]=> array(1) {
[0]=> array(5) {
["href"]=> string(55) "http://www.facebook.com/video/video.php?v=1392999461587"
["alt"]=> string(13) "IN THE STUDIO"
["type"]=> string(5) "video"
My question is, is this protected by Facebook? Or we can actually access this array position?
Well, once the data is returned to you, it can no longer be protected by Facebook. You have full access to everything in that result as a regular data structure.
From the looks of it, there are multiple href properties throughout, so you'll want to be careful which one you're going for. $feeds["posts"][$i]["attachment"]["href"] is a valid element for some items, but $feeds["posts"][$i]["attachment"]["media"][0]["href"] is also a valid element.
There doesn't appear to be a $feeds["posts"][$i]["attachment"]["type"] element though, so that's why you're getting nothing for that particular item. There is a type inside ["attachment"]["media"][0] however, which is probably what you want.
If you are getting a string offset error when using array syntax, you've probably mixed up an element somewhere. Strings can be accessed via array syntax. For example:
$str = "string";
echo $str[1]; //echos 't'
You would get an offset warning if you tried to access an index that was larger than the string. In any case, from the looks of that output, $feeds["posts"][$i]["attachment"]["media"][0]["type"] should work.

Categories