由于文件结构已经发送了标头

时间:2016-05-31 14:20:40

标签: php

在任何人将此标记为重复之前。我知道错误的原因,这是由于我的结构..我想要帮助解决这个问题。

首先,我想创建一个有点动态的网站,以便我index.php使用这个:

<?php
    include '../../includes/functions.php';
    sec_session_start();
    include 'headers/set_session_variables.php';

    include "headers/header.php";
    include "headers/navbar.php";


    $view = 'index';
    if (!empty($_GET['view'])) {
        $tmp_view = basename($_GET['view']);

        if (file_exists("templates/{$tmp_view}.php")) {
                $view = $tmp_view;
        }
    }

    include "templates/{$view}.php";

    include "headers/footer.php";
?>

只是查看get请求(例如index.php?view=profile)并检查是否有模板文件,如果有,则显示它。

在我的个人资料模板中,我可以显示个人资料并编辑个人资料。

在个人资料页面的顶部,我添加了一个文件,用于处理名为form_actions.php

的帖子数据
<?php

if(isset($_POST['type'])) {
    $switch = $_POST['type'];

    switch($switch) {
        case 'create_team':
            $team = new teams();
            $return_msg = $team->create_team($db, $_POST['team_name'], $_POST['abb_team_name'], $_POST['p'], $_POST['website'], $_POST['team_logo'], $userID);
            break;
        case 'edit_profile':
            $edit_profile = new edit_user_profile($db, $_POST['firstname'], $_POST['lastname'], $_POST['location'], $_POST['age'], $_POST['facebook'], $_POST['twitter'], $_POST['youtube'],
                $_POST['instagram'], $_POST['website'], $_POST['profilePicOld'], $_POST['headerPicOld'], $_FILES["profilepic"], $_FILES["profileheader"], $userID);
            $return_msg = $edit_profile->update_profile();
            header('Refresh: 2; URL=?view=profile&pid='.$username);
            break;
        default:
    }
}

但是由于这种结构,编辑一个配置文件后的重定向导致:

  

无法修改标题信息 - 已由(...)

发送的标题

有没有什么方法可以解决这个问题,还是因为结构会阻止它工作这么简单?

提前致谢。

1 个答案:

答案 0 :(得分:0)

既然它适合你,我把代码放在这里作为答案(这不是最好的,胸围仍然有效):

如果标题已经发送过,您可以将一些javascript代码写入redirect to antoher page,然后通过您的PHP代码编写echo,如下所示:

case 'edit_profile':
    $edit_profile = new edit_user_profile($db, $_POST['firstname'], $_POST['lastname'], $_POST['location'], $_POST['age'], $_POST['facebook'], $_POST['twitter'], $_POST['youtube'],
        $_POST['instagram'], $_POST['website'], $_POST['profilePicOld'], $_POST['headerPicOld'], $_FILES["profilepic"], $_FILES["profileheader"], $userID);
    $return_msg = $edit_profile->update_profile();
    echo '<script text="text/javascript">window.location = "index.php?view=profile&pid=' . $username . '"</script>';
    break;

如果您认为自己会多次这样做,那么您可以创建自己的功能:

function redirect($url)
{
    echo '<script type="text/javascript">window.location = "' . $url . '"</script>';
}