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.
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'
}
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"
}
精彩评论