0

I have a php header location problem what i am doing is getting some data from my database and then making a if else check and depending on that i am redirecting the user to the next page ,here goes my code

            if($claim_status == 'Pending Verification')
        {
            $page = "pending-verification.php?verifyid=".$id."";
        }
        elseif($claim_status == 'Pending PO Clearance')
        {
            $page = "po-clearance.php?verifyid=".$id."";
        }
        elseif($claim_status == 'Payment Approved')
        {
            $page = "payment-approved.php?verifyid=".$id."";
        }
        elseif($claim_status == 'Payment Receipt')
        {
            $page = "confirm-claim.php?verifyid=".$id."";
        }
        elseif($claim_status == 'Billing')
        {
            $page = "billing.php?verifyid=".$id."";
        }
        elseif($claim_status == 'Billed')
        {
            $page = "billed.php?verifyid=".$id."";
        }
        header("Location:$page");

$claim_status is a variable i am getting from my database.

The error i am getting is ( this page has a redirect loop) how can i take care of this Please help me

Thank You

6
  • Try replacing the last header statement with print header("Location:$page"); -> print("Location:$page"); and see what gets printed. Commented Nov 4, 2011 at 12:25
  • Also, do you have any rewrite rules acting? Commented Nov 4, 2011 at 12:26
  • @SalmanA cool dude i was not sure it can be done thumbs up Commented Nov 4, 2011 at 12:28
  • 1
    you should not use so many else if, use an array instead Commented Nov 4, 2011 at 12:31
  • ...or a case statement, or a functional mapping. Commented Nov 4, 2011 at 12:48

4 Answers 4

2

It appears you emit a Location header no matter what (the header statement isn't inside an if … at least not in the code you've shared), so it will always redirect (and always with a malformed Location header, even when $page is defined, it fails to be an absolute URI).

Only emit the Location when you actually want to redirect.

Sign up to request clarification or add additional context in comments.

Comments

0

Not answering your question, but you should avoid so many else if. Your code should look like this:

$allowedStatus = array ( 
    'Pending Verification' => 'pending-verification',
    'Pending PO Clearance' => 'po-clearance'
    //> Etc
);

At this point is easy to build your $page var

$page = $allowedStatus[$claim_status] . '.php?verifyid=' . $id;

Comments

0

Use case instead of if-elsif statements. Also, make sure there's no other php output before header();

switch($claim_status) {

    case 'Pending Verification':
        $page = "pending-verification.php?verifyid=".$id."";
        break;

    case 'Pending PO Clearance';
        $page = "po-clearance.php?verifyid=".$id."";
        break;

    case 'Payment Approved';
        $page = "payment-approved.php?verifyid=".$id."";
        break;

    case 'Payment Receipt';
        $page = "confirm-claim.php?verifyid=".$id."";
        break;

    case 'Billing';
        $page = "billing.php?verifyid=".$id."";
        break;  

    case 'Billed';
        $page = "billed.php?verifyid=".$id."";
        break;


    default:
        $page = ''; //Set a default rediraction page in case $claim_status is invalid.
        break;

}

header("Location:$page");

2 Comments

this is just bad as the else if, read my answer to get a better code
I saw it after i posted the answer. Much better version. Only thing you need to do, is to check if the array key exists so that the $page will be always valid. if (array_key_exists($claim_status, $allowedStatus)) { $page = $allowedStatus[$claim_status] . '.php?verifyid=' . $id; } else { $page = "error_handling.php"; }
0

You may use javascript to stop redirection loop

<?php
   $redirect_page = 'page.php';
   echo "<script type='text/javascript'>window.location=$redirect_page</script>";
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.