从 url 获取片段后的 PHP 问题。
我有一些页面,它的网址类似于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的代码实际上还没有接近获得这两个值。我不知道该怎么做,因为它太远了。
如果您的 PHP 代码在设置新位置之后运行,那么您将希望值
more
和starting
为 before 哈希 (#
),因为哈希部分不会发送到服务器。如果您想在 JavaScript 中获取
more
和starting
值,那么可以在任一位置,但您需要知道您处于哪个阶段才能以正确的方式获取它。示例
URL:http://www.domainname.com/m2.php?more=apple&starting=1
查询字符串:
more=apple&starting=1
哈希值:无
URL:http://www.domainname.com/m1.php?#m2.php?more=apple&starting=1
查询字符串: >无
哈希值:
m2.php?more=apple&starting=1
JavaScript:
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
andstarting
to be before the hash (#
) as the hash part does not get sent to the server.If you want to get the
more
andstarting
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
URL: http://www.domainname.com/m1.php?#m2.php?more=apple&starting=1
Query string: none
Hash:
m2.php?more=apple&starting=1
Javascript:
或者,如果您希望获取此信息以便在 PHP 中使用,则
parse_url()
和parse_str()
的简单组合也能达到目的:
$vars
然后产生:Alternatively, if you're looking to get this information for use in PHP, a simple combination of
parse_url()
andparse_str()
will also do the trick:A dump of
$vars
then yields: