Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

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

http://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.

like image 389
davidjhp Avatar asked Nov 30 '10 02:11

davidjhp


People also ask

What is $_ SERVER [' HTTP_REFERER ']?

$_SERVER['HTTP_REFERER'] Returns the complete URL of the current page (not reliable because not all user-agents support it) $_SERVER['HTTPS'] Is the script queried through a secure HTTP protocol.

How do I get http referer in PHP?

$_SERVER['HTTP_REFERER'] will give you the referrer page's URL if there exists any. If users use a bookmark or directly visit your site by manually typing in the URL, http_referer will be empty. Also if the users are posting to your page programatically (CURL) then they're not obliged to set the http_referer as well.

Is HTTP referer reliable?

Using HTTP_REFERER isn't reliable, its value is dependent on the HTTP Referer header sent by the browser or client application to the server and therefore can't be trusted because it can be manipulated.


2 Answers

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

like image 59
deceze Avatar answered Oct 15 '22 09:10

deceze


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
like image 24
Blender Avatar answered Oct 15 '22 09:10

Blender