$_SERVER 不返回查询字符串

发布于 2024-12-07 01:36:57 字数 1189 浏览 1 评论 0原文

我只是想将用户正在查看的当前页面存储在数据库中。页面加载时,我插入 $_SERVER['REQUEST_URI'] 。 $_SERVER['QUERY_STRING'] 进入我的数据库,但只显示页面(例如 index.php?),没有查询字符串(我已经验证有一个查询URL 中的字符串)。

我尝试了 $_SERVER['PHP_SELF'] 得到了相同的结果。

编辑添加: 这是 $_SERVER 的转储:

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

因此查询字符串存在于数组中,甚至作为 REQUEST_URI 的一部分。所以我的查询...

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

...实际上应该插入查询字符串两次,而不是一次!

想法?

补充想法:MySQL DB 是否有可能从输入中删除 之外的所有内容??该字段是 varchar。

更新部分解决方案:将 SQL 输入更改为 $_SERVER['QUERY_STRING'](不含 REQUEST_URI)成功输入查询字符串。因此,它让我相信 PHP 或 MySQL 正在从输入字符串中删除 ? 之后的所有内容。所以输入的参数是正确的;结果被截断了。

有谁知道为什么会出现这种情况?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

錯遇了你 2024-12-14 01:36:57

不同的服务器将不同的 $_SERVER 全局变量传递给页面。我假设您使用的是 Apache 而不是 NGINX,您可能需要检查 FASTCGI_PARAMS 中是否定义了 QUERY_STRING

解决方案是像 @hakre 所说的那样,然后查看哪个 $_SERVER 键有您想要的内容。

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

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>';
山人契 2024-12-14 01:36:57

您不能只使用 $_SERVER['REQUEST_URI'] 吗?它具有查询字符串作为输出的一部分......

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

苏大泽ㄣ 2024-12-14 01:36:57

感谢您的反馈,特别是@nachito。该问题已被隔离到 MySQL,而不是 PHP。 PHP 的输出是正确的,但 MySQL 在插入数据库时​​会删除 ? 之后 URL 中的所有内容。

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.

太傻旳人生 2024-12-14 01:36:57

您能给我们展示一下clickstream表的定义吗?

如果 page 列只有 10 个字符长,那么我们就发现了问题:)

'index.php?' (10 个字符)

要查看表结构,您可以发出以下 MySQL 命令:

SHOW CREATE TABLE clickstream;

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;
一抹淡然 2024-12-14 01:36:57

对我来说, $_SERVER['QUERY_STRING'] 也返回一个空字符串。我所做的是使用 $_SERVER['HTTP_REFERER'] 从服务器获取完整的 URL,然后使用 parse_url($_SERVER['HTTP_REFERER'], PHP_URL_QUERY) 获取查询字符串代码>.

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文