【问题标题】:can php get value from url after hash with js href.split?php 可以在使用 js href.split 散列后从 url 获取值吗?
【发布时间】:2011-05-11 21:33:11
【问题描述】:

我有一些网址,例如http://www.domainname.com/m1.php?#m2.php?more=apple&starting=1

如何使用PHP获取值$_GET['more']&$_GET['starting']?

是否可以使用 js href.split 来获取值 hehind hash #?

更新问题:

添加一些代码如下:我仍然得到$_GET['fragment'] = m2.php?more=apple,但丢失了&starting=1,接下来我该怎么办?

<script type="text/javascript">
var parts = location.href.split('#');
if(parts.length > 1)
{
    var params = parts[0].split('?');
    var mark = '?';
    if(params.length > 1)
    {
        mark = '&';
    }
    location.href = parts[0] + mark + 'fragment=' + parts[1];
}
</script>
<?php
if(isset($_GET['fragment']))
{
    echo $_GET['fragment'];
}
?>

【问题讨论】:

  • JS 将是获取散列部分以发送给客户端的唯一方法,然后只能从 URL 中提取它并作为常规查询参数插入。
  • @Marc B,所以如果使用 $_SERVER["REQUEST_URI"] 获取当前页面的 url。然后使用parse_url 获取fragment 部分,然后做一些事情来探索2个值...有可能吗?
  • request_uri 没有片段
  • @yes123,那么如何获取带有片段的当前网址?
  • @yes123:哇,你说得对。 RFC 2396 cleary 声明,片段仅在客户端。从我的脑海中滑落。应该去睡觉了。删除了我的答案。

标签: php javascript hash get split


【解决方案1】:

在 php 中你根本看不到请求的片段。

你只能用javascript看到它

【讨论】:

  • 那么你有什么想法,如何从 js 中获取价值并传递给 php?
  • 你在一个问题中问了太多东西。使用 js,您可以使用 window.location.hash 使用 jQuery 获取此字符串后,您可以进行简单的 ajax 调用(如果您不知道如何进行 ajax 调用,请打开另一个问题)
  • 好的,仍然回到这个问题作为我的标题描述,我不会再问这个话题了。我更新了我的问题,添加了一些代码。你能帮我解决吗?谢谢。
【解决方案2】:

我认为你应该可以使用 PHP 的内置函数 parse_url http://php.net/manual/en/function.parse-url.php

如果查看返回值,可以得到查询参数作为哈希,# 后面的值是fragment

【讨论】:

    【解决方案3】:

    您可以这样做以在 php 中使用哈希片段。

    需要重新加载页面,但它可以工作。 您可以将此脚本标记设置在任何其他 js 标记之前,这样不会延迟重新加载。

    <script type="text/javascript">
    if(location.hash.length > 0){
        var hash = location.hash.replace('#','');
        location.href = hash;
        //or
        //location.href = 'index.php?param1=1&hash'+hash;
        //or
        //location.href = '<?=$_SERVER['SCRIPT_NAME'].'?'.$_SERVER['QUERY_STRING'].'&hash='?>'+hash;
    }
    </script>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-31
      • 2017-01-08
      • 2023-01-25
      • 2013-07-13
      • 1970-01-01
      • 1970-01-01
      • 2013-01-19
      相关资源
      最近更新 更多