Search code examples
wordpresssessioncontact-form-7

Session data ends with wrong visitor? How is that possible?


We are facing a problem with a website. A prospect noticed that a form was prefilled with data from another prospect.

We have 4 contactforms on one page and when a contact has entered a form than the data is saved in a session. When they return the forms are prefilled with that data from the session. But now we got a printscreen from a prospect with data from a previous prospect.

Here's the code for the session:

//This will start a session, then save the posted_data[something] to the session
add_action('init', 'myStartSession', 1);
add_action('wp_logout', 'myEndSession');
add_action('wp_login', 'myEndSession');

function myStartSession() {
    if ( empty( $_SESSION ) ) {
        session_start();
    }
}

function myEndSession() {
    session_destroy ();
}


function set_sessions($cf7){
  /* Use WPCF7_Submission object's get_posted_data() method to get it. */
$submission = WPCF7_Submission::get_instance();

if ( $submission ) {
    $posted_data = $submission->get_posted_data();
}
    


  $_SESSION['formdata']['firstname'] = $posted_data['firstname'];
  $_SESSION['formdata']['lastname'] = $posted_data['lastname'];
  $_SESSION['formdata']['email2'] = $posted_data['email2'];
  $_SESSION['formdata']['tel'] = $posted_data['tel'];
  $_SESSION['formdata']['postcode'] = $posted_data['postcode'];
  $_SESSION['formdata']['adres'] = $posted_data['adres'];

    return false;
}

add_action( 'wpcf7_before_send_mail', 'set_sessions' );



function firstname(){

    return $_SESSION['formdata']['firstname'];
}
add_shortcode('firstname', 'firstname');

function lastname(){
    return $_SESSION['formdata']['lastname'];
}
add_shortcode('lastname', 'lastname');

function email2(){
    return $_SESSION['formdata']['email2'];
}
add_shortcode('email2', 'email2');

function tel(){
    return $_SESSION['formdata']['tel'];
}
add_shortcode('tel', 'tel');

function postcode(){
    return $_SESSION['formdata']['postcode'];
}
add_shortcode('postcode', 'postcode');


function adres(){
    return $_SESSION['formdata']['adres'];
}
add_shortcode('adres', 'adres');

Solution

  • Have you checked if you are caching the pages? This could lead to an issue like that.