简体   繁体   中英

header location unable to redirect

Overview:

I am having a block of code where I am checking for a condition and redirect to a page( manage_contents.php ) if the condition( mysqli_num_rows($pages_set)>0 ) satisfies and thus everything after the if condition should not execute as the header has been redirected.

if(mysqli_num_rows($pages_set)>0) {
    $_SESSION["message"] = "can't delete a subject with pages";
    redirect_to("manage_contents.php?subject={$current_subject["id"]}");    
}

    $id = $current_subject["id"];

    //Standard delete query
    $query = "DELETE FROM subjects WHERE id = {$id} LIMIT 1";
    $result = mysqli_query($connection,$query);

    if($result && mysqli_affected_rows($connection)==1) {
        $_SESSION["message"] = "Subjects deleted";
        redirect_to("manage_contents.php");
    }else{
        $_SESSION["message"]="Subject deletion failed";
        redirect_to("manage_contents.php?subject={$id}");   
    }

But if I execute the above the code isn't redirecting(even if the if condition satisfied) and executing whatever is next to the if loop.

redirect_to function:

function redirect_to($new_location) {
        header("Location:".$new_location);  
    }

Working solution for me:

if(mysqli_num_rows($pages_set)>0) {
        $_SESSION["message"] = "can't delete a subject with pages";
        redirect_to("manage_contents.php?subject={$current_subject["id"]}");    
    } else {

        $id = $current_subject["id"];

        //Standard delete query
        $query = "DELETE FROM subjects WHERE id = {$id} LIMIT 1";
        $result = mysqli_query($connection,$query);

        if($result && mysqli_affected_rows($connection)==1) {
            $_SESSION["message"] = "Subjects deleted";
            redirect_to("manage_contents.php");
        }else{
            $_SESSION["message"]="Subject deletion failed";
            redirect_to("manage_contents.php?subject={$id}");   
        }
}

So,if i put all the code inside of the else statement then ofcourse it doesn't go to the else section when the if condition satisfies and hence works just fine.

Doubt:

Why the header doesn't redirect if I just leave the code outside of else section and why does it redirect just fine when the code is inside of else block?

I think both the code should work exactly same as far I know about header redirects.(when header is redirected all the following codes execution should skip).

Setting the Location header is not going to stop processing of the current PHP page. You'll need to explicitly exit :

function redirect_to($new_location)
{
    header("Location: $new_location");
    exit;
}

In other languages/frameworks (ASP.NET's Response.Redirect comes to mind) the current page's execution is stopped for you, but this does not happen in PHP and nothing in the documentation for header indicates otherwise.

Take a look to the caveat in http://php.net/manual/en/function.header.php : when you use the "Location" header you must take care that the code below it will not be executed (and your second code comply with it).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM