“Proper” way to pass session information
-
Hello, as an adjunct to an earlier question that i asked just a day or so ago, i wanted to inquire on the “correct” or “WordPress way” to pass session information between pages.
My use case is this: I am creating a application within a website (through the use of a plugin that i am writing) with its own custom login/password and based on that, i’m assigning that user a role and each role has certain screens that they will have access to. After they’ve logged in, I give them one or more links that allow them to proceed to those other pages. My question revolves around how to pass information from the login page to the other application pages. In each case, i want to pass a userid and the application role that they’ve already been granted.
My question resolves around “the right way” to do this. One way to do this would be to create a ‘virtual’ form and programmatically ‘save’ the information and pass the data via $_POST. But, is this the “proper WP way” to do this? My first alternative choice to even attempting that was to set a $_SESSION value like this:
$_SESSION['emp_name'] = $_POST['emp_name'];
Then, on the target page, i attempt to retrieve that value like this:
$emp_name = $_SESSION['emp_name'];
I tried setting the values via setting $_SESSION[‘username’] (for example), but found that this value did not persist between pages. I have also considered/tried cookies. However, i’m not sure this is the ‘right’ use of cookies either, which typically save data over multiple sessions, which i don’t necessarily want. In addition, it’s my understanding that you have to set the cookie value in the ‘init’ action hook and put the code in the functions.php file. During that action, we won’t know the value to set in the cookie.
In short, i “think” i can fall back on the ‘virtual form’ method and pass the data via POST, but is that the “WordPress Pro” way to do it? I’m looking for pointers/examples on how to do this “the right way”.
Many thanks for anyone who can take the time to help out.
- The topic ‘“Proper” way to pass session information’ is closed to new replies.