PHP | Codeigniter重定向|无法修改标头信息

时间:2017-12-08 08:05:38

标签: php codeigniter redirect warnings require-once

当我尝试使用CodeIgniter中的redirect()函数时,我遇到了问题。

错误是(当我尝试调用addMed()函数时)

A PHP Error was encountered
Severity: Warning
Message: Cannot modify header information - headers already sent by (output started at /a_directory/CabUser.php:46)
Filename: helpers/url_helper.php
Line Number: 561
Backtrace:
File: /a_directory/CabUser.php> 
Line: 48
Function: redirect
File: /a_directory/index.php
Line: 315
Function: require_once

我的文件“CabUser.php”


<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class CabUser extends CI_Controller {

  function __construct() {
    parent::__construct();
    $this->load->database();
  }

  public function index() {
    if ($this->session->has_userdata('for_login')) {
        $userPrivelegies = $this->session->userdata['for_login']['privelegies'];
        if($userPrivelegies) {
            redirect('CabMed', 'refresh');
        }
        else {
            $this->load->model('Cab_model');
              $this->Cab_model->getUserData();
        }
    }
    else {
        redirect('login');
    }
  }

  function logout() {
    if ($this->session->has_userdata('for_login')) {
      $this->session->sess_destroy('for_login');
    }
    redirect('home', 'refresh');
  }

  function addMed() {
    $data['numePrenumeMedic'] = $this->input->post('medic');
    $username = $this->session->userdata['for_login']['username'];
    $this->db->where('username', $username);
    $query = $this->db->get('Users');
    $ID = $query->row()->ID;

    $this->db->where('ID', $ID);
    $this->db->update('UserData', $data);

    echo "<script>
            alert('Succes!!!');
          </script>";

    redirect('CabUser', 'refresh'); //here's the problem
  }

  function upload() {
    if ($this->input->post('update')) {
      $data['numePrenume'] = $this->input->post('name');  
      $data['numePrenume'] = $this->input->post('name');
      $data['facebook'] = $this->input->post('facebook');
      $data['ok'] = $this->input->post('ok');
      $data['instagram'] = $this->input->post('instagram');
      $data['datanasterii'] = $this->input->post('year') . '-' . $this->input->post('month') .'-' . $this->input->post('day');
      $data['telefon'] = $this->input->post('telefon');
      $data['adresa'] = $this->input->post('address');
      $data['sex'] = $this->input->post('sex');

      $sess = $this->session->userdata('for_login', 'username');

      $this->db->where('username',$sess['username']);
      $query = $this->db->get('Users');

      $ID = $query->row()->ID;

      $this->db->where('ID', $ID);
      $this->db->update('Users', $data);

      $this->db->where('ID', $ID);
      $this->db->set('numePrenumePacient', $data['numePrenume']);
      $this->db->update('Pacienti');    

      echo "<script>
            alert('Succes!!!');
          </script>";
      redirect('CabUser', 'refresh');
    }
    else {
      echo "<script>
            alert('Error to update. \n Try later please...);
          </script>";
      redirect('CabUser', 'refresh');
    }


  }

}
?>

1 个答案:

答案 0 :(得分:0)

您的解决方案是,

删除redirect() 来自您使用header功能的所有地方的代码。因为重定向函数使用php的<template v-if="mainPage"> <div class="col-md-12"> <h1>1 page</h1> </div> </template> <template v-else> <div class="col-md-12"> <h1>2 page</h1> </div> </template> export default { data () { return { text : '', mainPage: true, } }, 函数并且在任何标题之前,所以不应该回显或向浏览器发送任何内容。

这就是错误所说的。