Wordpress session variable not working

Problem : Wordpress session variable not working

I have an issue with a WordPress session.

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

1
2
3
4
5
6
7
8
9
10
session_start();
 
if(isset($_SESSION['var'])) {
      echo 'Welcome';
else if(isset($_POST['var'])) {
       $_SESSION['var'] = $_POST['var'];
} else {
       echo 'No access...';
       exit;
}

Solution For : Wordpress session variable not working

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

1
2
3
4
5
6
function ur_theme_start_session()
{
    if (!session_id())
        session_start();
}
add_action("init", "ur_theme_start_session", 1);

Then we can use our session variables.

I hope that help you.

No comments:

Post a Comment