7

Tried to print the $_SERVER array in PHP, yet I can't find what I want:

http://www.domain.com/#sometaginpage

I want the "sometaginpage".

Help. Thanks!

JoHa
  • 1,989
  • 10
  • 28
  • 42
  • 3
    `window.location.hash` in JavaScript? – Marty May 25 '11 at 03:58
  • possible duplicate of [How to get the value after the hash in "somepage.php#name"?](http://stackoverflow.com/questions/1917762/how-to-get-the-value-after-the-hash-in-somepage-phpname) – PhoneixS Jul 08 '15 at 09:38

3 Answers3

9

The browser doesn't actually send anything that comes after the hash(#) to the server because it is resolved within the browser.

Sean Walsh
  • 8,266
  • 3
  • 30
  • 38
4

Fairly certain that #hashtags are NOT sent to the server, but you could develop a workaround with AJAX:

some-page.html:

<script type="text/javascript">
    $(document).ready(function() {
        $(window).bind('hashchange', function() {
            var hash = window.location.hash.substring(1);
            $.get('ajax-hash.php', { tag: hash },
                function(data) { $('#tag').html(data); }
            );
        });
    });
</script>

<div id="tag"></div>
<a href="#one">#one</a> | <a href="#two">#two</a> | <a href="#lolwut">#lolwut</a>

ajax-hash.php:

<?php
    $hash = isset($_GET['tag']) ? $_GET['tag'] : 'none';
    echo $_SERVER['HTTP_REFERER'] . '#' . $hash;
?>

Note: This is dependent on the browser actually sending the HTTP_REFERER.. Since it's done through jQuery, it SHOULD.. but no promises! (Antivirus/Firewalls love to strip that from your packets)

drudge
  • 35,471
  • 7
  • 34
  • 45
0

I'm not sure if $_SERVER['REQUEST_URI'] will give it or not. s992 might be right, it may not send that to the server.

John
  • 1,530
  • 1
  • 10
  • 19