getting the full url including the query string af

2020-01-27 06:59发布

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.

标签: php url hashtag
3条回答
Animai°情兽
2楼-- · 2020-01-27 07:26

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

查看更多
叛逆
3楼-- · 2020-01-27 07:40

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"]);
    }
查看更多
家丑人穷心不美
4楼-- · 2020-01-27 07:45

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.

查看更多
登录 后发表回答