hannebaumsaway
hannebaumsaway

Reputation: 2734

$_SERVER not returning query string

I'm simply trying to store the current page a user is viewing in a DB. When the page loads, I insert $_SERVER['REQUEST_URI'] . $_SERVER['QUERY_STRING'] into my DB, but only the page (e.g. index.php?) is showing up, without the query string (I have verified that there IS a query string in the URL).

I tried $_SERVER['PHP_SELF'] with the same results.

EDIT TO ADD: Here is the dump of $_SERVER:

Array
(
    . . .
    [REQUEST_METHOD] => GET
    [QUERY_STRING] => view=scores&yr=2010&wk=1
    [REQUEST_URI] => /index.php?view=scores&yr=2010&wk=1
    . . .
)

So the query string is present in the array, even as part of REQUEST_URI. So my query...

mysql_query("insert into clickstream
               (user_id, page)
             values
               (" . $_SESSION['user_id'] . ", '" . mysql_real_escape_string($_SERVER['REQUEST_URI']) . mysql_real_escape_string($_SERVER['QUERY_STRING']) . "');")
  or die('mysql error: ' . mysql_error());

...should actually insert the query string twice, instead of no times!

Thoughts?

ADDED THOUGHT: Is it possible the MySQL DB strips everything from the input beyond the ?? The field is varchar.

UPDATE W/ PARTIAL SOLUTION: Changing the SQL input to just $_SERVER['QUERY_STRING'] (without REQUEST_URI) successfully input the query string. Thus, it leads me to believe that either PHP or MySQL was stripping everything from the input string after the ?. So the input params were correct; the result just got truncated.

Does anyone know why this might be the case?

Upvotes: 4

Views: 8724

Answers (5)

StomperHK
StomperHK

Reputation: 1

For me, $_SERVER['QUERY_STRING'] was also returning an empty string. What I did was get the full URL from the server with $_SERVER['HTTP_REFERER'] and then get the query string with parse_url($_SERVER['HTTP_REFERER'], PHP_URL_QUERY).

$queryString = parse_url($_SERVER['HTTP_REFERER'], PHP_URL_QUERY);

Upvotes: 0

hannebaumsaway
hannebaumsaway

Reputation: 2734

Thanks for the feedback, particularly @nachito. The problem has been isolated to MySQL, not PHP. The output from PHP is correct, but MySQL is stripping everything from the URL after ? upon insertion into the database.

Upvotes: 1

Santiago Lizardo
Santiago Lizardo

Reputation: 72

Can you show us the definition of the table clickstream ?

If the page column were only 10 chars long, then we have spotted the problem :)

'index.php?' (10 chars)

To see the table structure you can issue this MySQL command:

SHOW CREATE TABLE clickstream;

Upvotes: 0

nachito
nachito

Reputation: 7035

Couldn't you just use $_SERVER['REQUEST_URI'] ? It has the query string as part of the output...

Upvotes: 1

Xeoncross
Xeoncross

Reputation: 57184

Different servers pass different $_SERVER globals to the page. I assume you are using Apache rather than NGINX where you might have to check that QUERY_STRING is defined in FASTCGI_PARAMS.

The solution is to to do like @hakre says and just see which $_SERVER key has what you want.

<?php
print '<pre>';
print_r($_SERVER);
print '</pre>';

Upvotes: 2

Related Questions