$ _SERVER does not return a query string

I am just trying to save the current page that the user is viewing in the database. When the page loads, I insert $_SERVER['REQUEST_URI'] . $_SERVER['QUERY_STRING'] $_SERVER['REQUEST_URI'] . $_SERVER['QUERY_STRING'] to your database, but only the page appears (for example, index.php? ), Without a query string (I checked that there is a query string in the URL).

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

EDIT ADD: Here is the $ _SERVER dump:

 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 request ...

 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 insert a query string twice, not time!

Thoughts?

ADDED THOUGHT: Is it possible that MySQL DB removes everything from input outside ? ? The field is varchar.

UPDATE W / PARTIAL SOLUTION: Changing SQL input only to $_SERVER['QUERY_STRING'] (without REQUEST_URI) successfully enters the query string. So this makes me believe that either PHP or MySQL extracted everything from the input line after ? . Thus, the input parameters were correct; the result just sat down.

Does anyone know why this could be so?

+4
source share
4 answers

Thanks for the feedback, especially @nachito. The problem was isolated from MySQL, not from PHP. The PHP result is correct, but ? MySQL drop everything from the URL after ? when pasted into a database.

+1
source

Different servers send different $_SERVER pages to the page. I assume that you are using Apache, not NGINX, where you might need to verify that QUERY_STRING is defined in FASTCGI_PARAMS.

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

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

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

+1
source

Can you show us the definition of the clickstream table?

If the page column was only 10 characters long, we noticed a problem :)

'index.php? (10 characters)

To see the structure of the table, you can send this MySQL command:

 SHOW CREATE TABLE clickstream; 
0
source

All Articles