我正在使用一个名为The Next Generation of Genealogy Site-Building (TNG) 并将其集成到WordPress中。要在WordPress模板中显示数据很容易,我已经成功地集成了注册功能,这样当用户在WordPress中注册时,他们就可以成功地添加到TNG用户表中。然而,我现在几乎秃顶了,因为我把头发扯下来,试图让会议正常进行。
TNG应用程序的结构允许管理员为每个用户设置不同的权限,以提供对站点数据的不同访问级别(添加、编辑、删除、查看活着的人的信息等)。这些权限在tng\\U users表中设置,并在用户登录时检索和分配给会话变量。我正在尝试编写一个函数,当用户登录WordPress时,该函数将设置TNG会话。
我的问题是,没有从函数中正确设置会话变量。但是,如果我获取函数中的所有代码并将其放在单独的php文件测试中。我单独运行的php,成功设置了所有会话变量,并分配了正确的权限。我所读到的关于此的所有内容都确保$\\u会话是一个超级全局会话,应该在函数中或函数外工作。。。那么,为什么它不起作用??????
启动会话的功能是:
function StartSession() {
if(!session_id()) {
session_start();
}
}
add_action(\'init\', \'StartSession\', 1);
add_action(\'wp_login\', \'StartSession\', 1);
创建会话变量的函数是:
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 );
关闭会话的功能是:
function tngwp_logout() {
// destroy the session
session_destroy();
}
add_action( \'wp_logout\', \'tngwp_logout\', 10 );
另一个奇怪的是,$wpdb->update也没有成功地更新函数中的lastlogin字段,但在函数外部进行了更新。我对此束手无策。有人能帮忙吗?
最合适的回答,由SO网友:cybmeta 整理而成
如果我记得的话,当用户使用wp loing登录时。php,它被重定向。因此,正确的流量应为:
在下一页加载(当用户被重定向时)中,挂接用户登录init
并检查用户是否正确登录,如果正确,则启动会话并填充$\\u会话钩住wp_login
和wp_logout
销毁会话(也在wp_login
, 如果用户登录另一个帐户,它将销毁以前的会话,并在下一个帐户上启动新会话init
).因此,我认为这应该是可行的:
//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;
}