假设有如下URL:
www.example.com/?val=1#part2
PHP可以通过GET数组读取请求变量val1
。
哈希值part2
是否也可读取?还是这只限于浏览器和JavaScript?
假设有如下URL:
www.example.com/?val=1#part2
PHP可以通过GET数组读取请求变量val1
。
哈希值part2
是否也可读取?还是这只限于浏览器和JavaScript?
是的,你可以:
使用这种方法来防止错误:
<script>
query=location.hash;
document.cookie= 'anchor'+query;
</script>
$split = explode('/', $_COOKIE['anchor']);
print_r($split[1]); //to test it, use print_r. this line will print the value after the anchortag
<script>
sendTokenParameter();
function sendTokenParameter() {
if (!window.location.hash) {
return;
}
const id_token = window.location.hash.split('&').filter(function(el) { if(el.match('id_token') !== null) return true; })[0].split('=')[1];
if (!id_token) {
console.error('No token returned');
return;
}
window.location.href = `${window.location.pathname}?id_token=${id_token}`
}
</script>
<?php
process_login();
function process_login() {
$id_token = $_GET['id_token'];
if (!$id_token) {
// Let the javascript code to send the token
return;
}
// Do stuff with the token
}
?>
另一种解决方案是在 PHP 页面中添加一个隐藏的输入字段:
<input type="hidden" id="myHiddenLocationHash" name="myHiddenLocationHash" value="">
$('#myHiddenLocationHash').val(document.location.hash.replace('#',''));
$server_location_hash = $_POST['myHiddenLocationHash'];