Wordpress Session Variable Doesn't Work

I have a problem with a WordPress session. I have a test.php file that is used to post a variable to a WordPress site. It has the condition: "if a session variable is set, the user can access the entire WordPress site, and if the session variable is not set, the user cannot access the site."

When I submit this variable to the WordPress site using test.php, the page works fine, but when I access the internal pages, for example "xyz.com/contact", I get a Not Access error message, which means the session variable was cleared on the next page.

Here is the test.php file:

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

In the themes / theme-name / header.php file, 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; } 
+5
source share
1 answer

Just hook the "init" function in your functions.php as follows:

 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.

Hope u helps.

+6
source

Source: https://habr.com/ru/post/1233086/


All Articles