开发者

PHP - How do I get a parameter value from $_SERVER['HTTP_REFERER']?

开发者 https://www.devze.com 2023-01-28 14:47 出处:网络
In a PHP application, $_SERVER[\'HTTP_REFERER\'] has the following value: http://开发者_JAVA百科www.google.com/aclk?sa=l&ai=CPWNSJV30TK{snip}&num=2&sig=AGiWqtxY{snip}

In a PHP application, $_SERVER['HTTP_REFERER'] has the following value:

http://开发者_JAVA百科www.google.com/aclk?sa=l&ai=CPWNSJV30TK{snip}&num=2&sig=AGiWqtxY{snip}
&adurl=http://www.jumpfly.com&rct=j&q=adwords&cad=rja

My question is what is the proper way to extract the value of q?

Should I search for the position of q, then the position of the next &, and then take the substring between them? That seems a bit unprofessional since what if someday q is the final parameter in that query string and then there is no & afterwards.

Thank you.


parse_str(parse_url($_SERVER['HTTP_REFERER'], PHP_URL_QUERY), $queries);
echo $queries['q'];

References:

http://php.net/parse_url
http://php.net/parse_str


Try these:

  • parse_url(): http://php.net/manual/en/function.parse-url.php.
  • parse_str(): http://www.php.net/manual/en/function.parse-str.php


You can use parse_url() for that. From there, split the query on &.

0

精彩评论

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