48

Is there a way to get the entire URL used to request the current page, including the anchor (the text after the # - I may be using the wrong word), in included pages?

i.e. page foo.php is included in bar.php. If I use your solution in foo.php, I need it to say bar.php?blarg=a#example.

Martijn
  • 15,791
  • 4
  • 36
  • 68
Alex S
  • 25,241
  • 18
  • 52
  • 63

8 Answers8

91

No, I am afraid not, since 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).

Labu
  • 2,572
  • 30
  • 34
Alistair Evans
  • 36,057
  • 7
  • 42
  • 54
16
// find out the domain:
$domain = $_SERVER['HTTP_HOST'];
// find out the path to the current file:
$path = $_SERVER['SCRIPT_NAME'];
// find out the QueryString:
$queryString = $_SERVER['QUERY_STRING'];
// put it all together:
$url = "http://" . $domain . $path . "?" . $queryString;
echo $url;

// An alternative way is to use REQUEST_URI instead of both
// SCRIPT_NAME and QUERY_STRING, if you don't need them seperate:
$url = "http://" . $domain . $_SERVER['REQUEST_URI'];
echo $url;
Mihir Bhatt
  • 3,019
  • 2
  • 37
  • 41
5

You can't - you'll have to write the value of the hash to a cookie via Javascript to send it to the server on the subsequent request.

karim79
  • 339,989
  • 67
  • 413
  • 406
5

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.

Alex Weinstein
  • 9,823
  • 9
  • 42
  • 59
2

This example show's full URL request with port or https

function curPageURL() {
            $pageURL = 'http';
            if(isset($_SERVER["HTTPS"]))
            if ($_SERVER["HTTPS"] == "on") {
                $pageURL .= "s";
            }
            $pageURL .= "://";
            if ($_SERVER["SERVER_PORT"] != "80") {
                $pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
            } else {
                $pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
            }
            return $pageURL;
        }

usage : echo curPageURL();

Elshan
  • 7,339
  • 4
  • 71
  • 106
  • This doesn't answer the crux of the question. Returning the portion of the url after the # – joel Mar 01 '17 at 16:31
0

It is true that you can't pass hash data to server. but sometimes what you want is get some url with '#blablabla' from the server, like when you share link to someone with '#', but the site need to login. below is another way to think, maybe not vary detailed.

First, sometimes you want to share your link with '#',like:

www.so.com#lala

First, you can change your url use javascript and pass some data with '?' and '#' at the same time, like:

www.so.com?foo=lala&&flag=hash#lala

then, as the '#' nerver pass to the server, but you can get the data from $_GET[''],like:

if($_GET['flag'] === 'hash'){

  // base url
  $_SESSION['url'] = strtok('http://'.$_SERVER['HTTP_HOST'].$_SERVER["REQUEST_URI"],"?"); 

  // hash data
  $_SESSION['hash'] = $_GET['foo'];
}

then, you can do everything with the '#' data, if you want to pass it to client, you can:

$url = $_SESSION['url']."#".$_SESSION['hash'];
header("Location:".$url);

At last, the url is back

gy134340
  • 566
  • 5
  • 19
-1

Syntax Error is right. And what I'd like to do is intercept the #foo from the call dosomething.org/page.php#foo and act as if I'd found a dosomething.org/page.php?x=foo call. Yes, this may function on the client but it's theoretically available at the server.

Why do I want this? Because people have old links stashed in their emails and I want to handle them differently.

Eclipsoid
  • 1
  • 4
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 28 '22 at 08:16
-5

You can pass the anchor using rawurlencode funcion.

Basically you just need to do something like this:

bar.php?blarg=a<?php echo rawurlencode('#blahblahblah')?>
Bart
  • 19,692
  • 7
  • 68
  • 77
  • I think you misunderstood the question. He wanted to read the named anchor from the url, not encode one for output. – Syntax Error Sep 29 '12 at 00:37