0

I have a php script (listening.php) listening for any in-bound GET requests.

For now, I only listen for specific GET variables. All other variables are ignored.

I'm placing a simple pixel-type script on an outside url:

<?php
 $url = $_SERVER['SCRIPT_URI'];
?>
<script src="http://mydomain.com/listening.php?page=1&url=<?php echo $url;?>"></script> 

This works fine and dandy. I'd like to pass more info, but I'd like to pass all $_SERVER variables in the array.

I have a few options, like looping through the array, and building the url string with the key/values.

But, I'm more interested in passing the entire array as a whole, and storing it in mysql, inside a Text column.

I know there's probably a limit of how large a single string can be passed via a url.

Does anyone have a good way or idea on a best practice for doing this? Personally I expect passing an array via the url is not a good idea, but I'm fishing for ideas.

coffeemonitor
  • 12,780
  • 34
  • 99
  • 149

1 Answers1

0

You could encode and serialize the array:

<?php
$url = $_SERVER['SCRIPT_URI'];
$srv = base64_encode(serialize($_SERVER));
?>
<script src="http://mydomain.com/listening.php?page=1&url=<?php echo $url;?>&srv=<?php echo $srv; ?>"></script>

Then in the listening script, you do the reverse:

<?php
$srv = unserialize(base64_decode($_GET['srv']));
?>

However, keep in mind that these requests have a limitation on GET length.

With that said, you could use gzcompress and gzuncompress:

$srv = base64_encode(gzcompress(serialize($_SERVER)));

Uncompress it:

<?php
$srv = unserialize(base64_decode(gzuncompress($_GET['srv'])));
?>
Rob W
  • 9,134
  • 1
  • 30
  • 50
  • I did a test and it looks like `gzdeflate` and `gzinflate` may be viable options as well. – Rob W Jul 03 '13 at 16:47
  • I like your optimized form of encoding and serializing the array. I hadn't thought of this, and seems the most viable route. – coffeemonitor Jul 03 '13 at 16:48
  • Based off of some research, and http://stackoverflow.com/questions/2659952/maximum-length-of-http-get-request , it seems you *might* be OK to use this. You may want to select what you want from `$_SERVER` just to shorten the length of the string.. However, in my testing, $srv ended up only being around 1200 characters including the compression. – Rob W Jul 03 '13 at 16:52