Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php从URL获取哈希值_Php_Javascript_Variables - Fatal编程技术网

Php从URL获取哈希值

Php从URL获取哈希值,php,javascript,variables,Php,Javascript,Variables,如何在php中获取散列的变量 我在页面上有这样一个变量 catalog.php#album=2song=1 如何获取唱片集和歌曲的值并将它们放入PHP变量中 使用PHP无法获取此值,因为PHP在服务器端处理内容,而URL中的哈希仅在客户端处理,从不发送到服务器。JavaScript可以使用window.location.hash(并且可以选择调用包含此信息的PHP脚本,或者将数据添加到DOM中)来获取哈希值。只需添加到@Alec的答案中即可 有一个功能: 它可以在hashmark#之后返回片段

如何在php中获取散列的变量

我在页面上有这样一个变量

catalog.php#album=2song=1

如何获取唱片集和歌曲的值并将它们放入PHP变量中

使用PHP无法获取此值,因为PHP在服务器端处理内容,而URL中的哈希仅在客户端处理,从不发送到服务器。JavaScript可以使用
window.location.hash
(并且可以选择调用包含此信息的PHP脚本,或者将数据添加到DOM中)来获取哈希值。

只需添加到@Alec的答案中即可

有一个功能:

它可以在hashmark#之后返回
片段。但是,在您的情况下,它将返回哈希标记后的所有值:

Array
(
    [path] => catalog.php
    [fragment] => album=2song=1
)

正如@NullUserException所指出的,除非您事先有url,否则这实际上是毫无意义的。但是,我还是很高兴知道这一点。

您可以使用AJAX/PHP来实现这一点。您可以使用javaScript获取哈希,并使用PHP加载一些内容。 假设我们正在加载一个页面的主要内容,那么我们的带有哈希的URL是“”:

我们头脑中的JavaScript:

 function getContentByHashName(hash) { // "main"
    // some very simplified AJAX (in this example with jQuery)
    $.ajax({
      url: '/ajax/get_content.php?content='+hash, // "main"
      success: function(content){
        $('div#container').html(content); // will put "Welcome to our Main Page" into the <div> with id="container"
      }
    });
 }

 var hash=parent.location.hash; // #main
 hash=hash.substring(1,hash.length); // take out the #

 getContentByHashName(hash);
函数getContentByHashName(哈希){/“main”
//一些非常简化的AJAX(在这个使用jQuery的示例中)
$.ajax({
url:'/ajax/get_content.php?content='+hash,//“main”
成功:功能(内容){
$('div#container').html(content);//将在with id=“container”中放入“欢迎访问我们的主页”
}
});
}
var hash=parent.location.hash;//#主要的
hash=hash.substring(1,hash.length);//取出#
getContentByHashName(哈希);
PHP可以有如下内容:

<?php
// very unsafe and silly code

$content_hash_name = $_GET['content'];

if($content_hash_name == 'main'):
  echo "Welcome to our Main Page";
endif;

?>


除非您事先有URL,否则这是没有用的。谢谢。听起来我可以把这个想法运用到我需要的东西中去