3

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!

3
  • 1
    What do you want the contents of $gets to look like? Dec 23, 2011 at 21:23
  • @jondavidjohn well, I'd like it exactly as it was sent. So if sent id=3&auth=1 I want it to show just that
    – JD Audi
    Dec 23, 2011 at 21:23
  • 1
    Take a look in $_SERVER, one of those values may have what you want. Do var_dump($_SERVER) to see what's in there. I think it's $_SERVER['QUERY_STRING'] or something.
    – gen_Eric
    Dec 23, 2011 at 21:25

6 Answers 6

7

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');
1
  • 2
    For logging purpose, it might be better to get unparsed data to catch any illegal queries. Dec 23, 2011 at 21:33
6

If you want both $_GETand $_POST as query strings, you can do this:

$gets = http_build_query($_GET);
$posts = http_build_query($_POST);
0
2

$_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.

5
  • @Michael does this grab POSTs too?
    – JD Audi
    Dec 23, 2011 at 21:25
  • @JDAudi: POST variables are not part of the query string, so, no.
    – gen_Eric
    Dec 23, 2011 at 21:26
  • I would like to grab POST variables as well!
    – JD Audi
    Dec 23, 2011 at 21:27
  • there is a separate variable $HTTP_RAW_POST_DATA. Also, you might be interested in $_SERVER['REQUEST_METHOD'] which will tell you what method was used. Dec 23, 2011 at 21:29
  • $HTTP_RAW_POST_DATA is not reliable.
    – Amadan
    Dec 23, 2011 at 21:38
2

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");
1
  • I don't think http_get_request_body is a standard PHP function. The docs page says it's in pecl_http.
    – gen_Eric
    Dec 23, 2011 at 21:29
1

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
    }
1

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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.