Session lost after form submit in wordpress

The session I set is lost after the form is submitted.

I had built the session class to set new session, unset and so on. In function.php of wordpress template.

function.php

if (!session_id()) {
    session_start();
}
include get_template_directory() . "/custom/session.php";

Session.php

class session {
    function __construct() {

    }

    function set_flashdata($name, $value) {
        $_SESSION[$name] = $value;
    }

    function flashdata($name) {
        if (isset($_SESSION[$name])) {
            $str = $_SESSION[$name];
            return $str;
        } else {
            return FALSE;
        }
    }

    function userdata($name) {
        if (isset($_SESSION[$name])) {
            return $_SESSION[$name];
        } else {
            return FALSE;
        }
    }

    function set_userdata($name, $value) {
        $_SESSION[$name] = $value;
    }

    function unset_userdata($name) {
        if (isset($_SESSION[$name])) {
            unset($_SESSION[$name]);
        }
    }
}

I try to set session as :

<?php 
    $sess = new session();
    $sess->set_userdata('sess_name',"some value");
?>
<form action="get_permalink(212);">
    //input buttons
</form>

After submit the form it goes to the permalink(212). Then I tried.

<?php
    $sess = new session();
    $value = $sess->userdata('sess_name');
    var_dump($value);      //returns false. That means session is lost after form submit. Why?
?>

Read more here: Session lost after form submit in wordpress

Leave a Reply

Your email address will not be published. Required fields are marked *