4

How to get the full URL including the string parameter after hash tag? I try to echo

$url = $_SERVER['REQUEST_URI'];
echo $url;

the string after the hash tag wont read.

Suvash sarker
  • 3,140
  • 1
  • 18
  • 21
user3034828
  • 73
  • 2
  • 2
  • 7
  • 13
    That part never makes it to the server, it's exclusive to the browser. – Pekka Nov 29 '13 at 07:12
  • @Pekka웃 great answer ;) . you can use the js function and pass it for other works. here is a question like your question. http://stackoverflow.com/questions/12469350/how-can-i-get-hash-code-from-url-when-i-write-url-and-press-enter – babak faghihian Nov 29 '13 at 07:17
  • @Pekka웃 so even if I use `encodeURIComponent()` the browser will still omits everything after `%23`? – lightbringer Oct 14 '15 at 22:45

3 Answers3

12

Pekka's comment should be an answer. The string parameter after the hash tag is not sent to the server, it's for the browsers eyes only.

This means that serverside code (PHP, in your case) does not have this info. The clientside code (the browser, javascript, ...) does.

Ideally,

  • the part after the ? is info for the server. Put everything your server needs here
  • the part after the # is info for the client. Put everything your client needs here. It's called the Fragment Identifier (Thanks Tim).

Historically, the part after the # was most often used to have your browser quicky scroll to a defined anchor on the page. Nowadays, it is more often used to hold state information for the client.

You could have javascript send this info to the server, or perform different actions based on this info. AJAX is your friend.

Konerak
  • 39,272
  • 12
  • 98
  • 118
  • +1, and nice answer! Is `#inbox` in `https://mail.google.com/mail/u/1/#inbox` also [fragment identifier](https://en.wikipedia.org/wiki/Fragment_identifier) and info for the client? – Tim Jun 28 '17 at 15:00
  • Tim: nice find, I'll add it! – Konerak Jun 28 '17 at 17:33
  • Is `#inbox` in `https://mail.google.com/mail/u/0/#inbox` a means for navigating from page to page without a page refresh? Is it implemented on server or client (web browser)? – Tim Jun 29 '17 at 01:32
  • Tim: did you read your own link? :) The Fragment Identifier is supposed to be used client-side only (although a script could change this). Back in the days it was frequently used to navigate within a page, using anchors. Nowadays, it's (re)used for similar purposes, for example one-page-websites where the contents is dynamically loaded. – Konerak Jun 29 '17 at 07:00
2

The hash (the string including the #) never gets passed to the server, it is solely a behavioural property of the browser. The $_SERVER['REQUEST_URI'] variable will contain the rest however.

If you really need to know what the hash is, you will have to use the document.location.hash JavaScript property, which contains the contents of the hash (you could then insert it in a form, or send it to the server with an ajax request).You can pass up the full URL, including the anchor (the part after the #), using a Javascript onload function that sends that URL to an Ajax endpoint.

You can also take a look here Get entire URL, including query string and anchor

Community
  • 1
  • 1
Suvash sarker
  • 3,140
  • 1
  • 18
  • 21
0

use urlencode() and urldecode() functions

In this short example, I will show you how to pass Hash value to the server and make it redirect to the hash value.

Firstly encode the Hash value in the link button

<a href="mylink.php?redirect=link1<?= urlencode("#tab5") ?>">redirect to Link1 </a>

Now to redirect to the link from the server mylink.php

if ($_GET["redirect"] != null )
    {
        header("location: urldecode($_GET["redirect"]);
    }