Reputation: 1405
I need to work with sessions only to maintain form input from edit.php
(1) to post.php
(2) to edit.php
(3) if the input validation failed on save_post
, so that my users don't have to re-fill the form at edit.php
(3) with their previous entries at edit.php
(1) since post.php
(2) does not re-post to edit.php
(3). There are a number of ways to transient data through this tunnel:
$_POST
vars may be too much for the querystringamong others.
I will not use sessions for login (since I'm using WordPress, I'll let WordPress take care of that.) From scouring the web, I inserted the following in my functions.php
:
/*
* manage sessions
*/
// http://wblinks.com/notes/secure-session-management-tips
// http://devondev.com/2012/02/03/using-the-php-session-in-wordpress/
// http://en.wikipedia.org/wiki/Session_fixation
// http://www.php.net/manual/en/function.session-regenerate-id.php
if (is_admin()) add_action('init', 'empl_sesh_start', 1);
add_action('wp_login', 'empl_sesh_cleanup');
add_action('wp_logout', 'empl_sesh_cleanup');
function empl_sesh_start() {
session_start();
// check if loaded session is server-generated
if (!isset($_SESSION['IS_SERVER_TRUSTED']))
session_regenerate_id(true); // if not, regenerate id and clean-up previous session files
// regenerate id for every request
session_regenerate_id();
$_SESSION['IS_SERVER_TRUSTED'] = true; // set flag
}
// cleanup
function empl_sesh_cleanup() {
session_start(); // needed for the rest of this function to work
$_SESSION = array(); // cleanup session variables
session_regenerate_id(true); // regenerate id and clean-up previous session files
session_destroy();
}
I just need to know if I got it right. I'm particularly concerned with
I'm also concerned with what I read about unsetting cookies and their complexities - do I need to do that? I don't use any cookies, I just use two session variables:
// persist form vars to next load
$_SESSION['empl_form_inputs'][] = $_POST['empl_age'];
// more similar code here...
$_SESSION['empl_form_inputs'][] = $_POST['empl_id'];
// persist message array to next load
$_SESSION['empl_messages'] = $empl_messages;
I posted this here instead of at wordpress.stackexchange.com as (I think) this is not really a WordPress question, but more of a PHP Session best practice.
RESOLUTION: I ended up abandoning the whole session caboodle and implemented collision-addressed (at least for my use case) transients with a 1 second expiration. thanks @Robbie
Upvotes: 0
Views: 744
Reputation: 17710
You shouldn't need sessions for posting from a form, not even in wordpress.
Your script / plugin should read.
You'd use a session if you were using a wizard (multi-page form) approach. This way the form alsways shows what the user enters and errors.
However, to answer your question, your addactions() are probably what you want, but the functions are agressive.
Example of time-out:
if ($_SESSION['empl_form_expires'] > time()) { // Also add user agent chack or something
$_SESSION['empl_form_inputs'] = array(); // Clear values
} else {
$_SESSION['empl_form_expires'] = time() + 600; // Keep the time running
}
Upvotes: 2