Hello,

I was wondering how to test for an anchor in the URL and to get its value.

Most URLs look something like this:

showthread.php?t=10376481

But sometimes an anchor is appended to the URL:

showthread.php?t=10376481[B]#4242[/B]

The reason I am asking is because on my forum I want members to be able to send links to posts. To be able to do that, I need to send an anchor. However, the issue lies in that members may have different display settings so they'll be displaying a different number of posts per page than perhaps the person sending the link.

If the link sent is to the 21st post, but the person receiving it is displaying 20 posts per page, then it would be on the second page.

What I am looking to achieve is:

1) Test for an anchor's presence. If it's not there then don't worry about it.
2) If it's there, get its value and determine which post it is.
3) Once the post is determined, determine which page the post is on.
4) Once that is determined, direct the user there.

Unless there's a completely different (and easier) method of doing this.

Thank you for your time. 🙂

    As far as I know, you cannot obtain the anchor value from PHP. I am not sure if it can be done via clientside scripting, but it may be easier just to pass that post id as a name/value pair.

      laserlight;10967078 wrote:

      As far as I know, you cannot obtain the anchor value from PHP. I am not sure if it can be done via clientside scripting, but it may be easier just to pass that post id as a name/value pair.

      Yeah I just may have to do that. I'm just curious how forum software such as vBulletin gets away with it.

        dagon;10967107 wrote:

        parse_url

        Ah, thank you! I'll take a look at this and see what I come up with.

          Bonesnap;10967084 wrote:

          I'm just curious how forum software such as vBulletin gets away with it.

          It doesn't. The anchor portion of the URL isn't transmitted to the server, thus there's no way to "use" it in a server-side application.

            the previous poster is right: the server can't pick up on the anchor in your URL. I would recommend that you use a query statement like this: "www.example.com?anchor=4242", to link to a section of the page and use this code to at the top of every page to check to see if an anchor is valid.

            if ((isset($_SERVER['QUERY_STRING']) || isset($_GET['t'])) && stripos($_SERVER['QUERY_STRING'], 'anchor=') != false) {//check to see if there is a query string being passed
            	$qs = $_SERVER['QUERY_STRING'];//get the query string
            	$anchor = substr($qs, stripos($qs, 'anchor=')+7);
            	$file = file_get_contents($_SERVER['PHP_SELF']);//set the contents of the current file to the string $file
            	$anchor_empty = strripos($file, 'name="' . $anchor . '"') == false;//check to see if name="4243(or whatever the anchor is)" doesn't exists
            
            if (!$anchor_empty){header('Location: http://www.example.com/snowthread.php?t=(some forum number)#(some anchor number)');}//send the user to the webpage now that we know the anchor exists
            }
            

            good luck.

              Write a Reply...