How to detect the REQUEST_URI ALONG WITH ANCHOR TA

2019-09-16 15:27发布

One of my script is being called as

script.php?l=addr#anchor

I found, using SESSION [REQUEST_URI] or QUERY_STRING, gives script.php?l=addr

Is there a way to get the #anchor too using php?

Thanks in advance

3条回答
Lonely孤独者°
2楼-- · 2019-09-16 16:08

You can do this by using parse_url() function.

查看更多
家丑人穷心不美
3楼-- · 2019-09-16 16:08

sorry, can not be done with PHP, unless given directly to the link. USE Javascript(Use Framework jQuery).

Example:

CURRENT URL: website.com/index/#my-id

jQuery Code:

$.ajax({
    url         : 'document.php',
    data        : { url : document.location.href },
    dataType    : 'json',
    success     : function( response )
    {
        if( response )
        {
            //this is a example... your code here
        }
    },
    error       : function( a , b )
    {
        //ops, error!
    }
});

PHP Code:

<?php

    $id = preg_replace( '/(.*?)\#/i' , '' , $_GET['url'] );    //RETURN "my-id"
    echo $id;    //print $id
?>

Bye!!

查看更多
萌系小妹纸
4楼-- · 2019-09-16 16:10

The anchor is not usually available serverside. It is a guide to the browser. If you really want to know what anchor is called, and you have control of the site you can add &anchor=anchor at the end of your url.

If you don't have control of the place where your script is called, I don't think you have much of a chance.

Regarding the post below: It is true that you can use parse_url() to retrieve the anchor part of a given url. The issue here is that you do not have access to the full url ( including anchors ) from php.

查看更多
登录 后发表回答