user254153
user254153

Reputation: 1883

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?
?>

Upvotes: 11

Views: 2788

Answers (6)

G4Hu
G4Hu

Reputation: 328

Sounds to me like session_start() is not set at the start of the page that get_permalink(212;) refers to.

I have almost no experience with WP itself though, so I might misunderstand the functionality of get_permalink()

Upvotes: 3

Udit Trivedi
Udit Trivedi

Reputation: 246

I agree with the answer from @rock3t to initialize session in constructor of class, but every time a class object is initiated, it will go to check for session!

Instead, if you are fine, the simplest way to get access to session is by adding following lines to your wp-config.php file before the call to wp-settings

if (!session_id())
    session_start();

This will set/initialize session globally and you won't need to set/check for session_start in constructor of a class.

Thank you.

Upvotes: 0

rock3t
rock3t

Reputation: 2233

You need to move session start/resume into your Session's constructor.

Like so:

class session
{
    function __construct()
    {
        if (! session_id()) {
            session_start();
        }
    }

Another thing to mention, every time you'll do new Session you'll be getting an object of the same functionality working with same global variable $_SESSION.

You don't need more than one $session object, it would be a good time to look into Singleton pattern.

Upvotes: 8

Hossein
Hossein

Reputation: 3107

It might be due to www. at the start of your website domain. Make sure that both of pages use the same structure.

Also I faced with the same issue long time ago when the form sends the data to a secured address (https://)

I hope these two items may help you.

Upvotes: 3

Miguel
Miguel

Reputation: 1421

You have to call always session_start() for each request.

The mission of session_start() is:

  • Creates a new session
  • Restart an existing session

That means, if you have created a session, and you don't call to the method session_start(), the variable $_SESSION is not going to be fulfilled.

Except: If in your php.ini you have set the option session.auto_start to 1, then, in that case it is not needed to call the session_start() because the variable $_SESSION is fulfilled implicitly.

Upvotes: 8

Mohsin khan
Mohsin khan

Reputation: 136

You need to use wordpress global variable for condition that session is set or not something like :

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

Upvotes: 3

Related Questions