从 url 获取片段后的 PHP 问题。

发布于 2024-11-06 13:46:22 字数 750 浏览 0 评论 0原文

我有一些页面,它的网址类似于 http://www.domainname.com/m1.php?#m2.php?more=apple&starting=1

我想要获取值 $_GET['more'] & $_GET['开始']

我使用此代码

<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'];
}
?>

,但仍然得到 m2.php?more=apple 并丢失了 starting=1。如何获得其中2个?然后分为 $_GET['more'] & $_GET['开始']?谢谢。

I have some page, it's url is like http://www.domainname.com/m1.php?#m2.php?more=apple&starting=1.

I want get the value $_GET['more'] & $_GET['starting'].

I use this code

<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'];
}
?>

but I still get the m2.php?more=apple and lost starting=1. How to get 2 of them? and then divide into $_GET['more'] & $_GET['starting']? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧夏天 2024-11-13 13:46:22

您的代码实际上还没有接近获得这两个值。我不知道该怎么做,因为它太远了。

如果您的 PHP 代码在设置新位置之后运行,那么您将希望值 morestartingbefore 哈希 (#),因为哈希部分不会发送到服务器。

如果您想在 JavaScript 中获取 morestarting 值,那么可以在任一位置,但您需要知道您处于哪个阶段才能以正确的方式获取它。

示例

URL:http://www.domainname.com/m2.php?more=apple&starting=1

查询字符串:more=apple&starting=1
哈希值:

<?php
echo $_GET['more']; // apple
echo $_GET['starting']; // 1

URL:http://www.domainname.com/m1.php?#m2.php?more=apple&starting=1

查询字符串: >无
哈希值:m2.php?more=apple&starting=1

<?php
echo count($_GET) // 0

JavaScript:

var hash = location.hash; // m2.php?more=apple&starting=1
var hashparts = hash.split("?");
var urlhashpart = hashparts[0]; // m2.php
var querystringhashpart = hashparts[1]; // more=apple&starting=1
var params = querystringhashpart.split("&");
for (var i in params) {
    var param = params[i].split("=");
    document.write(param[0] + ": " + param[1]); // outputs 'more: apple', 'starting: 1'
}

Your code really isn't even close to getting those two values. I'm not sure what to make of it since it is so far off.

If your PHP code is running after setting the new location, then you will want the values more and starting to be before the hash (#) as the hash part does not get sent to the server.

If you want to get the more and starting values in JavaScript, then it can be either place, but you need to know what stage you are at to get it the right way.

Examples

URL: http://www.domainname.com/m2.php?more=apple&starting=1

Query string: more=apple&starting=1
Hash: none

<?php
echo $_GET['more']; // apple
echo $_GET['starting']; // 1

URL: http://www.domainname.com/m1.php?#m2.php?more=apple&starting=1

Query string: none
Hash: m2.php?more=apple&starting=1

<?php
echo count($_GET) // 0

Javascript:

var hash = location.hash; // m2.php?more=apple&starting=1
var hashparts = hash.split("?");
var urlhashpart = hashparts[0]; // m2.php
var querystringhashpart = hashparts[1]; // more=apple&starting=1
var params = querystringhashpart.split("&");
for (var i in params) {
    var param = params[i].split("=");
    document.write(param[0] + ": " + param[1]); // outputs 'more: apple', 'starting: 1'
}
囍笑 2024-11-13 13:46:22

或者,如果您希望获取此信息以便在 PHP 中使用,则 parse_url()parse_str() 的简单组合也能达到目的

$url = parse_url('http://www.domainname.com/m1.php?#m2.php?more=apple&starting=1');
$anchorparts = explode('?',$url['fragment']);
parse_str($anchorparts[1],$vars);

$vars 然后产生:

array(2) {
  ["more"]=>
  string(5) "apple"
  ["starting"]=>
  string(1) "1"
}

Alternatively, if you're looking to get this information for use in PHP, a simple combination of parse_url() and parse_str() will also do the trick:

$url = parse_url('http://www.domainname.com/m1.php?#m2.php?more=apple&starting=1');
$anchorparts = explode('?',$url['fragment']);
parse_str($anchorparts[1],$vars);

A dump of $vars then yields:

array(2) {
  ["more"]=>
  string(5) "apple"
  ["starting"]=>
  string(1) "1"
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文