I am trying to store any and all variables sent to the site in a single variable (just for logging purposes.)
So if a user goes to www.mysite.com and put on a ?id=4&auth=230984721839
I want to grab both of those GETs and store them in a variable such as $gets
I was trying:
$gets = print_r($_GET);
$posts = print_r($_POST);
But it did not work. Is this even possible? I don't want the user know I am capturing these.
I would also like to grab POSTs too!
If you want both $_GET
and $_POST
as query strings, you can do this:
$gets = http_build_query($_GET);
$posts = http_build_query($_POST);
$_SERVER['QUERY_STRING']
. See this.
This will give you unparsed query, so you can catch any invalid URLs which might be useful for logging purpose.
First of all, you might appreciate that all the parameters, both GET and POST ones, are collected in $_REQUEST
.
Second, don't use print_r
. That will print the variable out, and not give you any sensible result. Instead, just say
$params = $_REQUEST;
If you want the raw data, then you can get it as in @MichałŠrajer's answer (GET) and comment (POST). So,
$get = $_SERVER['QUERY_STRING'];
$post = file_get_contents("php://input");
You can add a flag to print_r to tell it to return the result instead of outputting it.
$gets = print_r($_GET, true);
$posts = print_r($_POST, true);
However, you may want to look into serializing the associative array instead.
Update
Based on your comments on your question I suppose you really want:
$gets = $_SERVER['QUERY_STRING'];
$posts = file_get_contents('php://input');
By this code you can get any POST or GET values and then -maybe- put them into array for later use
foreach ($_REQUEST as $key => $value ) {
##Do what you want here - may be push values into array here
}
For storing purpose there is another possibility, and it is to serialize
the $_GET array.
$data = serialize($_GET);
And then you store $data
in your database.
For example, if your get string is ?foo=bar
you get this string a:1:{s:3:"foo";s:3:"bar";}
You can ever deserialize it with unserialize($data);