开发者

$_SERVER not returning query string

开发者 https://www.devze.com 2023-04-08 21:03 出处:网络
I\'m simply trying to store the current page a user is viewing in a DB. When 开发者_JS百科the page loads, I insert $_SERVER[\'REQUEST_URI\'] . $_SERVER[\'QUERY_STRING\'] into my DB, but only the page

I'm simply trying to store the current page a user is viewing in a DB. When 开发者_JS百科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?


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


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


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消