-1

I have searched the forum but couldnt understand the PHP redirect page loop issue.

I have a simple PHP page:

 <?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
?>

This page name is dashboard.php init.core.php page starts the session.

The task is simple. If user found then update user details with $_Session details. If not then add a new user and redirect to this page to display relevant info. I can see it executes the else part i.e. inserts user but It inserts alot of records in the database before eventually giving up stating ERR_TOO_MANY_REDIRECTS

Where am I going wrong?

Shery
  • 1,808
  • 5
  • 27
  • 51
  • 2
    Whats the name of the file this code lives in? – Rob Jul 22 '15 at 18:53
  • Please use `exit;` unless you explicitly want to **abort** the script with a `die()` – RiggsFolly Jul 22 '15 at 18:55
  • 1
    @RiggsFolly The manual explicitly states: `die — Equivalent to exit`. They both abort the script. And when doing a header redirect, it is actually suggested to abort. – Jonathan Kuhn Jul 22 '15 at 18:56
  • @JonathanKuhn [The Manual](http://php.net/manual/en/function.header.php) Please count the usage of `die()` and the usage of `exit;` – RiggsFolly Jul 22 '15 at 19:01
  • @JonathanKuhn See Hayley Watson's opinion on `die()` at [The manual](http://php.net/manual/en/function.die.php) – RiggsFolly Jul 22 '15 at 19:03
  • This `$user=$user->fetch_assoc();` is at best going to cause a memory leak – RiggsFolly Jul 22 '15 at 19:05
  • Your `switch()` statement is badly coded [see the manual](http://php.net/manual/en/control-structures.switch.php) A case does not require a `{}` wrap. Also there is not much point in coding a switch if you have only one case. – RiggsFolly Jul 22 '15 at 19:15
  • @RiggsFolly I have alot of other cases...I presented one case to save space – Shery Jul 22 '15 at 19:22
  • @Rob The URL name is dashboard.php – Shery Jul 22 '15 at 19:22
  • Looks like you keep hitting the header("Location:dashboard.php) for whatever reason. – Rob Jul 22 '15 at 19:27
  • @Rob just added the code as it is. Please check now and suggest? – Shery Jul 22 '15 at 19:30
  • 1
    @RiggsFolly My point is that `die` is the same thing as `exit` and even the manual states they are equivalent. `exit;` will do the exact same thing as `die;`. They both are language constructs and take in an optional status code or message that will be output to the user. `die` is just a alias of `exit`. Hayley Watson's comment doesn't change the fact that they are exactly the same. With redirects it is suggested to use them to prevent the rest of the page from running, see here: http://stackoverflow.com/questions/2747791/why-i-have-to-call-exit-after-redirection-through-headerlocation-in-php – Jonathan Kuhn Jul 22 '15 at 19:33
  • What is `get_user_on_uin()` actually returning? Is it a mysqli result set? For whatever reason, mysqli_num_rows($user) appears to always return either 0 or false. – Joseph Jul 22 '15 at 19:35
  • `function get_user_on_uin($uin){ $connection = get_db(); $query = 'SELECT * FROM users WHERE uin='.$uin; return $connection->query($query); }` – Shery Jul 22 '15 at 19:46
  • @JonathanKuhn With respect that link is not to a manual. die signifies a error termination and exit a clean termination. – RiggsFolly Jul 22 '15 at 20:39
  • @RiggsFolly in any case...it didn't work... – Shery Jul 22 '15 at 21:07

1 Answers1

1

I don't see where you are setting the value for $_SESSION['account']['id']. Without that being set, it is going to always go to the primary else and insert a new user.

That being said, are you calling session_start() at the beginning of the script? If not, then the values will not be maintained between script calls.

Joseph
  • 1,988
  • 14
  • 21
  • Don't worry about the `$_SESSION['account']['id']` I am setting it up correctly (double checked) just didn't show here – Shery Jul 22 '15 at 19:23
  • Are you calling `session_start()` at the beginning of the script as well? http://php.net/manual/en/function.session-start.php – Joseph Jul 22 '15 at 19:25
  • Yes I am... I going to try to add the whole page as it – Shery Jul 22 '15 at 19:26