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.
$_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.
$_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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With