0

I am working with an external application called The Next Generation of Genealogy Site-Building (TNG) and integrating it into WordPress. It has been an easy matter to get the data to display inside a WordPress Template, and I have successfully integrated the registration function so that when a user registers in WordPress, they are successfully added to the TNG user table. However, I am now nearly bald from tearing my hair out trying to get sessions to work properly.

The way the TNG application is structured allows the admin to set different privileges to each user providing various levels of access to the site data (adding, editing, deleting, viewing info for living people, etc.). These permissions are set in the tng_users table and retrieved and assigned to session variables when a user logs in. I am trying to write a function that will set up the TNG session when a user logs into WordPress.

My issue is that the session variables are not being correctly set from within the function. However, if I take all the code that is in the function and put it in a separate php file test.php that I run separately, all the session variables are successfully set and the correct privileges are assigned. Everything I've read about this assures that $_SESSION is a super global that should work in or out of a function... so WHY is it not working??????

The function to start a session is:

function StartSession() {
    if(!session_id()) {
        session_start();
    }
}
add_action('init', 'StartSession', 1);
add_action('wp_login', 'StartSession', 1);

The function to create the session variables is:

function tngwp_processlogin() {
    global $wpdb, $current_user;
    define('PATH', $_SERVER['DOCUMENT_ROOT']);
    get_currentuserinfo();
    $username = $current_user->user_login;
    $tng_folder = $_SERVER['DOCUMENT_ROOT'] . '/genealogy/';
    include($tng_folder.'config.php');
    include($tng_folder."subroot.php");
    $session_language = $_SESSION['session_language'];
    $session_charset = $_SESSION['session_charset'];
    $languages_path = "languages/";
    include($tng_folder.'getlang.php');

    $tng_user = $wpdb->get_row("
                SELECT * 
                FROM tng_users 
                WHERE username = '$username'", 
                ARRAY_A
            );
    $newdate = date ("Y-m-d H:i:s", time() + ( 3600 * $time_offset ) );
    $userid = $tng_user['userID'];
    $wpdb->update( 
        'tng_users', 
        array( 'lastlogin' => $newdate ), 
        array( 'userID' => $userid ), 
        array( '%s' ), 
        array( '%d' )
    );

    $newroot = ereg_replace( "/", "", $rootpath );
    $newroot = ereg_replace( " ", "", $newroot );
    $newroot = ereg_replace( "\.", "", $newroot );
    setcookie("tnguser_$newroot", $tng_user['username'], time()+31536000, "/");
    setcookie("tngpass_$newroot", $tng_user['password'], time()+31536000, "/");
    setcookie("tngpasstype_$newroot", $tng_user['password_type'], time()+31536000, "/");

    $_SESSION['currentuser'] = $tng_user['username'];
    if ( $tng_user['role']=='admin' ) { 
        $_SESSION['allow_admin'] = 1;
        setcookie("tngloggedin_$newroot", "1", 0, "/");
    }
    else { $_SESSION['allow_admin'] = 0; }
    $logged_in = $_SESSION['logged_in'] = 1;
    $allow_edit = $_SESSION['allow_edit'] = ($tng_user['allow_add'] == 1 ? 1 : 0);
    $allow_add = $_SESSION['allow_add'] = ($tng_user['allow_add'] == 1 ? 1 : 0);
    $tentative_edit = $_SESSION['tentative_edit'] = $tng_user['tentative_edit'];
    $allow_delete = $_SESSION['allow_delete'] = ($tng_user['allow_delete'] == 1 ? 1 : 0);

    $allow_media_edit = $_SESSION['allow_media_edit'] = ($tng_user['allow_edit'] ? 1 : 0);
    $allow_media_add = $_SESSION['allow_media_add'] = ($tng_user['allow_add'] ? 1 : 0);
    $allow_media_delete = $_SESSION['allow_media_delete'] = ($tng_user['allow_delete'] ? 1 : 0);

    $_SESSION['mygedcom'] = $tng_user['mygedcom'];
    $_SESSION['mypersonID'] = $tng_user['personID'];

    $allow_living = $_SESSION['allow_living'] = $tng_user['allow_living'];
    $allow_private = $_SESSION['allow_private'] = $tng_user['allow_private'];

    $allow_ged = $_SESSION['allow_ged'] = $tng_user['allow_ged'];
    $allow_pdf = $_SESSION['allow_pdf'] = $tng_user['allow_pdf'];
    $allow_profile = $_SESSION['allow_profile'] = $tng_user['allow_profile'];

    $allow_lds = $_SESSION['allow_lds'] = $tng_user['allow_lds'];

    $assignedtree = $_SESSION['assignedtree'] = $tng_user['gedcom'];
    $assignedbranch = $_SESSION['assignedbranch'] = $tng_user['branch'];
    $currentuser = $tng_user['username'];
    $_SESSION['currentuser'] = $currentuser;
    $currentuserdesc = $_SESSION['currentuserdesc'] = $tng_user['description'];
    $session_rp = $_SESSION['session_rp'] = $rootpath;

    $wpdb->flush();
    return $tngusername;
}
add_action( 'wp_login', 'tngwp_processlogin', 3 );

The function to close a session is:

function tngwp_logout() {
    // destroy the session 
    session_destroy();
}
add_action( 'wp_logout', 'tngwp_logout', 10 );

What is also weird is that the $wpdb->update also isn't successfully updating the lastlogin field in the function but does outside the function. I'm at my wits end over this. Can anyone help?

2
  • PHP sessions must be initiated as the very first request of a page processing. WordPress's init hook may process other PHP code before your attempt to begin a session, which would prevent creating or maintaining a valid session. Is it possible to use a physical cookie instead of a session?
    – user12479
    Mar 17, 2015 at 16:39
  • When say that the code works if you put it in a separate file, how/when do you load that file and execute the function?
    – cybmeta
    Mar 17, 2015 at 17:03

1 Answer 1

1

If I remember, when a user is logged in using wp-loing.php, it is redirected. So, the correct flow should be:

  1. User log in
  2. In next page load (when user is redirected), hook on init and check if the user is correctly logged in, if so, start session and populate $_SESSION.
  3. Hook on wp_login and wp_logout to destroy session (also in wp_login, if user log in another account it will destroy previous session and start new one on next init).

So, I think this should work:

//Start session and populated variables for logged-in users
add_action('init', 'StartSession', 1);
function StartSession() {

    //Check if current user is logged in WordPress
    if( is_user_logged_in() ) {

        if(!session_id()) {
            session_start();
        }

        //You may need some check here of inside tngwp_processlogin() to avoid running
        //the same process over and over again if it is not needed
        tngwp_processlogin();

    }
}


//Destroy session if user logout or login in another account
add_action( 'wp_login', 'tngwp_session_destroy' );
add_action( 'wp_logout', 'tngwp_session_destroy' );
function tngwp_session_destroy() {
    // destroy the session 
    session_destroy();
}

function tngwp_processlogin() {
    global $wpdb, $current_user;
    define('PATH', $_SERVER['DOCUMENT_ROOT']);
    get_currentuserinfo();
    $username = $current_user->user_login;
    $tng_folder = $_SERVER['DOCUMENT_ROOT'] . '/genealogy/';
    include($tng_folder.'config.php');
    include($tng_folder."subroot.php");
    $session_language = $_SESSION['session_language'];
    $session_charset = $_SESSION['session_charset'];
    $languages_path = "languages/";
    include($tng_folder.'getlang.php');

    $tng_user = $wpdb->get_row("
                SELECT * 
                FROM tng_users 
                WHERE username = '$username'", 
                ARRAY_A
            );
    $newdate = date ("Y-m-d H:i:s", time() + ( 3600 * $time_offset ) );
    $userid = $tng_user['userID'];
    $wpdb->update( 
        'tng_users', 
        array( 'lastlogin' => $newdate ), 
        array( 'userID' => $userid ), 
        array( '%s' ), 
        array( '%d' )
    );

    $newroot = ereg_replace( "/", "", $rootpath );
    $newroot = ereg_replace( " ", "", $newroot );
    $newroot = ereg_replace( "\.", "", $newroot );
    setcookie("tnguser_$newroot", $tng_user['username'], time()+31536000, "/");
    setcookie("tngpass_$newroot", $tng_user['password'], time()+31536000, "/");
    setcookie("tngpasstype_$newroot", $tng_user['password_type'], time()+31536000, "/");

    $_SESSION['currentuser'] = $tng_user['username'];
    if ( $tng_user['role']=='admin' ) { 
        $_SESSION['allow_admin'] = 1;
        setcookie("tngloggedin_$newroot", "1", 0, "/");
    }
    else { $_SESSION['allow_admin'] = 0; }
    $logged_in = $_SESSION['logged_in'] = 1;
    $allow_edit = $_SESSION['allow_edit'] = ($tng_user['allow_add'] == 1 ? 1 : 0);
    $allow_add = $_SESSION['allow_add'] = ($tng_user['allow_add'] == 1 ? 1 : 0);
    $tentative_edit = $_SESSION['tentative_edit'] = $tng_user['tentative_edit'];
    $allow_delete = $_SESSION['allow_delete'] = ($tng_user['allow_delete'] == 1 ? 1 : 0);

    $allow_media_edit = $_SESSION['allow_media_edit'] = ($tng_user['allow_edit'] ? 1 : 0);
    $allow_media_add = $_SESSION['allow_media_add'] = ($tng_user['allow_add'] ? 1 : 0);
    $allow_media_delete = $_SESSION['allow_media_delete'] = ($tng_user['allow_delete'] ? 1 : 0);

    $_SESSION['mygedcom'] = $tng_user['mygedcom'];
    $_SESSION['mypersonID'] = $tng_user['personID'];

    $allow_living = $_SESSION['allow_living'] = $tng_user['allow_living'];
    $allow_private = $_SESSION['allow_private'] = $tng_user['allow_private'];

    $allow_ged = $_SESSION['allow_ged'] = $tng_user['allow_ged'];
    $allow_pdf = $_SESSION['allow_pdf'] = $tng_user['allow_pdf'];
    $allow_profile = $_SESSION['allow_profile'] = $tng_user['allow_profile'];

    $allow_lds = $_SESSION['allow_lds'] = $tng_user['allow_lds'];

    $assignedtree = $_SESSION['assignedtree'] = $tng_user['gedcom'];
    $assignedbranch = $_SESSION['assignedbranch'] = $tng_user['branch'];
    $currentuser = $tng_user['username'];
    $_SESSION['currentuser'] = $currentuser;
    $currentuserdesc = $_SESSION['currentuserdesc'] = $tng_user['description'];
    $session_rp = $_SESSION['session_rp'] = $rootpath;

    $wpdb->flush();
    return $tngusername;
 }
2
  • 1
    THANK YOU!!!!!!!! That worked!! Lesson learned: Instead of driving myself crazy, go ask a pro! If I had enough rep points I'd plus your answer! Mar 17, 2015 at 17:20
  • Glad to help. You are wellcome.
    – cybmeta
    Mar 17, 2015 at 21:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.