如何在php脚本中检测请求URI和锚标记

如何在php脚本中检测请求URI和锚标记,php,query-string,anchor,Php,Query String,Anchor,我的一个脚本被称为 script.php?l=addr#anchor 我发现,使用SESSION[REQUEST\u URI]或QUERY\u STRING可以给出script.php?l=addr 有没有一种方法可以使用php来获取#锚定 提前感谢您可以使用函数来完成此操作。服务器端通常不提供锚。它是浏览器的指南。 如果你真的想知道锚点叫什么,并且你可以控制网站,你可以在url的末尾添加&anchor=anchor 如果你不能控制脚本调用的位置,我认为你没有多少机会 关于以下职位: 确实,您可

我的一个脚本被称为

script.php?l=addr#anchor

我发现,使用SESSION[REQUEST\u URI]QUERY\u STRING可以给出script.php?l=addr

有没有一种方法可以使用php来获取#锚定


提前感谢

您可以使用函数来完成此操作。

服务器端通常不提供锚。它是浏览器的指南。 如果你真的想知道锚点叫什么,并且你可以控制网站,你可以在url的末尾添加&anchor=anchor

如果你不能控制脚本调用的位置,我认为你没有多少机会

关于以下职位: 确实,您可以使用parse_url()检索给定url的锚定部分。
这里的问题是,您无法从php访问完整的url(包括锚定)。

抱歉,除非直接提供给链接,否则无法使用php。 使用Javascript(使用框架jQuery)

例如:

当前URL:website.com/index/#我的id

jQuery代码:

$.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

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

$.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

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

再见