page redirect from custom file

I’ve created a custom file that calls a global function to carry out background processing which is working correctly and I want to redirect so I’ve added the following event after reading other topics:

    public function pageUnload()
    {
        //Log("Page Unload");
    $this->setSuccessMessage("Roll Forward Successful");
    $this->terminate("index");            
    }

pageUnload displays the message but doesn’t redirect whereas pageLoad displays the message and redirects successfully, but doesn’t carry out any processing!Contents of custom file below:

if (CurrentUserLevel() == -1) {
    $league_id = ExecuteScalar("SELECT current_league_id FROM season_master WHERE current_season='1'");
} else {
    $league_id = CurrentUserInfo("league_id");

}
$rsold=ExecuteRow("SELECT * FROM league WHERE league_id = " . $league_id);
$rsnew=$rsold;
$rsnew["season_id"] = $rsold["season_id"] + 1;
print_r2(rollForward($rsold,$rsnew));
//echo "Season Roll Forward complete\n";
  1. Page_Unload server event is not what you thought, you can not redirect there.
  2. The content of Custom File is HTML for showing, you should not use it to do server side work and then redirect.
  3. You better want to read Using Route_Action server event.