-1

I have a url and its not a string. Its in the browser's address bar

http://localhost/sample/?post_id2#access_key=1654040333

How can I get access_key parameter that is starting with #?

I have tried to use $_SERVER['HTTP_HOST'] and $_SERVER['REQUEST_URI'] and $_SERVER['QUERY_STRING'] but in vein.

Can anyone help me out?

Omer
  • 1,727
  • 5
  • 28
  • 47

3 Answers3

2

post_id2#access_key=1654040333

access_key is not regular query parameter you'd obtain using i.e.$_GET. It's webpage anchor reference used (by design by browsers, but nowadays also by JS frameworks etc). You however are not fully lost as there's parse_url() function that can parse this for you. So:

$url = 'http://localhost/sample/?post_id2#access_key=1654040333';
var_dump(parse_url($url));

would return you the following array:

array(5) {
  ["scheme"]=>
  string(4) "http"
  ["host"]=>
  string(9) "localhost"
  ["path"]=>
  string(8) "/sample/"
  ["query"]=>
  string(8) "post_id2"
  ["fragment"]=>
  string(21) "access_key=1654040333"
}

so your access_key will be in fragment key, so you may process it further to get the value:

$parse_url_result = parse_url($_SERVER['QUERY_STRING']);
if (array_key_exists('fragment', $parse_url_result)) {
   $tmp = explode('=', $parse_url_result['fragment']);
   $access_key = $tmp[1];
}
Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • You have created the `url` in string. How can I make it as string since its in a browser's address bar – Omer Jul 29 '16 at 06:07
  • Use `$_SERVER['QUERY_STRING']`. See http://www.php.net/manual/en/reserved.variables.server.php. Check edited answer too – Marcin Orlowski Jul 29 '16 at 06:15
1

Try parsing the url through javascript

alert(window.location.hash);
FaISalBLiNK
  • 691
  • 2
  • 11
  • 24
0

You can use parse_url to get the required results.

$url = "http://localhost/sample/?post_id2#access_key=1654040333";
var_dump(parse_url($url, PHP_URL_FRAGMENT));