简体   繁体   English

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

[英]PHP | Codeigniter Redirect | Cannot modify header information

I have a problem when i try to use redirect() function from CodeIgniter. 我尝试从CodeIgniter使用redirect()函数时遇到问题。

The error is (when i try to call addMed() function) 错误是(当我尝试调用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

My File "CabUser.php" 我的文件“ 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');
    }


  }

}
?>

Your solution is, 您的解决方案是

Remove echo "<script> alert('Succes!!!'); </script>"; 删除echo "<script> alert('Succes!!!'); </script>"; code from everywhere where you are using redirect() function. 您在使用redirect()函数的任何地方编写代码。 Because redirect function uses header function of php and before any header, you should not echo or send anything to browser. 因为重定向功能使用php的header功能,并且在任何标头之前,所以您不应回显或向浏览器发送任何内容。

And that is what the error says. 这就是错误的意思。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM