How to detect the REQUEST_URI ALONG WITH ANCHOR TAG in a php script

▼魔方 西西 提交于 2019-12-13 04:19:42

问题


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


回答1:


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.




回答2:


You can do this by using parse_url() function.




回答3:


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!!



来源:https://stackoverflow.com/questions/8744299/how-to-detect-the-request-uri-along-with-anchor-tag-in-a-php-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!