在WordPress中使用非默认永久链接的PHP会话问题 [英] Problem with PHP sessions in WordPress with non-default permalinks
问题描述
当我在WordPress中更改默认永久链接时,出现了奇怪的问题。
我的主题(甚至在WordPress的根index.php文件)functions.php文件,并浏览和更新几页(例如/关于页面),我的计数器往往运行两次。
这个问题在我尝试过的每个服务器上以及任何主题或WordPress安装中都被重现。另一件让我的大脑更加困惑的事情是,它不会出现在Safari,Internet Explorer(6,7和8)中,而是仅在Firefox中 。我使用的是Firefox 3.6.8,并且已经在Mac OS X和Windows XP中使用了它。
session_start();
$ counter = $ _SESSION ['wp_action_counter'];
if(!isset($ counter)){
$ counter = 0;
}
$ counter ++;
echo $ counter;
$ _SESSION ['wp_action_counter'] = $ counter;
我的.htaccess规则看起来像这样
< IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^ index\.php $ - [L]
RewriteCond%{REQUEST_FILENAME}!-f
RewriteCond%{REQUEST_FILENAME}! -d
RewriteRule。 /index.php [L]
< / IfModule>
我很好奇为什么会发生这种情况,并且非常感谢能帮助我解决问题的人。
我终于找到了问题所在。
当我有<?php wp_head();
< link rel ='next ...>
'title ='About'href ='http://mysite.dev/about/'/>
这个链接是 prefetched ,这就是计数器运行两次的原因。调整 wp_head();
的输出或移除它可以解决问题。
I'm experiencing a weird problem that occurs when I change the default permalinks in WordPress.
When I place this code in my functions.php file in my theme (or even in the root index.php file in WordPress) and navigate between and updating a few pages (for example the /about page), my counter often runs twice.
This problem is reproduced on every server I tried and on any theme or WordPress installation. Another thing that makes my brain even more confused is that it does not occur in Safari, Internet Explorer (6, 7 & 8), but only in Firefox. I'm using Firefox 3.6.8 and has tried it in both Mac OS X and Windows XP.
session_start();
$counter = $_SESSION['wp_action_counter'];
if( !isset($counter) ){
$counter = 0;
}
$counter++;
echo $counter;
$_SESSION['wp_action_counter'] = $counter;
My .htaccess rules looks like this
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
I'm curious why this happens and is ever thankful to the one who can help me solve the problem.
I have finally found out what is the problem.
When I have <?php wp_head(); ?>
in my header, WordPress adds this line:
<link rel='next' title='About' href='http://mysite.dev/about/' />
This link is prefetched and this is the cause why the counter runs twice. Adjusting the output of wp_head();
or removing it solves the problem.
这篇关于在WordPress中使用非默认永久链接的PHP会话问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!