如何在 Wordpress 中使用 session_start? [英] How to use session_start in Wordpress?

查看:33
本文介绍了如何在 Wordpress 中使用 session_start?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个双语网站,并决定使用 session_start 来确定页面的语言,使用以下内容:

I'm creating a bilingual site and have decided to use session_start to determine the language of the page using the following:

session_start();    
if(!isset($_SESSION['language'])){
    $_SESSION['language'] = 'English'; //default language
}

问题在于它与 Wordpress 发生冲突,我得到以下信息:

The problem with this is that it clashes with Wordpress and I get the following:

警告:session_start() [function.session-start]:无法发送会话cookie - 已发送的标头(输出开始于/home/neurosur/public_html/v2/wp-content/themes/default/header.php:8)在/home/neurosur/public_html/v2/wp-content/themes/default/region.php第 13 行

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/neurosur/public_html/v2/wp-content/themes/default/header.php:8) in /home/neurosur/public_html/v2/wp-content/themes/default/region.php on line 13

有没有办法解决这个问题?

Is there a way to get around this?

推荐答案

我已将我的原始答案更改为@rafi 的正确答案(见下文).

I've changed my original answer to the correct one from @rafi (see below).

在您的functions.php文件中写入以下代码:

Write the following code in your functions.php file:

function register_my_session()
{
  if( !session_id() )
  {
    session_start();
  }
}

add_action('init', 'register_my_session');

这篇关于如何在 Wordpress 中使用 session_start?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆