Php,捕获重定向

时间:2014-09-23 20:54:08

标签: php redirect

所以我有一个代码:

function testMe ($redir)
{
    if ($redir)
    {
        header ('Location: /');
    }
}

我想测试这个函数,如果它使用重定向,但我不知道如何捕获它。它就像捕捉异常一样

编辑:所以我想如果header()没有写,但知道它已被调用

1 个答案:

答案 0 :(得分:2)

//calling the function which somewhere does this
header('Location: /');

//testing
foreach (headers_list() as $header) {
    if (strpos($header, 'Location:') !== false) {
        //header location set, function works, removing the set header
        header_remove('Location');
    }
}