如何打破重定向循环

时间:2018-01-19 20:28:16

标签: php redirect

如何打破重定向循环?

提交表单并通过POST方法向DB发送数据时,我尝试通过重定向到空白页来清空字段。

redirect.php

    <Window.Resources>
    <local:MainWindowViewModel x:Key="mKeyMainWindowViewModel" />

    <Style TargetType="{x:Type local:PropertyUpdateControl}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:PropertyUpdateControl}">
                    <TextBlock 
                        Margin="25" Text="{TemplateBinding Label}"></TextBlock>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <DataTemplate x:Key="mDataTemplate" DataType="{x:Type local:PropertyUpdateViewModel}">
        <local:PropertyUpdateControl
            Value="{Binding Value}"></local:PropertyUpdateControl>

    </DataTemplate>


</Window.Resources>

<Window.DataContext>
    <StaticResource ResourceKey="mKeyMainWindowViewModel"/>
</Window.DataContext>

<Grid>
    <StackPanel>
        <ContentPresenter Content="{Binding PropertyUpdateViewModel}" ContentTemplate="{StaticResource mDataTemplate}"/>
        <Button Content="Update Value" Command="{Binding ChangeCommand}"></Button>
    </StackPanel>

</Grid>

并重定向回表单页面

$host  = $_SERVER['HTTP_HOST'];
$uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra = 'redirect.php';
header("Location: https://$host$uri/$extra");
exit;

但是,不是由F5发起的单一重定向,而是陷入无限循环的重定向。

如何停止这个不受控制的循环并使其仅由F5启动?

<?php 
$host  = $_SERVER['HTTP_HOST'];
$uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra = 't54646.php';
header("Location: https://$host$uri/$extra");
exit;
?>

1 个答案:

答案 0 :(得分:0)

那么你所要做的就是

if ($res) {
    // Only redirect when the action is successfull
    $host  = $_SERVER['HTTP_HOST'];
    $uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
    $extra = 't54646.php';
    header("Location: https://$host$uri/$extra");
    exit;
} else {
    echo "Not updated";
}