English 中文(简体)
从 php 代码错误的 url 中获取 #var
原标题:get #var from url in php code error

i 想要从 url 获得 < code> javar 值,就像我的 url 是 mydomain.com/index.php#1 那样,所以我想从 url (1 ) 获得有(#) 值, url (##) 也就是 1 , 在我得到这篇文章< a href=> 之后, http://www. stoidimen.com/bbout/2009/04/15/read-the-nchor-part of-the-url-with-php/

i 使用此代码获取 has( #) 值, 在 < code> JavaScript < /code > 中工作良好, 但是在 < code> > php < /code > 中工作无效, 我的代码是 :

<script language="javascript">
var query = location.href.split( # );
document.cookies =  anchor=  + query[1];
alert(query[1]);
</script>
<?php
echo $_COOKIE[ anchor ];
?>

此代码给了我 < code> alert 值, 在 < code> JavaScript 中, 但不在 < code> > echo 值中 。 任何解决方案吗?

最佳回答

此外,您似乎在 JS 中设置了错误的财产( 它 s < code>. cookie , 而不是 < code>. cookies ):

document.cookie =  anchor  + query[1];
问题回答

您设置的曲奇在 next page request. 您链接到 的文章明确注明了 :

Of course, yes. This is not working correctly. In fact it’s working correctly from the second load on, but on the initial load of the page the $_COOKIE array does not has any anchor key inside. That’s because this part of the code is executed before the browser setup the cookie on the client.

但坦率地说, 文章中呈现了一种“变通办法 ” : 诸如此类的东西是垃圾, 如果您想要 PHP 读取, 您就不应该( 仅) 在查询碎片中( ) 输入这个信息 。

依据你所寄出的文章,你必须改变方向,例如:

<?php if (!$_COOKIE[ anchor ]){ ?>
<script language="javascript">
  var query = location.href.split( # );
  document.cookie =  anchor=  + query[1];
  window.location.reload();
</script>
<?php } ?>

<?php
echo $_COOKIE[ anchor ];
?>

您正在设置一个饼干, 在下次重新装入之前不会传递给php 。 我错了吗?

客户动态是链条的终点。

使用此方法防止错误 :

<script> 
query=location.hash;
document.cookie=  anchor +query;
</script>

当然,在PHP,爆炸那只小狗 并获得一个价值

$split = explode( / , $_COOKIE[ anchor ]);
print_r($split[1]); //to test it, use print_r. this line will print the value after the anchortag




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签