我正在使用登录系统,该系统根据usertype和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,而后者又必须将页面重定向到适当的页面。
/my-home/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()
是相应功能的用户定义函数。
逻辑在localhost上完美运行但是当我上传到服务器后,在登录后,即使符合条件,它也不会重定向到check_user_action_status()
中定义的适当页面。相反,它只是加载/my-home/index.php。
我哪里错了?
答案 0 :(得分:0)
在输出发送之前必须设置标题。在php脚本的最开头添加ob_start();
。如果它包含另一个文件,那么不要使用?&gt;最后。
或尝试
header("location:".$GLOBALS['siteurl']."/my-home/confirm-email");
exit();
或echo "<script>location='your_url.com'</script>";
答案 1 :(得分:0)
之前我遇到过这样的问题,现在我不再使用header('Location: pageExample.php');
,而是使用JavaScript document.location
。
改变你的:
header('Location: page1.php');
对于这样的事情:
echo "<script type='text/javascript'> document.location = 'page1.php'; </script>";