PHP重定向循环错误问题

时间:2015-07-22 18:52:32

标签: php redirect infinite-loop

我搜索了论坛,但无法理解PHP重定向页面循环问题。

我有一个简单的PHP页面:

 <?php include('core/init.core.php');


$page_request="graph";

if (isset($_SESSION['account']['user_role']) && 
    $_SESSION['account']['user_role'] =='coaching_manager' ||
    $_SESSION['account']['user_role'] =='OEM'||
    $_SESSION['account']['user_role'] =='GM'||
    $_SESSION['account']['user_role'] =='director'||
    $_SESSION['account']['user_role'] =='proactive_coaching_manager') {
    $page_request="graph";
}
else
    $page_request='request_form';

if (isset($_GET['page_request'])) {
    $page_request=$_GET['page_request'];
}

$user = get_user_on_uin(strval($_SESSION['account']['ein']));

//var_dump(mysqli_num_rows($user));
//die();

if(mysqli_num_rows($user)>0){ //if user exists in the user table in sql


    //get user data from user table in sql
    $user=$user->fetch_assoc();

        //update user details in db
    update_user_on_uin($_SESSION['account']['firstname'],
        $_SESSION['account']['lastname'],
        $_SESSION['account']['email'],
        $_SESSION['account']['phone'],
        $_SESSION['account']['ein'],
        $_SESSION['account']['managerein'],
        $_SESSION['account']['ouc'],
        $_SESSION['account']['user_role'],
        $_SESSION['account']['som'],
        $_SESSION['account']['gm']);

    switch ($user['user_role']){

        case 'engineer':
            //check if user have changed their role/ouc if yes then resetup the user
        if ($_SESSION['account']['ouc']==$user['ouc']) {

           if ($page_request=='request_form') {
            header('Location: request-form-engineer.php');
            die();
        }
        elseif ($page_request=='view_requests') {
            header('Location: view-requests-engineer.php');
            die();
        }
        elseif ($page_request=='view_accepted_requests') {
            header('Location: view-accepted-requests.php');
            die();
        }
        elseif ($page_request=='view_rejected_requests') {
            header('Location: view-rejected-requests.php');
            die();
        }
        elseif ($page_request=='view_coach_assigned_requests') {
            header('Location: view-coachassigned-requests.php');
            die();
        }
        elseif ($page_request=='view_completed_requests') {
            header('Location: view-completed-requests.php');
            die();
        } 
    } 

    else{
        update_user_on_uin($_SESSION['account']['firstname'],
            $_SESSION['account']['lastname'],
            $_SESSION['account']['email'],
            $_SESSION['account']['phone'],
            $_SESSION['account']['ein'],
            $_SESSION['account']['managerein'],
            $_SESSION['account']['ouc'],
            $_SESSION['account']['user_role'],
            $_SESSION['account']['som'],
            $_SESSION['account']['gm']);
            //header('Location: setup-account.php?message=update');
        header('Location: dashboard.php');
        die();
    }


    case 'manager':

    if ($_SESSION['account']['ouc']==$user['ouc']) {

        if ($page_request=='request_form') {
            header('Location: request-form-om.php');
            die();
        }
        elseif ($page_request=='view_requests') {
            header('Location: view-requests-om.php');
            die();
        }
        elseif ($page_request=='view_accepted_requests') {
            header('Location: view-accepted-requests.php');
            die();
        }
        elseif ($page_request=='view_rejected_requests') {
            header('Location: view-rejected-requests.php');
            die();
        }
        elseif ($page_request=='view_coach_assigned_requests') {
            header('Location: view-coachassigned-requests.php');
            die();
        }
        elseif ($page_request=='view_completed_requests') {
            header('Location: view-completed-requests.php');
            die();
        }            
    } 

    else{
     update_user_on_uin($_SESSION['account']['firstname'],
        $_SESSION['account']['lastname'],
        $_SESSION['account']['email'],
        $_SESSION['account']['phone'],
        $_SESSION['account']['ein'],
        $_SESSION['account']['managerein'],
        $_SESSION['account']['ouc'],
        $_SESSION['account']['user_role'],
        $_SESSION['account']['som'],
        $_SESSION['account']['gm']);
            //header('Location: setup-account.php?message=update');
     header('Location: dashboard.php');
               // header('Location: setup-account.php?message=update');
     die();
 }



 case 'coaching_manager':

 if ($_SESSION['account']['ouc']==$user['ouc']) {

    if ($page_request=='request_form') {
        header('Location: request-form-coaching-manager.php');
        die();
    }
    elseif ($page_request=='view_requests') {
        header('Location: view-requests-coaching-manager.php');
        die();
    }
    elseif ($page_request=='view_accepted_requests') {
        header('Location: view-accepted-requests.php');
        die();
    }
    elseif ($page_request=='view_rejected_requests') {
        header('Location: view-rejected-requests.php');
        die();
    }
    elseif ($page_request=='view_coach_assigned_requests') {
        header('Location: view-coachassigned-requests.php');
        die();
    }
    elseif ($page_request=='view_completed_requests') {
        header('Location: view-completed-requests.php');
        die();
    }
    elseif ($page_request=='graph') {
        header('Location: graph.php');
        die();
    }    

}

else{
    update_user_on_uin($_SESSION['account']['firstname'],
        $_SESSION['account']['lastname'],
        $_SESSION['account']['email'],
        $_SESSION['account']['phone'],
        $_SESSION['account']['ein'],
        $_SESSION['account']['managerein'],
        $_SESSION['account']['ouc'],
        $_SESSION['account']['user_role'],
        $_SESSION['account']['som'],
        $_SESSION['account']['gm']);
            //header('Location: setup-account.php?message=update');
    header('Location: dashboard.php');
        //header('Location: setup-account.php?message=update');
    die();
}


case 'coach':

if ($_SESSION['account']['ouc']==$user['ouc']) {

   if ($page_request=='request_form') {
    header('Location: request-form-coach.php');
    die();
}
elseif ($page_request=='view_requests') {
    header('Location: view-requests-coach.php');
    die();
}
elseif ($page_request=='view_accepted_requests') {
    header('Location: view-accepted-requests.php');
    die();
}
elseif ($page_request=='view_rejected_requests') {
    header('Location: view-rejected-requests.php');
    die();
}
elseif ($page_request=='view_coach_assigned_requests') {
    header('Location: view-coachassigned-requests.php');
    die();
}
elseif ($page_request=='view_completed_requests') {
    header('Location: view-completed-requests.php');
    die();
} 

}

else{
    update_user_on_uin($_SESSION['account']['firstname'],
        $_SESSION['account']['lastname'],
        $_SESSION['account']['email'],
        $_SESSION['account']['phone'],
        $_SESSION['account']['ein'],
        $_SESSION['account']['managerein'],
        $_SESSION['account']['ouc'],
        $_SESSION['account']['user_role'],
        $_SESSION['account']['som'],
        $_SESSION['account']['gm']);
            //header('Location: setup-account.php?message=update');
    header('Location: dashboard.php');
                //header('Location: setup-account.php?message=update');
    die();
}


default:
break;


    }//end of switch

}   //end of if block

else{
    //header('Location: setup-account.php?message=new_user');
    //var_dump("expression");
   // die();
    if( insert_user($_SESSION['account']['firstname'],
        $_SESSION['account']['lastname'],
        $_SESSION['account']['email'],
        $_SESSION['account']['phone'],
        $_SESSION['account']['ein'],
        $_SESSION['account']['managerein'],
        $_SESSION['account']['ouc'],
        $_SESSION['account']['user_role'],
        $_SESSION['account']['som'],
        $_SESSION['account']['gm']) )
    {


        header("Location: dashboard.php");
        die();

    }
    else
    {
        var_dump('Sorry we do not recognise you. bye!');
        die();
    }
           // header("Location: dashboard.php");
           // die(); */

}


    //if no, redirect to role selection page
?>

此网页名称为dashboard.php init.core.php页面开始会话。

任务很简单。如果找到用户,则使用$_Session详细信息更新用户详细信息。如果没有,则添加新用户并重定向到此页面以显示相关信息。我可以看到它执行else部分,即插入用户但是它在数据库中插入了很多记录,最终放弃了ERR_TOO_MANY_REDIRECTS

我哪里错了?

2 个答案:

答案 0 :(得分:1)

我看不到您为$_SESSION['account']['id']设置值的位置。如果没有设置,它将始终转到主要的else并插入一个新用户。

话虽如此,您是否在脚本开头调用session_start()?如果不是,那么在脚本调用之间将不会保持这些值。

答案 1 :(得分:-3)

您错过了开关盒的支架。

$user = get_user_on_id(strval($_SESSION['account']['id']));
if (mysqli_num_rows($user) > 0){ //if user exists in the user table in sql
    //get user data from user table in sql
    $user = $user->fetch_assoc();
    //update user details with current details in db
    update_user_on_uin($_SESSION['account']['firstname'],
                       $_SESSION['account']['lastname'],
                       $_SESSION['account']['email'],
                       $_SESSION['account']['id'],
                       $_SESSION['account']['user_role']
    );
    switch ($user['user_role']){
        case 'engineer': {
            //check if user have changed their department if yes then update user details
            if ($_SESSION['account']['ouc'] == $user['ouc']){
                if ($page_request == 'request_form'){
                    header('Location: request-form-engineer.php');
                    die();
                }
            }
            else{
                update_user_on_uin($_SESSION['account']['firstname'],
                                   $_SESSION['account']['lastname'],
                                   $_SESSION['account']['email'],
                                   $_SESSION['account']['id'],
                                   $_SESSION['account']['user_role']
                );
                header('Location: dashboard.php');
                die();
            }
        } //case engineer
    }
}//end of switch
else{
    if (insert_user($_SESSION['account']['firstname'],
                    $_SESSION['account']['lastname'],
                    $_SESSION['account']['email'],
                    $_SESSION['account']['id'],
                    $_SESSION['account']['ouc'],
                    $_SESSION['account']['user_role']
    )){
        header("Location: dashboard.php");
        die();
    }
    else{
        var_dump('Sorry we do not recognise you. bye!');
        die();
    }
}