PHP不会重定向

PHP不会重定向,php,redirect,Php,Redirect,我在一个简单的php论坛上工作,当我登录/注销或注册时,它不会将我重定向回index.php 例如,我的注销代码 <?php if(isset($_POST['do_logout'])){ //Create User Object $user = new User; if($user->logout()){ redirect('index.php','You are now logged out','success'); } } else {

我在一个简单的php论坛上工作,当我登录/注销或注册时,它不会将我重定向回index.php 例如,我的注销代码

<?php
if(isset($_POST['do_logout'])){
    //Create User Object

$user = new User;
    if($user->logout()){
        redirect('index.php','You are now logged out','success');
} 
} else {
    redirect('index.php');
}
和我的重定向定义代码

<?php
function redirect($page = FALSE, $message = NULL, $message_type = NULL) {
    if (is_string ($page)) {
        $location = $page;
    } else {
        $location = $_SERVER ['index.php'];
    }
    if($message != NULL){
        $_SESSION['message'] = $message;
    }
    if($message_type != NULL){
        $_SESSION['message_type'] = $message_type;
    }
header('Location: '.$location);
    exit(); 
}

定义脚本位于一个php文件中,该文件包含在注销脚本之前

没有$_SERVER['index.php']这样的东西。您不需要使用代码段,可以使用{}用于标记代码的工具。请检查PHP错误日志,以获取有关已发送的标题的警告。是的,我已收到错误标题。在调用重定向之前,您需要确保不打印任何输出。错误消息应该告诉您打印输出的是哪行,您需要修复它。没有工作的人谢谢分享
<?php
function redirect($page = FALSE, $message = NULL, $message_type = NULL) {

    $location = (is_string($page)) ? $page : 'index.php';

    if($message != NULL){
        $_SESSION['message'] = $message;
    }

    if($message_type != NULL){
        $_SESSION['message_type'] = $message_type;
    }

    header('Location: '. $location); exit(); 
}