从JS重定向后,标头位置在服务器上不起作用(但在localhost上工作)

I am working on login system that redirects to appropriate URL based on usertype and user_activation_status. The code below:

$.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();
            }
        });

I haven't included ./assets/tasks/process-login.php for fear of lengthening the post. It just does echo 1 for success, 0 for wrong password, -2 for un-existing email.

After being redirected to /my-home/, there is a check in the header of the page for user_activation_status, which in turn, has to redirect the page to appropriate page.

/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:

/**
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();
        }

}

(please note, $GLOBALS['siteurl']='http://localhost/tlaqi/'. user_logout() and sqlescapes() are user defined functions for corresponding functionality)

The logic works perfectly on localhost but when I have uploaded to the server, after login, it is not redirecting to appropriate page as defined in check_user_action_status() even though the conditions are met. Instead, it is just loading /my-home/index.php.

Where am I going wrong?

Header has to set before output is sent.Add ob_start(); at very beginning of the php script. If it include another file then do not use ?> in the end.

or try

header("location:".$GLOBALS['siteurl']."/my-home/confirm-email");
exit();

or echo "<script>location='your_url.com'</script>";

I have experienced that kind of issue before and now I'm not using header('Location: pageExample.php'); anymore, instead I'm using JavaScript's document.location.

Change your:

header('Location: page1.php');

To something like this:

echo "<script type='text/javascript'> document.location = 'page1.php'; </script>";