I have the following url
http://www.site.com/index.php#idDaDiv
I would like to know if there is a $ _ SERVER [] variable or another preference feature in php that returns only
index.php#idDadiv
I have the following url
http://www.site.com/index.php#idDaDiv
I would like to know if there is a $ _ SERVER [] variable or another preference feature in php that returns only
index.php#idDadiv
It is not possible , the hash / hashtag ( Fragment identifier ) can not be retrieved in the backend, regardless of the language / technology you use, be it PHP, or asp.net, be JSP.
This occurs because the browser does not send the hash along with HTTP request when typed in the AddressBar.
The hash is used for front-end interactions, such as with CSS, HTML, and JavaScript.
An example of using HTML is to scroll (scroll to the desired item):
body {
background: #f8f8f8;
}
section {
box-shadow: 0 1px 5px rgba(0,0,0,.3);
background: #fff;
margin: 5px;
padding: 15px;
min-height: 140px;
}
.lnks {
position: fixed;
bottom: 0;
right: 0;
}
<section id="foo">Sessão 1.</section>
<section id="bar">Sessão 2.</section>
<section id="baz">Sessão 3.</section>
<div class="lnks">
<a href="#foo">vai para foo</a> |
<a href="#bar">vai para bar</a> |
<a href="#baz">vai para baz</a>
</div>
You can use to highlight an item with the ID equal to hash (similar to the Scroll situation) using the :target
:
body {
background: #f8f8f8;
}
section {
box-shadow: 0 1px 5px rgba(0,0,0,.3);
background: #fff;
margin: 5px;
padding: 15px;
width: 140px;
height: 140px;
display: inline-block;
}
section:target {
color: #fff;
font-weight: bold;
background: #8f8f8f;
}
.lnks {
position: fixed;
bottom: 0;
right: 0;
}
<section id="foo">Sessão 1.</section>
<section id="bar">Sessão 2.</section>
<section id="baz">Sessão 3.</section>
<div class="lnks">
<a href="#foo">vai para foo</a> |
<a href="#bar">vai para bar</a> |
<a href="#baz">vai para baz</a>
</div>
The response from @Miguel in the comments is complete enough for the query string part (depending on the question).
On returning the URL without the HOST ( www.site.com
), you can only use $_SERVER[REQUEST_URI]
. This property will return the entire URL after the host ( $_SERVER[HTTP_HOST]
).
However, in your comment, a question remained on the air. So I see a more complete answer is important.
Query String
Your example case was with query string . As @Miguel commented, it is possible to recover through the following code:
$_SERVER['QUERY_STRING'];
For your initial questioning, the most appropriate answer is:
$_SERVER['REQUEST_URI'];
Well, it will return everything that comes after the HOST ( www.site.com
) matching query string .
Fragment Identifier (#)
Here comes your question via comment. The fragment identifier , anchor, or sometimes hash, is interpreted only ( client-side >) and it is not sent to the server. Using only PHP, you will not be able to get this snippet from the URL.
What usually happens is a URL preprocessing and the call being made via JavaScript. You can retrieve the fragment value using the following code:
window.location.hash
You should basically retrieve fragment identifier and add it as query string in a request GET or add it to < in> request body from a POST request.
Full URL
To retrieve the complete URL, simply use the following code:
$actual_link = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
It will retrieve full URL in conjunction with the protocol. However, the HTTP_HOST
and REQUEST_URI
attributes can be handled via client. There is nothing we can do about it, just be aware that it can be manipulated.
Source: link
URL Interpretation
If you have the URL and just want to interpret it, the parse_url
parse_url('http://www.site.com/index.php?param1=123¶m2=abril¶m3=28#contatos');
Output:
array(5) {
["scheme"]=>
string(4) "http"
["host"]=>
string(12) "www.site.com"
["path"]=>
string(10) "/index.php"
["query"]=>
string(33) "param1=123¶m2=abril¶m3=28"
["fragment"]=>
string(8) "contatos"
}