Wordpress 会话变量不起作用 [英] Wordpress session variable not working

查看:62
本文介绍了Wordpress 会话变量不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 WordPress 会话时遇到问题.我有一个文件test.php",用于将变量发布到 WordPress 网站.它具有条件:如果设置了会话变量,则用户可以访问整个 WordPress 站点,如果未设置会话变量,则用户无法访问该站点".

I have an issue with a WordPress session. I have a file 'test.php' that is used post a variable to a WordPress site. It has the condition: "if the session variable is set then the user can access the whole WordPress site, and if the session variable is not set then user can't access the site".

当我使用 test.php 将变量发布到 WordPress 网站时,主页工作正常,但是当我访问像xyz.com/contact"这样的内部页面时,我收到一个错误 Not Access 这意味着会话变量在下一页被清除.

When I post the variable to the WordPress site using test.php the homepage works fine, but when I access inner pages like 'xyz.com/contact' I get an error Not Access which means that the session variable was cleared on the next page.

这里是 test.php 文件:

Here is the test.php file:

<form action="wordpress-site-link" method="POST">
    <input type="submit" name="var" value="go"/>
</form>

在文件 themes/theme-name/header.php 中我写了这段代码:

In the file themes/theme-name/header.php I wrote this code:

session_start();

if(isset($_SESSION['var'])) {
      echo 'Welcome'; 
}  else if(isset($_POST['var'])) {
       $_SESSION['var'] = $_POST['var']; 
} else {
       echo 'No access...';
       exit; 
}

推荐答案

只需像这样在你的functions.php中的init"上挂一个函数:

Just hook a function on "init" in your functions.php like this :

function ur_theme_start_session()
{
    if (!session_id())
        session_start();
}
add_action("init", "ur_theme_start_session", 1);

然后你可以使用你的会话变量.

Then u can use your session variables.

希望能帮到你.

这篇关于Wordpress 会话变量不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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