3
I have the following url
http://www.site.com/index.php#idDaDiv
I wonder if there is a variable $_SERVER[] or another preference feature in php that returns only
index.php#idDadiv
3
I have the following url
http://www.site.com/index.php#idDaDiv
I wonder if there is a variable $_SERVER[] or another preference feature in php that returns only
index.php#idDadiv
4
It is not possible, the hash/hashtag (Fragment Identifier) cannot be recovered in the back-end, regardless of the language/technology you use, whether PHP, Asp.net, or JSP.
This is because the browser does not send the hash along with HTTP request, when typed in 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 hashed ID (similar to the Scroll situation) using the selector :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>
got it! Thank you! The idea is to mark the menu item with background according to the navigated page. But one of the pages sends the request to a specific div. Ok
3
The reply from @Miguel in the comments is complete enough for the query string (as per the question).
About returning the URL without the host (www.site.com
), you can only use $_SERVER[REQUEST_URI]
. This property will return you the entire URL after the host ($_SERVER[HTTP_HOST]
).
However, in your comment, there was a doubt still in the air. So I see that a more complete answer is important.
Query String
His example was with the query string. As @Miguel commented, it is possible to recover through the following code:
$_SERVER['QUERY_STRING'];
For your initial question, the most appropriate answer is:
$_SERVER['REQUEST_URI'];
For, in addition to return everything that comes after the HOST (www.site.com
) together with the query string.
Fragment Identifier (#)
Here comes your inquiry via comment. The Fragment Identifier, Anchor, or sometimes hash(tag), is interpreted only by browser (client-side) and this is not sent to the server. Using PHP only, you will not be able to get this snippet from the URL.
What usually happens is a pre-processing of the URL and the call being made via Javascript. It is possible to recover the value of the Fragment using the following code:
window.location.hash
Basically, the Fragment Identifier and add it as query string in a request GET or to adopt it request body of a request POST.
URL Complete
To recover the full URL, just use the following code:
$actual_link = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
It will recover full URL in conjunction with the protocol. However, the attributes HTTP_HOST
and REQUEST_URI
can be manipulated via client. There is nothing we can do about it, just be aware that it can be manipulated.
Source: https://stackoverflow.com/questions/6768793/get-the-full-url-in-php
URL interpretation
If you have the URL and just want to interpret it, there is the function parse_url
parse_url('http://www.site.com/index.php?param1=123¶m2=abril¶m3=28#contatos');
Exit:
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"
}
Check this out and please forgive me for the question formulated in the first place. What I have is page.php#myId and not page.php? myId. The way I’m looking I’m not getting the whole page name with the #myID.
@Carlosrocha as explained, you will only be able to process the URL via Javascript. Rephrase the question to fit what you want;
Okay, modified as per orientation
Browser other questions tagged php
You are not signed in. Login or sign up in order to post.
Yes there is:
$_SERVER['QUERY_STRING']
, for the parameters, and__FILE__
to theindex.php
, just concatenate after– Miguel
My page looks like this: index.php#contacts. I did like this: echo FILE.$_SERVER['QUERY_STRING']; and is not exiting #contacts. What do I do? __ do not exit here in the comments
– Carlos Rocha
Hello @Carlosrocha your last issue of the question ends up invalidating the answer already accepted. This is considered a chameleon question and recommended in these cases is to reverse the last edit and create a new question.
– gmsantos
Ok! Done! as directed
– Carlos Rocha