我正在登录系统,根据用户类型和user_activation_status重定向到适当的URL。以下代码:
$.post(
'./assets/tasks/process-login.php',
{
email:email,
password:password
},
function(data){
console.log(data);
if(data==1){
$("#lblMessage_frmLogin").css("color","green");
$("#lblMessage_frmLogin").text("Login successful.. Redirecting to your homepage").fadeIn();
window.location.href=siteurl + '/my-home/';
}else if(data==-2){
$("#lblMessage_frmLogin").css("color","red");
$("#lblMessage_frmLogin").html("Looks like you haven't registered with us, Please <a href='" + siteurl + "/home'> click here </a> to register.").fadeIn();
}else{
$("#lblMessage_frmLogin").css("color","red");
$("#lblMessage_frmLogin").text("Login unsuccessful, please recheck the details you have entered.").fadeIn();
}
});
我没有包括./assets/tasks/process-login.php
,因为担心延长帖子。它只返回1表示成功,0表示密码错误,-2表示不存在的电子邮件。
在被重定向到/my-home/之后,在页面的头部有一个user_activation_status的检查,反过来,它必须将页面重定向到适当的页面。
/我的家/index . php
<?php include './includes/meta.php';
include './includes/header-nav.php';
check_user_action_status($_SESSION['logged_in_userid']);
switch ($usertype) {
case 1:
$selected_dashboard='candidate-dashboard.php';
break;
case 2:
$selected_dashboard='expert-dashboard.php';
break;
case 3:
$selected_dashboard='admin-dashboard.php';
break;
default:
if($usertype=="") user_logout();
break;
}
include './includes/page-content/'.$selected_dashboard;
include './includes/footer.php';
?>
功能:
/**
Function to check if the user has confirmed email and filled profile, uploaded docs or disabled.
Params : userid
Returns none; Redirects to appropriate page, or logs out if user not logged in.
**/
function check_user_action_status($userid){
$userid=trim(sqlescapes($userid));
if($get_row = mysqli_query($GLOBALS['conn'], "select user_type,user_activation_status from tbl_users where user_id=$userid")){
if (mysqli_num_rows($get_row) > 0) {
if($got_row=mysqli_fetch_object($get_row)){
if($got_row->user_activation_status==0){
header("location:".$GLOBALS['siteurl']."/my-home/confirm-email");
}else if($got_row->user_activation_status==1){
header("location:".$GLOBALS['siteurl']."/my-home/my-profile?id=start");
}else if($got_row->user_activation_status==-1){
header("location:".$GLOBALS['siteurl']."/my-home/disabled-account");
}
if($got_row->user_type==2){
if($got_row->user_activation_status==2){
header("location:".$GLOBALS['siteurl']."/my-home/upload-docs");
}
}
}
}else{
user_logout();
}
}else{
user_logout();
}
}
(请注意,$GLOBALS['siteurl']='http://localhost/tlaqi/'
。user_logout()
和sqlescapes()
为对应功能的用户自定义函数)
逻辑在本地主机上完美地工作,但是当我上传到服务器时,登录后,即使满足条件,它也不会重定向到check_user_action_status()
中定义的适当页面。相反,它只是加载/my-home/index.php。
我哪里错了?
Header必须在输出之前设置。在php脚本的最开始添加ob_start();
。如果它包含另一个文件,那么最后不要使用?>。
or try
header("location:".$GLOBALS['siteurl']."/my-home/confirm-email");
exit();
或echo "<script>location='your_url.com'</script>";
我以前遇到过这种问题,现在我不再使用header('Location: pageExample.php');
了,而是使用JavaScript的document.location
。
改变你的:
header('Location: page1.php');
到像这样的东西:
echo "<script type='text/javascript'> document.location = 'page1.php'; </script>";