0

我需要一种方法来检测“ foreach ”是否不成功。如果不成功,则重复当前“ else ”中的相同错误消息

<?php

    if(file_exists('redirects.xml')) {
        $xml = simplexml_load_file('redirects.xml');
        if(isset($_GET['r']) && $_GET['r'] != '' && !is_array($_GET['r'])) {
            foreach($xml->short as $shorts) {
                if($shorts->name == $_GET['r']) {
                    header('Location: '.$shorts->url);
                    break;
                }
            }
        }
        else {
            header("refresh:2;url=http://www.wlatw.co/");
            echo '<div align="center" style="font-weight: bolder; font-size: 24px;">Malformed URL</div><div align="center" style="font-weight: bolder; font-size: 16px;">Redirecting...</div>';
        }
    }
?>
4

2 回答 2

3

在开始循环之前创建一个标志;失败时设置为不成功。

<?php

    if(file_exists('redirects.xml')) {
        $xml = simplexml_load_file('redirects.xml');
        if(isset($_GET['r']) && $_GET['r'] != '' && !is_array($_GET['r'])) {
            $success = false; // set the flag
            foreach($xml->short as $shorts) {
                if($shorts->name == $_GET['r']) {
                    header('Location: '.$shorts->url);
                    $success = true;
                    break;
                }
            }

            if ($success) { // do what you want when not success ful.
                header("refresh:2;url=http://www.wlatw.co/");
                echo '<div align="center" style="font-weight: bolder; font-size: 24px;">Malformed URL</div><div align="center" style="font-weight: bolder; font-size: 16px;">Redirecting...</div>';
            }
        }
        else {
            header("refresh:2;url=http://www.wlatw.co/");
            echo '<div align="center" style="font-weight: bolder; font-size: 24px;">Malformed URL</div><div align="center" style="font-weight: bolder; font-size: 16px;">Redirecting...</div>';
        }
    }
?>

但是查看您的代码,您可以在设置标题后退出:

foreach($xml->short as $shorts) {
    if($shorts->name == $_GET['r']) {
        header('Location: '.$shorts->url);
        exit;
        break;
    }
}

注意:正如@Sverri M. Olsen 所说,您应该始终在设置 Location 标头后停止脚本,无论是使用 die、exit 还是您拥有的任何其他机制。

于 2013-02-20T03:47:41.763 回答
2

您需要添加一个变量来跟踪循环的状态。

<?php

if(file_exists('redirects.xml')) {
    $xml = simplexml_load_file('redirects.xml');
    if(isset($_GET['r']) && $_GET['r'] != '' && !is_array($_GET['r'])) {
        $url_is_malformed = false;
        foreach($xml->short as $shorts) {
            if($shorts->name == $_GET['r']) {
                header('Location: '.$shorts->url);
                break;
            }
        }
        $url_is_malformed = true;
    }
    else {
        $file_doesnt_exist = true;
    }

    if( $file_doesnt_exist || $url_is_malformed )
    {
        header("refresh:2;url=http://www.wlatw.co/");
        echo '<div align="center" style="font-weight: bolder; font-size: 24px;">Malformed URL</div><div align="center" style="font-weight: bolder; font-size: 16px;">Redirecting...</div>';
    }
}

?>

于 2013-02-20T03:48:59.917 回答