#reply3 of URL

Let’s suppose I have the url above.

The code below produces 2

echo $_GET['forum'];

The code below produces 101

echo $_GET['post'];

I like to get the value “reply3” or just “3” with the url above.
The code below doesn’t work correctly. but I hope it shows what I want.

echo $_GET['#'];

Pound signs aren’t really recognized like that. If you want the specific reply number, you should target that with a regular <a> tag with an ID of that specific reply number.

Is there any way for recognizing it in PHP?
I tried the following for it. However none of these recognize the pound sign.

$_SERVER['SCRIPT_NAME']
$_SERVER['REQUEST_URI']
$_SERVER["PHP_SELF"]

You could explode() it at the pound sign and grab what’s behind it

PHP rarely receives the anchor portion of the URL - the browser may choose not to transmit it in the request. This is more often the domain of Javascript to play with.

If it’s part of the URL you receive, parse_url would be the way to go.

parse_url($_SERVER['REQUEST_URI'], PHP_URL_FRAGMENT)

I don’t think that would work. but grabbing the content from XMLHttpRequest with js would work for sure.

No, parse_url should work. It’ll be in the fragment index array.

What are you trying to achieve? The idea doesn’t sound right. If you want to select 1 specific reply and display it to the screen based on a link like that, then I can see how this would make sense. But if you’re trying to get the user to that specific reply, it doesn’t make sense at all and what I said in my initial response would be the way to go.

<?php

echo 'request_uri=' .$_SERVER['REQUEST_URI']. '<br>';
echo 'parse_url=' .parse_url($_SERVER['REQUEST_URI'], PHP_URL_FRAGMENT);

I have the code above at http://form.kr/q/_SERVER/URL/fragment.php?forum=2&post=101#reply3.
The result of the page above is the following.

I cannot get the value “reply3” or “3”.
Does it mean “parse_url($_SERVER[‘REQUEST_URI’], PHP_URL_FRAGMENT)” doesn’t work or I apply it wrong way?

@joon1,

Try the following to show all possible values returned from the server:

<?php declare(strict_types=1);
echo ‘<pre>’; // add line feeds
 print_r($_SERVER);
echo’</pre>’;
die;

That’s funny, but looks like fragment-part ignored in $_SERVER['REQUEST_URI'] and not found anywhere else in $_SERVER. Hm… I just don’t know, how can I get it.

As I try your code above with URL_of_myDomain?#reply3, it produces Array of many Server values, but I cannot find any value of neither reply3 nor 3.

It means I should conclude there is no way for recognizing it in PHP.

1 Like

From a bit of reading just now, it seems that the issue here is not PHP, but that the browser does not actually send the anchor (the bit after the # symbol) to the server as @m_hutley said above. Presumably it retains that part, and when the page content arrives, searches through the content for the appropriate tag and sets focus there. So if it isn’t sent to the server, you won’t be able to access it regardless of what server-side language you use. Your server logs would confirm that the request does not contain it.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.